Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
set external controller at core launch
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Oct 6, 2024
1 parent 6eb41df commit 349e422
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
7 changes: 6 additions & 1 deletion src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
stdio: detached ? 'ignore' : undefined
})
Expand Down
46 changes: 8 additions & 38 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,15 +19,11 @@ let mihomoConnectionsWs: WebSocket | null = null
let connectionsRetry = 10

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

axiosIns = axios.create({
baseURL: `http://localhost`,
socketPath: process.platform === 'win32' ? mihomoPipe : mihomoUnix,
socketPath: mihomoIpcPath,
timeout: 15000
})

Expand Down Expand Up @@ -180,14 +177,7 @@ export const stopMihomoTraffic = (): void => {
}

const mihomoTraffic = async (): Promise<void> => {
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<void> => {
const data = e.data as string
Expand Down Expand Up @@ -239,14 +229,7 @@ export const stopMihomoMemory = (): void => {
}

const mihomoMemory = async (): Promise<void> => {
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
Expand Down Expand Up @@ -288,15 +271,9 @@ export const stopMihomoLogs = (): void => {
}

const mihomoLogs = async (): Promise<void> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock',
'log-level': logLevel = 'info'
} = await getControledMihomoConfig()

mihomoLogsWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/logs?level=${logLevel}`
)
const { 'log-level': logLevel = 'info' } = await getControledMihomoConfig()

mihomoLogsWs = new WebSocket(`ws+unix:${mihomoIpcPath}:/logs?level=${logLevel}`)

mihomoLogsWs.onmessage = (e): void => {
const data = e.data as string
Expand Down Expand Up @@ -338,14 +315,7 @@ export const stopMihomoConnections = (): void => {
}

const mihomoConnections = async (): Promise<void> => {
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
Expand Down
11 changes: 4 additions & 7 deletions src/main/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,13 @@ async function migration(): Promise<void> {
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) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export const defaultConfig: IAppConfig = {
}

export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'external-controller-pipe': '\\\\.pipe\\MihomoParty\\mihomo',
'external-controller-unix': '/tmp/mihomo-party.sock',
'external-controller': '',
ipv6: true,
mode: 'rule',
Expand Down

0 comments on commit 349e422

Please sign in to comment.