set external controller at core launch

This commit is contained in:
pompurin404 2024-10-06 23:35:38 +08:00
parent c4047ff57a
commit 3e8755621a
No known key found for this signature in database
4 changed files with 17 additions and 47 deletions

View File

@ -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 setPublicDNSTimer: NodeJS.Timeout | null = null
let recoverDNSTimer: NodeJS.Timeout | null = null let recoverDNSTimer: NodeJS.Timeout | null = null
let child: ChildProcess let child: ChildProcess
@ -87,7 +91,8 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
}) })
} }
} }
child = spawn(corePath, ['-d', mihomoWorkDir()], {
child = spawn(corePath, ['-d', mihomoWorkDir(), ctlParam, mihomoIpcPath], {
detached: detached, detached: detached,
stdio: detached ? 'ignore' : undefined stdio: detached ? 'ignore' : undefined
}) })

View File

@ -6,6 +6,7 @@ import { tray } from '../resolve/tray'
import { calcTraffic } from '../utils/calc' import { calcTraffic } from '../utils/calc'
import { getRuntimeConfig } from './factory' import { getRuntimeConfig } from './factory'
import { floatingWindow } from '../resolve/floatingWindow' import { floatingWindow } from '../resolve/floatingWindow'
import { mihomoIpcPath } from './manager'
let axiosIns: AxiosInstance = null! let axiosIns: AxiosInstance = null!
let mihomoTrafficWs: WebSocket | null = null let mihomoTrafficWs: WebSocket | null = null
@ -18,15 +19,11 @@ let mihomoConnectionsWs: WebSocket | null = null
let connectionsRetry = 10 let connectionsRetry = 10
export const getAxios = async (force: boolean = false): Promise<AxiosInstance> => { export const getAxios = async (force: boolean = false): Promise<AxiosInstance> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock'
} = await getControledMihomoConfig()
if (axiosIns && !force) return axiosIns if (axiosIns && !force) return axiosIns
axiosIns = axios.create({ axiosIns = axios.create({
baseURL: `http://localhost`, baseURL: `http://localhost`,
socketPath: process.platform === 'win32' ? mihomoPipe : mihomoUnix, socketPath: mihomoIpcPath,
timeout: 15000 timeout: 15000
}) })
@ -180,14 +177,7 @@ export const stopMihomoTraffic = (): void => {
} }
const mihomoTraffic = async (): Promise<void> => { const mihomoTraffic = async (): Promise<void> => {
const { mihomoTrafficWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/traffic`)
'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.onmessage = async (e): Promise<void> => { mihomoTrafficWs.onmessage = async (e): Promise<void> => {
const data = e.data as string const data = e.data as string
@ -239,14 +229,7 @@ export const stopMihomoMemory = (): void => {
} }
const mihomoMemory = async (): Promise<void> => { const mihomoMemory = async (): Promise<void> => {
const { mihomoMemoryWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/memory`)
'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.onmessage = (e): void => { mihomoMemoryWs.onmessage = (e): void => {
const data = e.data as string const data = e.data as string
@ -288,15 +271,9 @@ export const stopMihomoLogs = (): void => {
} }
const mihomoLogs = async (): Promise<void> => { const mihomoLogs = async (): Promise<void> => {
const { const { 'log-level': logLevel = 'info' } = await getControledMihomoConfig()
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock',
'log-level': logLevel = 'info'
} = await getControledMihomoConfig()
mihomoLogsWs = new WebSocket( mihomoLogsWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/logs?level=${logLevel}`)
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/logs?level=${logLevel}`
)
mihomoLogsWs.onmessage = (e): void => { mihomoLogsWs.onmessage = (e): void => {
const data = e.data as string const data = e.data as string
@ -338,14 +315,7 @@ export const stopMihomoConnections = (): void => {
} }
const mihomoConnections = async (): Promise<void> => { const mihomoConnections = async (): Promise<void> => {
const { mihomoConnectionsWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/connections`)
'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.onmessage = (e): void => { mihomoConnectionsWs.onmessage = (e): void => {
const data = e.data as string const data = e.data as string

View File

@ -195,16 +195,13 @@ async function migration(): Promise<void> {
await patchAppConfig({ envType: [envType] }) await patchAppConfig({ envType: [envType] })
} }
// use unix socket // use unix socket
if (process.platform !== 'win32' && externalControllerUnix !== '/tmp/mihomo-party.sock') { if (externalControllerUnix) {
await patchControledMihomoConfig({ 'external-controller-unix': '/tmp/mihomo-party.sock' }) await patchControledMihomoConfig({ 'external-controller-unix': undefined })
} }
// use named pipe // use named pipe
if ( if (externalControllerPipe) {
process.platform === 'win32' &&
externalControllerPipe !== '\\\\.\\pipe\\MihomoParty\\mihomo'
) {
await patchControledMihomoConfig({ await patchControledMihomoConfig({
'external-controller-pipe': '\\\\.\\pipe\\MihomoParty\\mihomo' 'external-controller-pipe': undefined
}) })
} }
if (externalController === undefined) { if (externalController === undefined) {

View File

@ -36,8 +36,6 @@ export const defaultConfig: IAppConfig = {
} }
export const defaultControledMihomoConfig: Partial<IMihomoConfig> = { export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'external-controller-pipe': '\\\\.pipe\\MihomoParty\\mihomo',
'external-controller-unix': '/tmp/mihomo-party.sock',
'external-controller': '', 'external-controller': '',
ipv6: true, ipv6: true,
mode: 'rule', mode: 'rule',