diff --git a/plugin/preload.js b/plugin/preload.js index 979dbec..01012e7 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -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'); diff --git a/src/components/quickFeatures/ApiServer.vue b/src/components/quickFeatures/ApiServer.vue index 6332a94..b70bc11 100644 --- a/src/components/quickFeatures/ApiServer.vue +++ b/src/components/quickFeatures/ApiServer.vue @@ -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 @@ - - - - +
+ +
+
+ + + +
@@ -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() { diff --git a/src/js/options/defaultProfile.js b/src/js/options/defaultProfile.js index 1725ea4..d6ddb85 100644 --- a/src/js/options/defaultProfile.js +++ b/src/js/options/defaultProfile.js @@ -22,7 +22,6 @@ export default { apiServer: { enable: false, port: 33442, - cmd: "", serverStatus: false } }