From 62151d613e9602eea83b45812ceb3b466438b9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=98=89=E4=BC=9F?= <8473136@qq.com> Date: Wed, 26 Feb 2025 15:58:48 +0800 Subject: [PATCH] :building_construction: refactor proxy handling to improve configuration generation and enhance visitors model management in ServerService --- electron/core/GlobalConstant.ts | 12 ---- electron/service/ServerService.ts | 99 ++++++++++++++++++++++++------- src/views/proxy/index.vue | 28 +++++++-- 3 files changed, 102 insertions(+), 37 deletions(-) diff --git a/electron/core/GlobalConstant.ts b/electron/core/GlobalConstant.ts index 4ed4ae8..ffde657 100644 --- a/electron/core/GlobalConstant.ts +++ b/electron/core/GlobalConstant.ts @@ -1,4 +1,3 @@ -import { app } from "electron"; class GlobalConstant { public static ZIP_EXT = ".zip"; @@ -20,17 +19,6 @@ class GlobalConstant { }; public static FRPC_PROCESS_STATUS_CHECK_INTERVAL = 3000; - // public static APP_DATA_PATH = app.getPath("userData"); - - // public static DOWNLOAD_STORAGE_PATH = path.join( - // GlobalConstant.APP_DATA_PATH, - // SecureUtils.calculateMD5("download") - // ); - // - // public static VERSION_STORAGE_PATH = path.join( - // GlobalConstant.APP_DATA_PATH, - // SecureUtils.calculateMD5("frpc") - // ); } export default GlobalConstant; diff --git a/electron/service/ServerService.ts b/electron/service/ServerService.ts index a490e23..f6b0ab1 100644 --- a/electron/service/ServerService.ts +++ b/electron/service/ServerService.ts @@ -48,23 +48,63 @@ class ServerService extends BaseService { }); } + private isRagePort(proxy: FrpcProxy) { + return ( + ["tcp", "udp"].indexOf(proxy.type) >= 0 && + (String(proxy.localPort).indexOf("-") !== -1 || + String(proxy.localPort).indexOf(",") !== -1) + ); + } + + private isVisitors(proxy: FrpcProxy) { + return ( + ["stcp", "sudp", "xtcp"].indexOf(proxy.type) >= 0 && + proxy.visitorsModel === "visitors" + ); + } + + private isEnableProxy(proxy: FrpcProxy) { + return proxy.status === 1; + } + async genTomlConfig(outputPath: string) { if (!outputPath) { return; } const server = await this.getServerConfig(); const proxies = await this._proxyDao.findAll(); + + const enabledRangePortProxies = proxies + .filter(f => this.isEnableProxy(f)) + .filter(f => !this.isVisitors(f)) + .filter(f => this.isRagePort(f)) + .map(proxy => { + return ` +{{- range $_, $v := parseNumberRangePair "${proxy.localPort}" "${proxy.remotePort}" }} +[[proxies]] + +type = "${proxy.type}" +name = "${proxy.name}-{{ $v.First }}" +localPort = {{ $v.First }} +remotePort = {{ $v.Second }} +{{- end }} +`; + }); + const enabledProxies = proxies - .filter(f => f.status === 1) - .filter(f => f.visitorsModel !== "visitors") + .filter(f => this.isEnableProxy(f)) + .filter(f => !this.isVisitors(f)) + .filter(f => !this.isRagePort(f)) .map(proxy => { if (proxy.type === "tcp" || proxy.type === "udp") { + const localPort = parseInt(proxy.localPort); + const remotePort = parseInt(proxy.remotePort); return { name: proxy.name, type: proxy.type, localIP: proxy.localIP, - localPort: parseInt(proxy.localPort), - remotePort: parseInt(proxy.remotePort) + localPort: localPort, + remotePort: remotePort }; } else if (proxy.type === "http" || proxy.type === "https") { const { _id, status, ...frpProxyConfig } = proxy; @@ -85,21 +125,32 @@ class ServerService extends BaseService { }); const enableVisitors = proxies - .filter(f => f.status === 1) - .filter(f => f.visitorsModel === "visitors") + .filter(f => this.isEnableProxy(f)) + .filter(f => this.isVisitors(f)) .map(proxy => { - return { - name: proxy.name, - type: proxy.type, - // serverUser: proxy.serverUser, - serverName: proxy.serverName, - secretKey: proxy.secretKey, - bindAddr: proxy.bindAddr, - bindPort: proxy.bindPort, - // keepTunnelOpen: proxy.keepTunnelOpen - // maxRetriesAnHour: proxy.maxRetriesAnHour, - // minRetryInterval: proxy.minRetryInterval, - }; + if (proxy.type === "xtcp") { + return { + name: proxy.name, + type: proxy.type, + // serverUser: proxy.serverUser, + serverName: proxy.serverName, + secretKey: proxy.secretKey, + bindAddr: proxy.bindAddr, + bindPort: proxy.bindPort, + keepTunnelOpen: proxy.keepTunnelOpen, + fallbackTo: proxy.fallbackTo, + fallbackTimeoutMs: proxy.fallbackTimeoutMs + }; + } else { + return { + name: proxy.name, + type: proxy.type, + serverName: proxy.serverName, + secretKey: proxy.secretKey, + bindAddr: proxy.bindAddr, + bindPort: proxy.bindPort + }; + } }); const { frpcVersion, _id, system, ...commonConfig } = server; @@ -107,10 +158,16 @@ class ServerService extends BaseService { frpcConfig.log.to = PathUtils.getFrpcLogFilePath(); frpcConfig.loginFailExit = GlobalConstant.FRPC_LOGIN_FAIL_EXIT; frpcConfig.webServer.addr = GlobalConstant.LOCAL_IP; - const toml = TOML.stringify({ + + let toml = TOML.stringify({ ...frpcConfig, - proxies: enabledProxies, - visitors: enableVisitors + ...(enabledProxies.length > 0 ? { proxies: enabledProxies } : {}), + ...(enableVisitors.length > 0 ? { visitors: enableVisitors } : {}) + }); + + enabledRangePortProxies.forEach(f => { + toml += ` +${f}`; }); fs.writeFileSync(outputPath, toml, { flag: "w" }); diff --git a/src/views/proxy/index.vue b/src/views/proxy/index.vue index 6346c06..972a1cb 100644 --- a/src/views/proxy/index.vue +++ b/src/views/proxy/index.vue @@ -132,7 +132,9 @@ const editFormRules = reactive({ trigger: "blur" } ], - visitorsModel: [{ required: true, message: "请选择stcp模式", trigger: "blur" }], + visitorsModel: [ + { required: true, message: "请选择stcp模式", trigger: "blur" } + ], secretKey: [ { required: true, message: "请输入stcp共享密钥", trigger: "blur" } ], @@ -582,6 +584,16 @@ onMounted(() => { // }); }); +const handleProxyTypeChange = e => { + if (e === "http" || e === "https" || e === "tcp" || e === "udp") { + editForm.value.visitorsModel = ""; + } else { + if (editForm.value.visitorsModel === "") { + editForm.value.visitorsModel = "visitorsProvider"; + } + } +}; + onUnmounted(() => { removeRouterListeners(ipcRouters.PROXY.createProxy); removeRouterListeners(ipcRouters.PROXY.modifyProxy); @@ -822,7 +834,10 @@ onUnmounted(() => { - + {