From d02421414fa0fba1f0d6ee2a2b7713276c5eff8d Mon Sep 17 00:00:00 2001 From: qianlongzt <18493471+qianlongzt@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:07:34 +0800 Subject: [PATCH] default listen on 127.0.0.1, listen on demand (#304) --- src/main/resolve/server.ts | 29 ++++++++++++++++++++--------- src/main/sys/sysproxy.ts | 4 +++- src/main/utils/init.ts | 4 +++- src/renderer/src/pages/syspeoxy.tsx | 15 +++++++++++++-- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/main/resolve/server.ts b/src/main/resolve/server.ts index 7cdaa71..ca49e2c 100644 --- a/src/main/resolve/server.ts +++ b/src/main/resolve/server.ts @@ -41,21 +41,32 @@ export function findAvailablePort(startPort: number): Promise { }) } +let pacServer: http.Server + export async function startPacServer(): Promise { + await stopPacServer() + const { sysProxy } = await getAppConfig() + const { mode = 'manual', host: cHost, pacScript } = sysProxy + if (mode !== 'auto') { + return + } + const host = cHost || '127.0.0.1' + let script = pacScript || defaultPacScript + const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() + script = script.replaceAll('%mixed-port%', port.toString()) pacPort = await findAvailablePort(10000) - const server = http + pacServer = http .createServer(async (_req, res) => { - const { - sysProxy: { pacScript } - } = await getAppConfig() - const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() - let script = pacScript || defaultPacScript - script = script.replaceAll('%mixed-port%', port.toString()) res.writeHead(200, { 'Content-Type': 'application/x-ns-proxy-autoconfig' }) res.end(script) }) - .listen(pacPort) - server.unref() + .listen(pacPort, host) +} + +export async function stopPacServer(): Promise { + if (pacServer) { + pacServer.close() + } } export async function startSubStoreFrontendServer(): Promise { diff --git a/src/main/sys/sysproxy.ts b/src/main/sys/sysproxy.ts index 22367f2..38188b7 100644 --- a/src/main/sys/sysproxy.ts +++ b/src/main/sys/sysproxy.ts @@ -1,6 +1,6 @@ import { triggerAutoProxy, triggerManualProxy } from '@mihomo-party/sysproxy' import { getAppConfig, getControledMihomoConfig } from '../config' -import { pacPort } from '../resolve/server' +import { pacPort, startPacServer, stopPacServer } from '../resolve/server' import { promisify } from 'util' import { execFile } from 'child_process' import path from 'path' @@ -63,6 +63,7 @@ export async function triggerSysProxy(enable: boolean): Promise { } async function enableSysProxy(): Promise { + await startPacServer() const { sysProxy } = await getAppConfig() const { mode, host, bypass = defaultBypass } = sysProxy const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() @@ -105,6 +106,7 @@ async function enableSysProxy(): Promise { } async function disableSysProxy(): Promise { + await stopPacServer() const execFilePromise = promisify(execFile) if (process.platform === 'win32') { try { diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index f3fe16f..5c3f8c4 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -239,11 +239,13 @@ export async function init(): Promise { await migration() await initFiles() await cleanup() - await startPacServer() await startSubStoreFrontendServer() await startSubStoreBackendServer() const { sysProxy } = await getAppConfig() try { + if (sysProxy.enable) { + await startPacServer() + } await triggerSysProxy(sysProxy.enable) } catch { // ignore diff --git a/src/renderer/src/pages/syspeoxy.tsx b/src/renderer/src/pages/syspeoxy.tsx index b352b1a..9ca1014 100644 --- a/src/renderer/src/pages/syspeoxy.tsx +++ b/src/renderer/src/pages/syspeoxy.tsx @@ -1,4 +1,4 @@ -import { Button, Input, Tab, Tabs } from '@nextui-org/react' +import { Button, Input, Tab, Tabs, Tooltip } from '@nextui-org/react' import BasePage from '@renderer/components/base/base-page' import SettingCard from '@renderer/components/base/base-setting-card' import SettingItem from '@renderer/components/base/base-setting-item' @@ -9,6 +9,7 @@ import { openUWPTool, triggerSysProxy } from '@renderer/utils/ipc' import { Key, useState } from 'react' import React from 'react' import { MdDeleteForever } from 'react-icons/md' +import { IoIosHelpCircle } from 'react-icons/io' const defaultBypass: string[] = platform === 'linux' @@ -135,7 +136,17 @@ const Sysproxy: React.FC = () => { }} /> - + + + + } + title="代理模式" + divider + >