Skip to content

Commit

Permalink
fix mihomo api
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 5, 2024
1 parent cdb3b8a commit d2e3ce2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@mihomo-party/sysproxy": "^1.0.1",
"@nextui-org/react": "^2.4.6",
"axios": "^1.7.2",
"dayjs": "^1.11.12",
"electron-updater": "^6.2.1",
"framer-motion": "^11.3.19",
"next-themes": "^0.3.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 36 additions & 26 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,82 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =

export async function mihomoVersion(): Promise<IMihomoVersion> {
const instance = await getAxios()
return instance.get('/version').catch((e) => {
return e.response.data
})
return (await instance.get('/version').catch(() => {
return { version: '-' }
})) as IMihomoVersion
}

export const mihomoConfig = async (): Promise<IMihomoConfig> => {
const instance = await getAxios()
return instance.get('/configs').catch((e) => {
return e.response.data
})
return (await instance.get('/configs').catch(() => {
return {}
})) as IMihomoConfig
}

export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<void> => {
const instance = await getAxios()
return instance.patch('/configs', patch).catch((e) => {
return (await instance.patch('/configs', patch).catch((e) => {
return e.response.data
})
})) as Promise<void>
}

export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
const instance = await getAxios()
return instance.get('/connections').catch((e) => {
return e.response.data
})
return (await instance.get('/connections').catch(() => {
return { downloadTotal: 0, uploadTotal: 0, connections: [], memory: 0 }
})) as IMihomoConnectionsInfo
}

export const mihomoCloseConnection = async (id: string): Promise<void> => {
const instance = await getAxios()
return instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
return (await instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
return e.response.data
})
})) as Promise<void>
}

export const mihomoCloseAllConnections = async (): Promise<void> => {
const instance = await getAxios()
return instance.delete('/connections').catch((e) => {
return (await instance.delete('/connections').catch((e) => {
return e.response.data
})
})) as Promise<void>
}

export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
const instance = await getAxios()
return instance.get('/rules').catch((e) => {
return e.response.data
})
return (await instance.get('/rules').catch(() => {
return { rules: [] }
})) as IMihomoRulesInfo
}

export const mihomoProxies = async (): Promise<IMihomoProxies> => {
const instance = await getAxios()
return instance.get('/proxies').catch((e) => {
return e.response.data
})
return (await instance.get('/proxies').catch(() => {
return { proxies: {} }
})) as IMihomoProxies
}

export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
const instance = await getAxios()
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch((e) => {
return e.response.data
})
return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => {
return {
alive: false,
extra: {},
history: [],
id: '',
name: '',
tfo: false,
type: 'Shadowsocks',
udp: false,
xudp: false
}
})) as IMihomoProxy
}

export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMihomoDelay> => {
const appConfig = getAppConfig()
const { delayTestUrl, delayTestTimeout } = appConfig
const instance = await getAxios()
return instance
return (await instance
.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
params: {
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
Expand All @@ -101,7 +111,7 @@ export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMi
})
.catch((e) => {
return e.response.data
})
})) as IMihomoDelay
}

export const startMihomoTraffic = (): void => {
Expand Down

0 comments on commit d2e3ce2

Please sign in to comment.