fix websocket on macos

This commit is contained in:
pompurin404 2024-09-30 13:42:14 +08:00
parent 7c432a97f6
commit 398fe4935b
No known key found for this signature in database
3 changed files with 13 additions and 15 deletions

View File

@ -5,8 +5,6 @@ import WebSocket from 'ws'
import { tray } from '../resolve/tray'
import { calcTraffic } from '../utils/calc'
import { getRuntimeConfig } from './factory'
import { join } from 'path'
import { mihomoWorkDir } from '../utils/dirs'
let axiosIns: AxiosInstance = null!
let mihomoTrafficWs: WebSocket | null = null
@ -21,13 +19,13 @@ let connectionsRetry = 10
export const getAxios = async (force: boolean = false): Promise<AxiosInstance> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = 'mihomo-party.sock'
'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 : join(mihomoWorkDir(), mihomoUnix),
socketPath: process.platform === 'win32' ? mihomoPipe : mihomoUnix,
timeout: 15000
})
@ -183,11 +181,11 @@ export const stopMihomoTraffic = (): void => {
const mihomoTraffic = async (): Promise<void> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = 'mihomo-party.sock'
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock'
} = await getControledMihomoConfig()
mihomoTrafficWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : join(mihomoWorkDir(), mihomoUnix)}:/traffic`
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/traffic`
)
mihomoTrafficWs.onmessage = async (e): Promise<void> => {
@ -241,11 +239,11 @@ export const stopMihomoMemory = (): void => {
const mihomoMemory = async (): Promise<void> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = 'mihomo-party.sock'
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock'
} = await getControledMihomoConfig()
mihomoMemoryWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : join(mihomoWorkDir(), mihomoUnix)}:/memory`
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/memory`
)
mihomoMemoryWs.onmessage = (e): void => {
@ -290,12 +288,12 @@ export const stopMihomoLogs = (): void => {
const mihomoLogs = async (): Promise<void> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = 'mihomo-party.sock',
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock',
'log-level': logLevel = 'info'
} = await getControledMihomoConfig()
mihomoLogsWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : join(mihomoWorkDir(), mihomoUnix)}:/logs?level=${logLevel}`
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/logs?level=${logLevel}`
)
mihomoLogsWs.onmessage = (e): void => {
@ -340,11 +338,11 @@ export const stopMihomoConnections = (): void => {
const mihomoConnections = async (): Promise<void> => {
const {
'external-controller-pipe': mihomoPipe = '\\\\.\\pipe\\MihomoParty\\mihomo',
'external-controller-unix': mihomoUnix = 'mihomo-party.sock'
'external-controller-unix': mihomoUnix = '/tmp/mihomo-party.sock'
} = await getControledMihomoConfig()
mihomoConnectionsWs = new WebSocket(
`ws+unix:${process.platform === 'win32' ? mihomoPipe : join(mihomoWorkDir(), mihomoUnix)}:/connections`
`ws+unix:${process.platform === 'win32' ? mihomoPipe : mihomoUnix}:/connections`
)
mihomoConnectionsWs.onmessage = (e): void => {

View File

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

View File

@ -37,7 +37,7 @@ export const defaultConfig: IAppConfig = {
export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'external-controller-pipe': '\\\\.pipe\\MihomoParty\\mihomo',
'external-controller-unix': 'mihomo-party.sock',
'external-controller-unix': '/tmp/mihomo-party.sock',
ipv6: true,
mode: 'rule',
'mixed-port': 7890,