Skip to content

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 9bc7237
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
6 changes: 5 additions & 1 deletion src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
})
}
}
child = spawn(corePath, ['-d', mihomoWorkDir()], {
const ctlParams =
process.platform === 'win32'
? ['-ext-ctl-pipe', '\\\\.\\pipe\\MihomoParty\\mihomo']
: ['-ext-ctl-unix', '/tmp/mihomo-party.sock']
child = spawn(corePath, ['-d', mihomoWorkDir()].concat(ctlParams), {
detached: detached,
stdio: detached ? 'ignore' : undefined
})
Expand Down
28 changes: 4 additions & 24 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ let logsRetry = 10
let mihomoConnectionsWs: WebSocket | null = null
let connectionsRetry = 10

const mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo'
const mihomoUnix = '/tmp/mihomo-party.sock'

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({
Expand Down Expand Up @@ -180,11 +179,6 @@ 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`
)
Expand Down Expand Up @@ -239,11 +233,6 @@ 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`
)
Expand Down Expand Up @@ -288,11 +277,7 @@ 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()
const { 'log-level': logLevel = 'info' } = await getControledMihomoConfig()

mihomoLogsWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/logs?level=${logLevel}`
Expand Down Expand Up @@ -338,11 +323,6 @@ 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`
)
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 9bc7237

Please sign in to comment.