diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index fe00308..ba3e8d1 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -45,6 +45,10 @@ chokidar.watch(path.join(mihomoCoreDir(), 'meta-update'), {}).on('unlinkDir', as } }) +export const mihomoIpcPath = + process.platform === 'win32' ? '\\\\.\\pipe\\MihomoParty\\mihomo' : '/tmp/mihomo-party.sock' +const ctlParam = process.platform === 'win32' ? '-ext-ctl-pipe' : '-ext-ctl-unix' + let setPublicDNSTimer: NodeJS.Timeout | null = null let recoverDNSTimer: NodeJS.Timeout | null = null let child: ChildProcess @@ -87,7 +91,8 @@ export async function startCore(detached = false): Promise[]> { }) } } - child = spawn(corePath, ['-d', mihomoWorkDir()], { + + child = spawn(corePath, ['-d', mihomoWorkDir(), ctlParam, mihomoIpcPath], { detached: detached, stdio: detached ? 'ignore' : undefined }) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 096158d..f53c95c 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -6,6 +6,7 @@ import { tray } from '../resolve/tray' import { calcTraffic } from '../utils/calc' import { getRuntimeConfig } from './factory' import { floatingWindow } from '../resolve/floatingWindow' +import { mihomoIpcPath } from './manager' let axiosIns: AxiosInstance = null! let mihomoTrafficWs: WebSocket | null = null @@ -18,15 +19,11 @@ let mihomoConnectionsWs: WebSocket | null = null let connectionsRetry = 10 export const getAxios = async (force: boolean = false): Promise => { - const { - 'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo', - 'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock' - } = await getControledMihomoConfig() if (axiosIns && !force) return axiosIns axiosIns = axios.create({ baseURL: `http://localhost`, - socketPath: process.platform === 'win32' ? mihomoPipe : mihomoUnix, + socketPath: mihomoIpcPath, timeout: 15000 }) @@ -180,14 +177,7 @@ export const stopMihomoTraffic = (): void => { } const mihomoTraffic = async (): Promise => { - const { - 'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo', - 'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock' - } = await getControledMihomoConfig() - - mihomoTrafficWs = new WebSocket( - `ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/traffic` - ) + mihomoTrafficWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/traffic`) mihomoTrafficWs.onmessage = async (e): Promise => { const data = e.data as string @@ -239,14 +229,7 @@ export const stopMihomoMemory = (): void => { } const mihomoMemory = async (): Promise => { - const { - 'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo', - 'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock' - } = await getControledMihomoConfig() - - mihomoMemoryWs = new WebSocket( - `ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/memory` - ) + mihomoMemoryWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/memory`) mihomoMemoryWs.onmessage = (e): void => { const data = e.data as string @@ -288,15 +271,9 @@ export const stopMihomoLogs = (): void => { } const mihomoLogs = async (): Promise => { - const { - 'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo', - 'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock', - 'log-level': logLevel = 'info' - } = await getControledMihomoConfig() + const { 'log-level': logLevel = 'info' } = await getControledMihomoConfig() - mihomoLogsWs = new WebSocket( - `ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/logs?level=${logLevel}` - ) + mihomoLogsWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/logs?level=${logLevel}`) mihomoLogsWs.onmessage = (e): void => { const data = e.data as string @@ -338,14 +315,7 @@ export const stopMihomoConnections = (): void => { } const mihomoConnections = async (): Promise => { - const { - 'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo', - 'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock' - } = await getControledMihomoConfig() - - mihomoConnectionsWs = new WebSocket( - `ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/connections` - ) + mihomoConnectionsWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/connections`) mihomoConnectionsWs.onmessage = (e): void => { const data = e.data as string diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index 8d15be9..f1bc548 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -195,16 +195,13 @@ async function migration(): Promise { await patchAppConfig({ envType: [envType] }) } // use unix socket - if (process.platform !== 'win32' && externalControllerUnix !== '/tmp/mihomo-party.sock') { - await patchControledMihomoConfig({ 'external-controller-unix': '/tmp/mihomo-party.sock' }) + if (externalControllerUnix) { + await patchControledMihomoConfig({ 'external-controller-unix': undefined }) } // use named pipe - if ( - process.platform === 'win32' && - externalControllerPipe !== '\\\\.\\pipe\\MihomoParty\\mihomo' - ) { + if (externalControllerPipe) { await patchControledMihomoConfig({ - 'external-controller-pipe': '\\\\.\\pipe\\MihomoParty\\mihomo' + 'external-controller-pipe': undefined }) } if (externalController === undefined) { diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index a5097fb..44d17be 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -36,8 +36,6 @@ export const defaultConfig: IAppConfig = { } export const defaultControledMihomoConfig: Partial = { - 'external-controller-pipe': '\\\\.pipe\\MihomoParty\\mihomo', - 'external-controller-unix': '/tmp/mihomo-party.sock', 'external-controller': '', ipv6: true, mode: 'rule',