🐛 修复停止方式

This commit is contained in:
刘嘉伟 2023-12-01 14:17:14 +08:00
parent 36507b05c2
commit 0a86a8cf10
2 changed files with 60 additions and 39 deletions

View File

@ -2,11 +2,13 @@ import {app, ipcMain} from "electron";
import {Config, getConfig} from "../storage/config"; import {Config, getConfig} from "../storage/config";
import {listProxy} from "../storage/proxy"; import {listProxy} from "../storage/proxy";
import {getVersionById} from "../storage/version"; import {getVersionById} from "../storage/version";
import treeKill from "tree-kill";
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const {exec, spawn} = require("child_process"); const {exec, spawn} = require("child_process");
export let frpcProcess = null; export let frpcProcess = null;
const runningCmd = { const runningCmd = {
@ -14,13 +16,6 @@ const runningCmd = {
configPath: null configPath: null
}; };
// const getFrpc = (config: Config) => {
// getVersionById(config.currentVersion, (err, document) => {
// if (!err) {
// }
// });
// };
/** /**
* *
* @param versionId ID * @param versionId ID
@ -78,14 +73,24 @@ log.level = "${config.logLevel}"
log.maxDays = ${config.logMaxDays} log.maxDays = ${config.logMaxDays}
webServer.addr = "127.0.0.1" webServer.addr = "127.0.0.1"
webServer.port = 57400 webServer.port = 57400
transport.tls.enable = ${config.tlsConfigEnable}
${config.tlsConfigEnable ? `
transport.tls.certFile = "${config.tlsConfigCertFile}"
transport.tls.keyFile = "${config.tlsConfigKeyFile}"
transport.tls.trustedCaFile = "${config.tlsConfigTrustedCaFile}"
transport.tls.serverName = "${config.tlsConfigServerName}"
` : ""}
${proxyToml}
${proxyToml.join("")}
`; `;
// const configPath = path.join("frp.toml"); // const configPath = path.join("frp.toml");
const filename = "frp.toml"; const filename = "frp.toml";
const configPath = path.join(app.getPath("userData"), filename)
console.debug("生成配置成功", configPath)
fs.writeFile( fs.writeFile(
path.join(app.getPath("userData"), filename), // 配置文件目录 configPath, // 配置文件目录
toml, // 配置文件内容 toml, // 配置文件内容
{flag: "w"}, {flag: "w"},
err => { err => {
@ -106,6 +111,7 @@ ${proxyToml}
*/ */
const startFrpcProcess = (commandPath: string, configPath: string) => { const startFrpcProcess = (commandPath: string, configPath: string) => {
const command = `${commandPath} -c ${configPath}`; const command = `${commandPath} -c ${configPath}`;
console.info("启动", command)
frpcProcess = spawn(command, { frpcProcess = spawn(command, {
cwd: app.getPath("userData"), cwd: app.getPath("userData"),
shell: true shell: true
@ -113,11 +119,11 @@ const startFrpcProcess = (commandPath: string, configPath: string) => {
runningCmd.commandPath = commandPath; runningCmd.commandPath = commandPath;
runningCmd.configPath = configPath; runningCmd.configPath = configPath;
frpcProcess.stdout.on("data", data => { frpcProcess.stdout.on("data", data => {
console.log(`命令输出: ${data}`); console.debug(`命令输出: ${data}`);
}); });
frpcProcess.stdout.on("error", data => { frpcProcess.stdout.on("error", data => {
console.log(`执行错误: ${data}`); console.log("启动错误", data)
frpcProcess.kill("SIGINT"); stopFrpcProcess()
}); });
}; };
@ -128,7 +134,7 @@ export const reloadFrpcProcess = () => {
if (config) { if (config) {
generateConfig(config, configPath => { generateConfig(config, configPath => {
const command = `${runningCmd.commandPath} reload -c ${configPath}`; const command = `${runningCmd.commandPath} reload -c ${configPath}`;
console.log("重启", command); console.info("重启", command);
exec(command, { exec(command, {
cwd: app.getPath("userData"), cwd: app.getPath("userData"),
shell: true shell: true
@ -140,6 +146,18 @@ export const reloadFrpcProcess = () => {
} }
}; };
export const stopFrpcProcess = () => {
if (frpcProcess) {
treeKill(frpcProcess.pid, (error: Error) => {
if (error) {
console.log("关闭失败", frpcProcess.pid, error)
} else {
frpcProcess = null
}
})
}
}
export const initFrpcApi = () => { export const initFrpcApi = () => {
ipcMain.handle("frpc.running", async (event, args) => { ipcMain.handle("frpc.running", async (event, args) => {
if (!frpcProcess) { if (!frpcProcess) {
@ -184,8 +202,7 @@ export const initFrpcApi = () => {
ipcMain.on("frpc.stop", () => { ipcMain.on("frpc.stop", () => {
if (frpcProcess && !frpcProcess.killed) { if (frpcProcess && !frpcProcess.killed) {
console.log("关闭"); stopFrpcProcess()
frpcProcess.kill();
} }
}); });
}; };

View File

@ -32,6 +32,7 @@ onMounted(() => {
setInterval(() => { setInterval(() => {
ipcRenderer.invoke("frpc.running").then(data => { ipcRenderer.invoke("frpc.running").then(data => {
running.value = data; running.value = data;
console.log('进程状态', data)
}); });
}, 300); }, 300);
@ -97,13 +98,16 @@ onUnmounted(() => {
Frpc {{ running ? "已启动" : "已断开" }} Frpc {{ running ? "已启动" : "已断开" }}
</div> </div>
</transition> </transition>
<el-button <!-- <el-button-->
class="block" <!-- class="block"-->
type="text" <!-- type="text"-->
v-if="running" <!-- v-if="running"-->
@click="$router.replace({ name: 'Logger' })" <!-- @click="$router.replace({ name: 'Logger' })"-->
>查看日志 <!-- >查看日志-->
</el-button> <!-- </el-button>-->
<div class="w-full justify-center text-center">
<el-link v-if="running" type="primary" @click="$router.replace({ name: 'Logger' })">查看日志</el-link>
</div>
<div <div
class="w-full h-8 bg-[#563EA4] rounded flex justify-center items-center text-white font-bold cursor-pointer" class="w-full h-8 bg-[#563EA4] rounded flex justify-center items-center text-white font-bold cursor-pointer"
@click="handleButtonClick" @click="handleButtonClick"