From b6d748b414276630397d044f182f3056ef06b364 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Mon, 15 Jan 2024 10:18:04 +0800 Subject: [PATCH] Revert Use Tauri Http Api --- src-tauri/tauri.conf.json | 4 -- src/hooks/use-clash.ts | 4 +- src/pages/_layout.tsx | 4 +- src/services/api.ts | 108 ++++++++++++++++++-------------------- 4 files changed, 54 insertions(+), 66 deletions(-) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 41b81610..79e2fb79 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -54,10 +54,6 @@ }, "notification": { "all": true - }, - "http": { - "all": true, - "scope": ["http://**", "https://**"] } }, "windows": [], diff --git a/src/hooks/use-clash.ts b/src/hooks/use-clash.ts index 1bd9318a..9804c5fc 100644 --- a/src/hooks/use-clash.ts +++ b/src/hooks/use-clash.ts @@ -1,7 +1,7 @@ import useSWR, { mutate } from "swr"; import { useLockFn } from "ahooks"; import { - refreshClashInfo, + getAxios, getClashConfig, getVersion, updateConfigs, @@ -72,7 +72,7 @@ export const useClashInfo = () => { mutateInfo(); mutate("getClashConfig"); // 刷新接口 - await refreshClashInfo(); + getAxios(true); }; return { diff --git a/src/pages/_layout.tsx b/src/pages/_layout.tsx index e3cb8a36..f49b6a16 100644 --- a/src/pages/_layout.tsx +++ b/src/pages/_layout.tsx @@ -10,7 +10,7 @@ import { alpha, List, Paper, ThemeProvider } from "@mui/material"; import { listen } from "@tauri-apps/api/event"; import { appWindow } from "@tauri-apps/api/window"; import { routers } from "./_routers"; -import { refreshClashInfo } from "@/services/api"; +import { getAxios } from "@/services/api"; import { useVerge } from "@/hooks/use-verge"; import LogoSvg from "@/assets/image/logo.svg?react"; import { BaseErrorBoundary, Notice } from "@/components/base"; @@ -50,7 +50,7 @@ const Layout = () => { listen("verge://refresh-clash-config", async () => { // the clash info may be updated - await refreshClashInfo(); + await getAxios(true); mutate("getProxies"); mutate("getVersion"); mutate("getClashConfig"); diff --git a/src/services/api.ts b/src/services/api.ts index 1941d4d2..f9b0cba2 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -1,30 +1,22 @@ +import axios, { AxiosInstance } from "axios"; import { getClashInfo } from "./cmds"; -import { - fetch as tauriFetch, - HttpVerb, - Body, - Response, -} from "@tauri-apps/api/http"; -let clashInfo: IClashInfo | null; -export const refreshClashInfo = async () => { - clashInfo = await getClashInfo(); - return clashInfo; -}; +let axiosIns: AxiosInstance = null!; + +/// initialize some information +/// enable force update axiosIns +export const getAxios = async (force: boolean = false) => { + if (axiosIns && !force) return axiosIns; -export const fetch = async ( - path: string, - method: HttpVerb, - body?: any -): Promise> => { let server = ""; let secret = ""; try { - const info = clashInfo ?? (await refreshClashInfo()); + const info = await getClashInfo(); if (info?.server) { server = info.server; + // compatible width `external-controller` if (server.startsWith(":")) server = `127.0.0.1${server}`; else if (/^\d+$/.test(server)) server = `127.0.0.1:${server}`; @@ -32,18 +24,19 @@ export const fetch = async ( if (info?.secret) secret = info?.secret; } catch {} - return tauriFetch(`http://${server}${path}`, { - method, + axiosIns = axios.create({ + baseURL: `http://${server}`, headers: secret ? { Authorization: `Bearer ${secret}` } : {}, timeout: 15000, - body: body ? Body.json(body) : undefined, }); + axiosIns.interceptors.response.use((r) => r.data); + return axiosIns; }; /// Get Version export const getVersion = async () => { - const res = await fetch("/version", "GET"); - return res.data as Promise<{ + const instance = await getAxios(); + return instance.get("/version") as Promise<{ premium: boolean; meta?: boolean; version: string; @@ -52,32 +45,33 @@ export const getVersion = async () => { /// Get current base configs export const getClashConfig = async () => { - const res = await fetch("/configs", "GET"); - return res.data as Promise; + const instance = await getAxios(); + return instance.get("/configs") as Promise; }; /// Update current configs export const updateConfigs = async (config: Partial) => { - const res = await fetch("/configs", "PATCH", config); - return res; + const instance = await getAxios(); + return instance.patch("/configs", config); }; /// Update geo data export const updateGeoData = async () => { - const res = await fetch("/configs/geo", "POST"); - return res; + const instance = await getAxios(); + return instance.post("/configs/geo"); }; /// Upgrade clash core export const upgradeCore = async () => { - const res = await fetch("/upgrade", "POST"); - return res; + const instance = await getAxios(); + return instance.post("/upgrade"); }; /// Get current rules export const getRules = async () => { - const res = await fetch("/rules", "GET"); - return res?.data?.rules as IRuleItem[]; + const instance = await getAxios(); + const response = await instance.get("/rules"); + return response?.rules as IRuleItem[]; }; /// Get Proxy delay @@ -86,26 +80,25 @@ export const getProxyDelay = async (name: string, url?: string) => { timeout: 10000, url: url || "http://1.1.1.1", }; - const result = await fetch( + const instance = await getAxios(); + const result = await instance.get( `/proxies/${encodeURIComponent(name)}/delay`, - "GET", { params } ); - return result.data as any as { delay: number }; + return result as any as { delay: number }; }; /// Update the Proxy Choose export const updateProxy = async (group: string, proxy: string) => { - const res = await fetch(`/proxies/${encodeURIComponent(group)}`, "PUT", { - name: proxy, - }); - return res; + const instance = await getAxios(); + return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }); }; // get proxy export const getProxiesInner = async () => { - const res = await fetch("/proxies", "GET"); - return (res?.data?.proxies || {}) as Record; + const instance = await getAxios(); + const response = await instance.get("/proxies"); + return (response?.proxies || {}) as Record; }; /// Get the Proxy information @@ -174,8 +167,10 @@ export const getProxies = async () => { // get proxy providers export const getProviders = async () => { - const res = await fetch("/providers/proxies", "GET"); - const providers = (res.data.providers || {}) as Record; + const instance = await getAxios(); + const response = await instance.get("/providers/proxies"); + + const providers = (response.providers || {}) as Record; return Object.fromEntries( Object.entries(providers).filter(([key, item]) => { @@ -187,34 +182,31 @@ export const getProviders = async () => { // proxy providers health check export const providerHealthCheck = async (name: string) => { - const res = await fetch( - `/providers/proxies/${encodeURIComponent(name)}/healthcheck`, - "GET" + const instance = await getAxios(); + return instance.get( + `/providers/proxies/${encodeURIComponent(name)}/healthcheck` ); - return res; }; export const providerUpdate = async (name: string) => { - const res = await fetch( - `/providers/proxies/${encodeURIComponent(name)}`, - "PUT" - ); - return res; + const instance = await getAxios(); + return instance.put(`/providers/proxies/${encodeURIComponent(name)}`); }; export const getConnections = async () => { - const res = await fetch("/connections", "GET"); - return res.data as any as IConnections; + const instance = await getAxios(); + const result = await instance.get("/connections"); + return result as any as IConnections; }; // Close specific connection export const deleteConnection = async (id: string) => { - const res = await fetch(`/connections/${encodeURIComponent(id)}`, "DELETE"); - return res; + const instance = await getAxios(); + await instance.delete(`/connections/${encodeURIComponent(id)}`); }; // Close all connections export const closeAllConnections = async () => { - const res = await fetch("/connections", "DELETE"); - return res; + const instance = await getAxios(); + await instance.delete(`/connections`); };