命令执行控制台,测试OK - 洛阳翼展电脑


命令执行控制台,测试OK

2026-2-5 乱云飞

import console;
import wsock.tcp.simpleHttpServer;

// 创建服务器
var httpServer = wsock.tcp.simpleHttpServer("0.0.0.0", 8055);

console.setTitle("命令执行服务器 V0.2");
console.log("服务器启动: http://localhost:8055");
console.log("URL直接执行示例: http://localhost:8055/notepad.exe");

/*
每次回复代码请更新版本号!!!
比较完美的版本v0.1
*/

// 请求处理器
httpServer.run(
    function(response, request) {
        // 获取路径
        var path = request.path;
        
        // 路由处理
        if (path == "/" or path == "/index" or path == "/main.aardio") {
            // 返回美化后的主页
            response.headers["Content-Type"] = "text/html; charset=utf-8";
            
            // 构建HTML内容
            var html = '<html>'
            html += '<!DOCTYPE html><html><head><meta charset="utf-8"><title>命令执行控制台</title>'
            html += '<style>'
            html += 'body{font-family:Arial,sans-serif;margin:0;padding:20px;background:#f5f5f5;}'
            html += '.container{max-width:800px;margin:0 auto;background:white;border-radius:0px;box-shadow:0 2px 10px rgba(0,0,0,0.1);overflow:hidden;}'
            html += '.header{background:linear-gradient(90deg,#4a6ee0,#6a4de0);color:white;padding:20px;}'
            html += '.header h1{margin:0;font-size:24px;}'
            html += '.main{padding:20px;}'
            html += '.input-area{display:flex;gap:10px;margin-bottom:15px;}'
            html += '#cmd{flex:1;padding:10px;border:1px solid #ddd;border-radius:0px;font-size:16px;}'
            html += '#runBtn{padding:10px 20px;background:#4a6ee0;color:white;border:none;border-radius:0px;cursor:pointer;font-size:16px;}'
            html += '#runBtn:hover{background:#3a5ed0;}'
            html += '.quick-buttons{margin:10px 0;}'
            html += '.qbtn{padding:5px 10px;margin-right:8px;margin-bottom:8px;background:#e8efff;color:#4a6ee0;border:none;border-radius:0px;cursor:pointer;}'
            html += '.output{background:#1e1e1e;color:#fff;padding:15px;border-radius:0px;margin-top:20px;font-family:Consolas,monospace;min-height:150px;overflow:auto;white-space:pre-wrap;}'
            html += '</style>'
            html += '</head><body>'
            html += '<div class="container">'
            html += '<div class="header"><h1>命令执行控制台</h1><div>端口: 8033</div></div>'
            html += '<div class="main">'
            html += '<div class="input-area">'
            html += '<input type="text" id="cmd" placeholder="输入命令,如: cmd.exe、notepad.exe、dir...">'
            html += '<button id="runBtn" onclick="runCmd()">执行</button>'
            html += '</div>'
            html += '<div class="quick-buttons">'
            html += '<button class="qbtn" onclick="runCmdWithCommand(\'cmd.exe\')">cmd.exe</button>'
            html += '<button class="qbtn" onclick="runCmdWithCommand(\'notepad.exe\')">notepad.exe</button>'
            html += '<button class="qbtn" onclick="runCmdWithCommand(\'calc.exe\')">calc.exe</button>'
            html += '<button class="qbtn" onclick="runCmdWithCommand(\'mspaint.exe\')">mspaint.exe</button>'
            html += '<button class="qbtn" onclick="runCmdWithCommand(\'ipconfig /all\')">ipconfig /all</button>'
            html += '</div>'
            html += '<div class="output" id="out">等待命令...</div>'
            html += '</div></div>'
            html += '<script>'
            html += 'function setCommand(cmd){document.getElementById("cmd").value=cmd;return false;}'
            html += 'async function runCmdWithCommand(cmd){'
            html += 'document.getElementById("cmd").value=cmd;'
            html += 'await runCmd();'
            html += '}'
            html += 'async function runCmd(){'
            html += 'var cmd=document.getElementById("cmd").value;'
            html += 'var out=document.getElementById("out");'
            html += 'if(!cmd){out.textContent="请输入命令";return;}'
            html += 'out.textContent="执行中...";'
            html += 'try{'
            html += 'var r=await fetch("/execute",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({command:cmd})});'
            html += 'var d=await r.json();'
            html += 'if(d.success){out.textContent=d.output||"(无输出)";}else{out.textContent="错误:"+d.output;}'
            html += '}catch(e){out.textContent="请求失败:"+e;}'
            html += '}'
            html += 'document.getElementById("cmd").addEventListener("keypress",function(e){if(e.key==="Enter"){runCmd();}});'
            html += '</script>'
            html += '</body></html>'
            
            response.write(html);
        }
        else if (path == "/execute") {
            // 处理命令执行(保持原始逻辑)
            response.headers["Content-Type"] = "application/json";
            
            if (request.method != "POST") {
                response.write({success=false; output="需要POST请求"});
                return;
            }
            
            // 导入需要的库
            import process.popen;
            import JSON;
            
            // 解析JSON
            var body = request.postJson();
            if (!body or !body.command) {
                response.write({success=false; output="需要命令参数"});
                return;
            }
            
            // 执行命令
            try {
                var cmd = body.command;
                
                // 判断是否是EXE程序(需要显示窗口)
                var cmdLower = string.lower(cmd);
                var isExeProgram = string.find(cmdLower, "%.exe$") 
                    or cmd == "cmd"
                    or string.find(cmdLower, "^notepad")
                    or string.find(cmdLower, "^calc")
                    or string.find(cmdLower, "^mspaint");
                
                if (isExeProgram) {
                    // 对于EXE程序,使用 cmd /c start 来显示窗口
                    var fullCmd = 'cmd /c start "" ' + cmd;
                    var proc = process.popen(fullCmd);
                    if (!proc) {
                        response.write({success=false; output="无法执行命令"});
                        return;
                    }
                    
                    var output = proc.readAll();
                    proc.close();
                    
                    response.write({success=true; output="程序已启动(显示窗口中)"});
                } else {
                    // 对于非EXE命令,使用原版的隐藏窗口方式
                    var proc = process.popen(cmd, null, {flags = 0x08000000});
                    if (!proc) {
                        response.write({success=false; output="无法执行命令"});
                        return;
                    }
                    
                    var output = proc.readAll();
                    proc.close();
                    
                    // 限制输出长度
                    if (#output > 5000) {
                        output = string.left(output, 5000) + "\n... (输出过长)";
                    }
                    
                    response.write({success=true; output=output});
                }
            } catch(e) {
                response.write({success=false; output="执行出错"});
            }
        }
        else if (path == "/status") {
            // 状态页面
            response.headers["Content-Type"] = "application/json";
            response.write({status="运行中", port=8033, time=time()});
        }
        else if (path == "/test") {
            // 测试页面
            response.headers["Content-Type"] = "text/html; charset=utf-8";
            response.write("<h1>测试</h1><p>服务器正常</p>");
        }
        else if (path == "/favicon.ico") {
            // 忽略favicon请求
            response.statusCode = 404;
            response.write("");
        }
        else {
            // 处理通过URL直接执行的命令
            // 去除开头的斜杠
            var cmd = string.right(path, #path - 1);
            if (!cmd or cmd == "") {
                response.statusCode = 404;
                response.write("404 - 未找到: " + path);
                return;
            }
            
            // 执行命令(复用原有执行逻辑)
            try {
                response.headers["Content-Type"] = "application/json";
                
                // 判断是否是EXE程序(需要显示窗口)
                var cmdLower = string.lower(cmd);
                var isExeProgram = string.find(cmdLower, "%.exe$") 
                    or cmd == "cmd"
                    or string.find(cmdLower, "^notepad")
                    or string.find(cmdLower, "^calc")
                    or string.find(cmdLower, "^mspaint");
                
                if (isExeProgram) {
                    // 对于EXE程序,使用 cmd /c start 来显示窗口
                    var fullCmd = 'cmd /c start "" ' + cmd;
                    console.log("URL执行(显示窗口): " + fullCmd);
                    
                    var proc = process.popen(fullCmd);
                    if (!proc) {
                        response.write({success=false; output="无法执行命令"});
                        return;
                    }
                    
                    var output = proc.readAll();
                    proc.close();
                    
                    response.write({success=true; output="程序已启动(显示窗口中)", command=cmd});
                } else {
                    // 对于非EXE命令,使用原版的隐藏窗口方式
                    console.log("URL执行(隐藏窗口): " + cmd);
                    
                    var proc = process.popen(cmd, null, {flags = 0x08000000});
                    if (!proc) {
                        response.write({success=false; output="无法执行命令"});
                        return;
                    }
                    
                    var output = proc.readAll();
                    proc.close();
                    
                    // 限制输出长度
                    if (#output > 5000) {
                        output = string.left(output, 5000) + "\n... (输出过长)";
                    }
                    
                    response.write({success=true; output=output, command=cmd});
                }
            } catch(e) {
                console.log("URL执行出错: ", e);
                response.headers["Content-Type"] = "application/json";
                response.write({success=false; output="执行出错: " + tostring(e), command=cmd});
            }
        }
    }
);

console.pause();
本文链接:http://80c.cc/ez/858.html
0

发表评论:

VirtualCamera:在录像机中虚拟一个通道显示电脑桌面、视频文件、USB摄像头~
预ICP备10086-001号 © 翼展网/80C.CC 技术支持/洛阳翼展科技
TEL / 13213610060 QQ / 345794501
Powered by emlog