fix mihomo api

This commit is contained in:
pompurin404 2024-08-05 12:43:02 +08:00
parent e9df76f1e6
commit 4fa96accd6
No known key found for this signature in database
3 changed files with 45 additions and 26 deletions

View File

@ -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",

View File

@ -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

View File

@ -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',
@ -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 => {