From c749a2d84aefbace1795da453526df63692acbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=98=89=E4=BC=9F?= <8473136@qq.com> Date: Tue, 28 Nov 2023 17:49:08 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=9A=84=E9=85=8D=E7=BD=AE=E3=80=81=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- electron/api/frpc.ts | 244 ++++++++++++++++++------------------ electron/storage/config.ts | 2 + src/views/config/index.vue | 248 ++++++++++++++++++++++--------------- 4 files changed, 270 insertions(+), 226 deletions(-) diff --git a/README.md b/README.md index 4021907..261fcbb 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,4 @@ [issues-shield]: https://img.shields.io/github/issues/luckjiawei/frpc-desktop.svg?style=for-the-badge [issues-url]: https://github.com/luckjiawei/frpc-desktop/issues [license-shield]: https://img.shields.io/github/license/luckjiawei/frpc-desktop.svg?style=for-the-badge -[license-url]: https://github.com/luckjiawei/frpc-desktop/blob/master/LICENSE.txt +[license-url]: https://github.com/luckjiawei/frpc-desktop/blob/master/LICENSE diff --git a/electron/api/frpc.ts b/electron/api/frpc.ts index 7dabac8..00f4354 100644 --- a/electron/api/frpc.ts +++ b/electron/api/frpc.ts @@ -1,17 +1,17 @@ -import { app, ipcMain } from "electron"; -import { Config, getConfig } from "../storage/config"; -import { listProxy } from "../storage/proxy"; -import { getVersionById } from "../storage/version"; +import {app, ipcMain} from "electron"; +import {Config, getConfig} from "../storage/config"; +import {listProxy} from "../storage/proxy"; +import {getVersionById} from "../storage/version"; const fs = require("fs"); const path = require("path"); -const { exec, spawn } = require("child_process"); +const {exec, spawn} = require("child_process"); export let frpcProcess = null; const runningCmd = { - commandPath: null, - configPath: null + commandPath: null, + configPath: null }; // const getFrpc = (config: Config) => { @@ -27,75 +27,75 @@ const runningCmd = { * @param callback */ const getFrpcVersionWorkerPath = ( - versionId: string, - callback: (workerPath: string) => void + versionId: string, + callback: (workerPath: string) => void ) => { - getVersionById(versionId, (err2, version) => { - if (!err2) { - callback(version["frpcVersionPath"]); - } - }); + getVersionById(versionId, (err2, version) => { + if (!err2) { + callback(version["frpcVersionPath"]); + } + }); }; /** * 生成配置文件 */ export const generateConfig = ( - config: Config, - callback: (configPath: string) => void + config: Config, + callback: (configPath: string) => void ) => { - listProxy((err3, proxys) => { - if (!err3) { - const proxyToml = proxys.map(m => { - let toml = ` + listProxy((err3, proxys) => { + if (!err3) { + const proxyToml = proxys.map(m => { + let toml = ` [[proxies]] name = "${m.name}" type = "${m.type}" localIP = "${m.localIp}" localPort = ${m.localPort} `; - switch (m.type) { - case "tcp": - toml += `remotePort = ${m.remotePort}`; - break; - case "http": - case "https": - toml += `customDomains=[${m.customDomains.map(m => `"${m}"`)}]`; - break; - default: - break; - } + switch (m.type) { + case "tcp": + toml += `remotePort = ${m.remotePort}`; + break; + case "http": + case "https": + toml += `customDomains=[${m.customDomains.map(m => `"${m}"`)}]`; + break; + default: + break; + } - return toml; - }); - let toml = ` + return toml; + }); + let toml = ` serverAddr = "${config.serverAddr}" serverPort = ${config.serverPort} auth.method = "${config.authMethod}" auth.token = "${config.authToken}" log.to = "frpc.log" -log.level = "debug" -log.maxDays = 3 +log.level = "${config.logLevel}" +log.maxDays = ${config.logMaxDays} webServer.addr = "127.0.0.1" webServer.port = 57400 ${proxyToml} `; - // const configPath = path.join("frp.toml"); - const filename = "frp.toml"; - fs.writeFile( - path.join(app.getPath("userData"), filename), // 配置文件目录 - toml, // 配置文件内容 - { flag: "w" }, - err => { - if (!err) { - callback(filename); - } + // const configPath = path.join("frp.toml"); + const filename = "frp.toml"; + fs.writeFile( + path.join(app.getPath("userData"), filename), // 配置文件目录 + toml, // 配置文件内容 + {flag: "w"}, + err => { + if (!err) { + callback(filename); + } + } + ); } - ); - } - }); + }); }; /** @@ -105,87 +105,87 @@ ${proxyToml} * @param configPath */ const startFrpcProcess = (commandPath: string, configPath: string) => { - const command = `${commandPath} -c ${configPath}`; - frpcProcess = spawn(command, { - cwd: app.getPath("userData"), - shell: true - }); - runningCmd.commandPath = commandPath; - runningCmd.configPath = configPath; - frpcProcess.stdout.on("data", data => { - console.log(`命令输出: ${data}`); - }); - frpcProcess.stdout.on("error", data => { - console.log(`执行错误: ${data}`); - frpcProcess.kill("SIGINT"); - }); + const command = `${commandPath} -c ${configPath}`; + frpcProcess = spawn(command, { + cwd: app.getPath("userData"), + shell: true + }); + runningCmd.commandPath = commandPath; + runningCmd.configPath = configPath; + frpcProcess.stdout.on("data", data => { + console.log(`命令输出: ${data}`); + }); + frpcProcess.stdout.on("error", data => { + console.log(`执行错误: ${data}`); + frpcProcess.kill("SIGINT"); + }); }; export const reloadFrpcProcess = () => { - if (frpcProcess && !frpcProcess.killed) { - getConfig((err1, config) => { - if (!err1) { - if (config) { - generateConfig(config, configPath => { - const command = `${runningCmd.commandPath} reload -c ${configPath}`; - console.log("重启", command); - exec(command, { - cwd: app.getPath("userData"), - shell: true - }); - }); - } - } - }); - } + if (frpcProcess && !frpcProcess.killed) { + getConfig((err1, config) => { + if (!err1) { + if (config) { + generateConfig(config, configPath => { + const command = `${runningCmd.commandPath} reload -c ${configPath}`; + console.log("重启", command); + exec(command, { + cwd: app.getPath("userData"), + shell: true + }); + }); + } + } + }); + } }; export const initFrpcApi = () => { - ipcMain.handle("frpc.running", async (event, args) => { - if (!frpcProcess) { - return false; - } else { - return !frpcProcess.killed; - } - }); - - ipcMain.on("frpc.start", async (event, args) => { - getConfig((err1, config) => { - if (!err1) { - if (config) { - getFrpcVersionWorkerPath( - config.currentVersion, - (frpcVersionPath: string) => { - generateConfig(config, configPath => { - const platform = process.platform; - if (platform === 'win32') { - startFrpcProcess( - path.join(frpcVersionPath, "frpc.exe"), - configPath - ); - }else { - startFrpcProcess( - path.join(frpcVersionPath, "frpc"), - configPath - ); - } - }); - } - ); + ipcMain.handle("frpc.running", async (event, args) => { + if (!frpcProcess) { + return false; } else { - event.reply( - "Home.frpc.start.error.hook", - "请先前往设置页面,修改配置后再启动" - ); + return !frpcProcess.killed; } - } }); - }); - ipcMain.on("frpc.stop", () => { - if (frpcProcess && !frpcProcess.killed) { - console.log("关闭"); - frpcProcess.kill(); - } - }); + ipcMain.on("frpc.start", async (event, args) => { + getConfig((err1, config) => { + if (!err1) { + if (config) { + getFrpcVersionWorkerPath( + config.currentVersion, + (frpcVersionPath: string) => { + generateConfig(config, configPath => { + const platform = process.platform; + if (platform === 'win32') { + startFrpcProcess( + path.join(frpcVersionPath, "frpc.exe"), + configPath + ); + } else { + startFrpcProcess( + path.join(frpcVersionPath, "frpc"), + configPath + ); + } + }); + } + ); + } else { + event.reply( + "Home.frpc.start.error.hook", + "请先前往设置页面,修改配置后再启动" + ); + } + } + }); + }); + + ipcMain.on("frpc.stop", () => { + if (frpcProcess && !frpcProcess.killed) { + console.log("关闭"); + frpcProcess.kill(); + } + }); }; diff --git a/electron/storage/config.ts b/electron/storage/config.ts index 625e427..44e5761 100644 --- a/electron/storage/config.ts +++ b/electron/storage/config.ts @@ -13,6 +13,8 @@ export type Config = { serverPort: number; authMethod: string; authToken: string; + logLevel: string; + logMaxDays: number; }; /** diff --git a/src/views/config/index.vue b/src/views/config/index.vue index 8f4284c..00b636e 100644 --- a/src/views/config/index.vue +++ b/src/views/config/index.vue @@ -1,11 +1,11 @@ - +