mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-07 21:46:12 +08:00
实时保存后台服务的脚本
This commit is contained in:
parent
64e585a7e5
commit
a53c1a16e0
@ -534,8 +534,8 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
|
||||
ext = option.ext,
|
||||
charset = option.charset,
|
||||
scptarg = option.scptarg || "";
|
||||
let script = getQuickcommandTempFile(ext, 'quickcommandTempScript')
|
||||
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
|
||||
let script = getQuickcommandTempFile(ext, 'quickcommandTempScript');
|
||||
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
|
||||
if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode);
|
||||
fs.writeFileSync(script, cmd);
|
||||
// var argvs = [script]
|
||||
@ -543,7 +543,7 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
|
||||
// argvs = argv.split(' ')
|
||||
// argvs.push(script);
|
||||
// }
|
||||
var child, cmdline
|
||||
let child, cmdline;
|
||||
if (bin.slice(-7) == 'csc.exe') {
|
||||
cmdline = `${bin} ${argv} /out:"${script.slice(0, -2) + 'exe'}" "${script}" && "${script.slice(0, -2) + 'exe'}" ${scptarg}`
|
||||
} else if (bin == 'gcc') {
|
||||
@ -557,33 +557,34 @@ window.runCodeFile = (cmd, option, terminal, callback) => {
|
||||
// 在终端中输出
|
||||
if (terminal) cmdline = getCommandToLaunchTerminal(cmdline)
|
||||
child = child_process.spawn(cmdline, {
|
||||
encoding: 'buffer',
|
||||
shell: true
|
||||
})
|
||||
// var chunks = [],
|
||||
// err_chunks = [];
|
||||
encoding: 'buffer',
|
||||
shell: true
|
||||
});
|
||||
// var chunks = [],
|
||||
// err_chunks = [];
|
||||
console.log('Running: ' + cmdline);
|
||||
child.stdout.on('data', chunk => {
|
||||
if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode)
|
||||
callback(chunk.toString(), null)
|
||||
// chunks.push(chunk)
|
||||
})
|
||||
if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode);
|
||||
callback(chunk.toString(), null);
|
||||
// chunks.push(chunk)
|
||||
});
|
||||
child.stderr.on('data', stderr => {
|
||||
if (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode)
|
||||
callback(null, stderr.toString())
|
||||
// err_chunks.push(err_chunk)
|
||||
})
|
||||
// child.on('close', code => {
|
||||
// let stdout = chunks.join("");
|
||||
// let stderr = err_chunks.join("");
|
||||
// callback(stdout, stderr)
|
||||
// })
|
||||
if (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode);
|
||||
callback(null, stderr.toString());
|
||||
// err_chunks.push(err_chunk)
|
||||
});
|
||||
// child.on('close', code => {
|
||||
// let stdout = chunks.join("");
|
||||
// let stderr = err_chunks.join("");
|
||||
// callback(stdout, stderr)
|
||||
// })
|
||||
return child
|
||||
}
|
||||
|
||||
let httpServer
|
||||
const dbStorage = utools.dbStorage;
|
||||
let httpServer;
|
||||
window.quickcommandHttpServer = () => {
|
||||
let run = (cmd = '', port = 33442) => {
|
||||
let run = (port = 33442) => {
|
||||
let httpResponse = (res, code, result) => {
|
||||
// 只收受一次 console.log,接收后就关闭连接
|
||||
if (res.finished) return
|
||||
@ -593,38 +594,39 @@ window.quickcommandHttpServer = () => {
|
||||
if (result) res.write(result);
|
||||
res.end();
|
||||
}
|
||||
let runUserCode = (res, cmd, userVars) => {
|
||||
let runUserCode = (res, userVars) => {
|
||||
let cmd = dbStorage.getItem('cfg_serverCode');
|
||||
// 不需要返回输出的提前关闭连接
|
||||
if (!cmd.includes('console.log')) httpResponse(res, 200)
|
||||
if (!cmd.includes('console.log')) httpResponse(res, 200);
|
||||
window.runCodeInSandbox(cmd, (stdout, stderr) => {
|
||||
// 错误返回 500
|
||||
if (stderr) return httpResponse(res, 500, stderr)
|
||||
return httpResponse(res, 200, stdout)
|
||||
if (stderr) return httpResponse(res, 500, stderr.join(" "));
|
||||
return httpResponse(res, 200, stdout.join(" "));
|
||||
}, userVars)
|
||||
}
|
||||
httpServer = http.createServer()
|
||||
httpServer = http.createServer();
|
||||
httpServer.on('request', (req, res) => {
|
||||
if (req.method === 'GET') {
|
||||
let parsedParams = _.cloneDeep(url.parse(req.url, true).query)
|
||||
runUserCode(res, cmd, parsedParams)
|
||||
let parsedParams = _.cloneDeep(url.parse(req.url, true).query);
|
||||
runUserCode(res, parsedParams);
|
||||
} else if (req.method === 'POST') {
|
||||
let data = []
|
||||
let data = [];
|
||||
req.on('data', (chunk) => {
|
||||
data.push(chunk)
|
||||
data.push(chunk);
|
||||
})
|
||||
req.on('end', () => {
|
||||
let parsedParams
|
||||
let params = data.join("").toString()
|
||||
// 先尝试作为 json 解析
|
||||
let parsedParams;
|
||||
let params = data.join("").toString();
|
||||
// 先尝试作为 json 解析
|
||||
try {
|
||||
parsedParams = JSON.parse(params)
|
||||
parsedParams = JSON.parse(params);
|
||||
} catch (error) {
|
||||
parsedParams = _.cloneDeep(url.parse('?' + params, true).query)
|
||||
parsedParams = _.cloneDeep(url.parse('?' + params, true).query);
|
||||
}
|
||||
runUserCode(res, cmd, parsedParams)
|
||||
runUserCode(res, parsedParams);
|
||||
})
|
||||
} else {
|
||||
httpResponse(res, 405)
|
||||
httpResponse(res, 405);
|
||||
}
|
||||
})
|
||||
httpServer.listen(port, 'localhost');
|
||||
|
@ -6,10 +6,9 @@
|
||||
ref="editor"
|
||||
@typing="
|
||||
(val) => {
|
||||
if ($root.profile.quickFeatures.apiServer.cmd !== val) {
|
||||
$root.profile.quickFeatures.apiServer.cmd = val;
|
||||
}
|
||||
restartServer();
|
||||
if (cmd === val) return;
|
||||
cmd = val;
|
||||
saveCode();
|
||||
}
|
||||
"
|
||||
:style="{
|
||||
@ -40,35 +39,34 @@
|
||||
<q-btn-group unelevated>
|
||||
<q-btn flat color="primary" icon="help" @click="showHelp" />
|
||||
<q-separator inset vertical />
|
||||
<q-btn
|
||||
flat
|
||||
color="negative"
|
||||
icon="stop"
|
||||
v-if="
|
||||
$root.profile.quickFeatures.apiServer.serverStatus && !restartTimer
|
||||
"
|
||||
@click="stopServer"
|
||||
label="停止服务"
|
||||
/>
|
||||
<q-btn
|
||||
flat
|
||||
color="warning"
|
||||
icon="restart_alt"
|
||||
v-else-if="
|
||||
$root.profile.quickFeatures.apiServer.serverStatus && !!restartTimer
|
||||
"
|
||||
@click="restartServer"
|
||||
label="正在重载"
|
||||
/>
|
||||
<q-btn
|
||||
flat
|
||||
color="primary"
|
||||
icon="play_arrow"
|
||||
v-else
|
||||
label="开启服务"
|
||||
@click="enableServer"
|
||||
>
|
||||
</q-btn>
|
||||
<div v-if="!!saveCodeTimer">
|
||||
<q-btn
|
||||
flat
|
||||
color="warning"
|
||||
icon="restart_alt"
|
||||
@click="saveCode"
|
||||
label="正在保存"
|
||||
/>
|
||||
</div>
|
||||
<div v-else>
|
||||
<q-btn
|
||||
flat
|
||||
color="negative"
|
||||
icon="stop"
|
||||
v-if="$root.profile.quickFeatures.apiServer.serverStatus"
|
||||
@click="stopServer"
|
||||
label="停止服务"
|
||||
/>
|
||||
<q-btn
|
||||
flat
|
||||
color="primary"
|
||||
icon="play_arrow"
|
||||
v-else
|
||||
label="开启服务"
|
||||
@click="enableServer"
|
||||
>
|
||||
</q-btn>
|
||||
</div>
|
||||
</q-btn-group>
|
||||
</div>
|
||||
</div>
|
||||
@ -82,32 +80,30 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
bottomHeight: 40,
|
||||
restartTimer: null,
|
||||
saveCodeTimer: null,
|
||||
cmd: null,
|
||||
};
|
||||
},
|
||||
mounted() {
|
||||
this.$refs.editor.setEditorValue(
|
||||
this.$root.profile.quickFeatures.apiServer.cmd
|
||||
);
|
||||
this.cmd =
|
||||
this.$root.utools.whole.dbStorage.getItem("cfg_serverCode") || "";
|
||||
this.$refs.editor.setEditorValue(this.cmd);
|
||||
this.$refs.editor.setEditorLanguage("javascript");
|
||||
this.restartTimer = null;
|
||||
},
|
||||
methods: {
|
||||
enableServer() {
|
||||
if (!this.cmd)
|
||||
return quickcommand.showMessageBox("脚本不能为空!", "warning");
|
||||
quickcommand
|
||||
.showConfirmBox(
|
||||
"请注意,该接口未做任何权限鉴定,千万不要试图将本端口转发出去,否则无异于将本机的 shell 权限暴露在公网!",
|
||||
"FBI WARNING"
|
||||
)
|
||||
.then((ok) => {
|
||||
if (!ok) return;
|
||||
.then(() => {
|
||||
this.$root.profile.quickFeatures.apiServer.serverStatus = true;
|
||||
window
|
||||
.quickcommandHttpServer()
|
||||
.run(
|
||||
this.$root.profile.quickFeatures.apiServer.cmd,
|
||||
this.$root.profile.quickFeatures.apiServer.port
|
||||
);
|
||||
.run(this.$root.profile.quickFeatures.apiServer.port);
|
||||
quickcommand.showMessageBox("启动服务成功!");
|
||||
});
|
||||
},
|
||||
@ -116,18 +112,11 @@ export default {
|
||||
this.$root.profile.quickFeatures.apiServer.serverStatus = false;
|
||||
quickcommand.showMessageBox("关闭服务成功!");
|
||||
},
|
||||
restartServer() {
|
||||
if (!this.$root.profile.quickFeatures.apiServer.serverStatus) return;
|
||||
clearTimeout(this.restartTimer);
|
||||
this.restartTimer = setTimeout(() => {
|
||||
window.quickcommandHttpServer().stop();
|
||||
window
|
||||
.quickcommandHttpServer()
|
||||
.run(
|
||||
this.$root.profile.quickFeatures.apiServer.cmd,
|
||||
this.$root.profile.quickFeatures.apiServer.port
|
||||
);
|
||||
this.restartTimer = null;
|
||||
saveCode() {
|
||||
clearTimeout(this.saveCodeTimer);
|
||||
this.saveCodeTimer = setTimeout(() => {
|
||||
this.$root.utools.whole.dbStorage.setItem("cfg_serverCode", this.cmd);
|
||||
this.saveCodeTimer = null;
|
||||
}, 1000);
|
||||
},
|
||||
showHelp() {
|
||||
|
@ -22,7 +22,6 @@ export default {
|
||||
apiServer: {
|
||||
enable: false,
|
||||
port: 33442,
|
||||
cmd: "",
|
||||
serverStatus: false
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user