From 4fa96accd60c21f12c0b51393633e8bc6f612a94 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Mon, 5 Aug 2024 12:43:02 +0800 Subject: [PATCH] fix mihomo api --- package.json | 1 + pnpm-lock.yaml | 8 +++++ src/main/core/mihomoApi.ts | 62 ++++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 79b142e..3887f25 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a9b5dc..63e6215 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: axios: specifier: ^1.7.2 version: 1.7.2 + dayjs: + specifier: ^1.11.12 + version: 1.11.12 electron-updater: specifier: ^6.2.1 version: 6.2.1 @@ -2318,6 +2321,9 @@ packages: resolution: {integrity: sha512-t/Ygsytq+R995EJ5PZlD4Cu56sWa8InXySaViRzw9apusqsOO2bQP+SbYzAhR0pFKoB+43lYy8rWban9JSuXnA==} engines: {node: '>= 0.4'} + dayjs@1.11.12: + resolution: {integrity: sha512-Rt2g+nTbLlDWZTwwrIXjy9MeiZmSDI375FvZs72ngxx8PDC6YXOeR3q5LAuPzjZQxhiWdRKac7RKV+YyQYfYIg==} + debug@4.3.6: resolution: {integrity: sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==} engines: {node: '>=6.0'} @@ -7520,6 +7526,8 @@ snapshots: es-errors: 1.3.0 is-data-view: 1.0.1 + dayjs@1.11.12: {} + debug@4.3.6: dependencies: ms: 2.1.2 diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 0a8fafc..920beb4 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -27,72 +27,82 @@ export const getAxios = async (force: boolean = false): Promise = export async function mihomoVersion(): Promise { 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 => { 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): Promise => { 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 } export const mihomoConnections = async (): Promise => { 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 => { 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 } export const mihomoCloseAllConnections = async (): Promise => { const instance = await getAxios() - return instance.delete('/connections').catch((e) => { + return (await instance.delete('/connections').catch((e) => { return e.response.data - }) + })) as Promise } export const mihomoRules = async (): Promise => { 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 => { 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 => { 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 => { 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', @@ -101,7 +111,7 @@ export const mihomoProxyDelay = async (proxy: string, url?: string): Promise { return e.response.data - }) + })) as IMihomoDelay } export const startMihomoTraffic = (): void => {