diff --git a/src/services/base.ts b/src/services/base.ts index 23b2d38cd3..952d36ec5a 100644 --- a/src/services/base.ts +++ b/src/services/base.ts @@ -1,11 +1,26 @@ -import axios from "axios"; +import axios, { AxiosInstance } from "axios"; +import { getClashInfo } from "./command"; -const axiosIns = axios.create({ - baseURL: "http://127.0.0.1:9090", -}); +let axiosIns: AxiosInstance | null = null; -axiosIns.interceptors.response.use((respone) => { - return respone.data; -}); +export async function getAxios() { + if (axiosIns) return axiosIns; -export default axiosIns; + let server = "127.0.0.1:9090"; + let secret = ""; + + try { + const info = await getClashInfo(); + const { server: server_, secret: secret_ } = info?.controller ?? {}; + if (server_) server = server_; + if (secret_) secret = secret_; + } catch {} + + axiosIns = axios.create({ + baseURL: `http://${server}`, + headers: secret ? { Authorization: `Bearer ${secret}` } : {}, + }); + axiosIns.interceptors.response.use((r) => r.data); + + return axiosIns; +} diff --git a/src/services/command.ts b/src/services/command.ts new file mode 100644 index 0000000000..ccc44e217d --- /dev/null +++ b/src/services/command.ts @@ -0,0 +1,46 @@ +import { invoke } from "@tauri-apps/api/tauri"; + +export async function restartSidecar() { + return invoke("restart_sidebar"); +} + +export interface ClashInfo { + status: string; + controller?: { server?: string; secret?: string }; + message?: string; +} + +export async function getClashInfo() { + return invoke("get_clash_info"); +} + +export async function importProfile(url: string) { + return invoke("import_profile", { url }); +} + +export interface ProfileItem { + name?: string; + file?: string; + mode?: string; + url?: string; + selected?: { name?: string; now?: string }[]; + extra?: { + upload: number; + download: number; + total: number; + expire: number; + }; +} + +export interface ProfilesConfig { + current?: number; + items?: ProfileItem[]; +} + +export async function getProfiles() { + return (await invoke("get_profiles")) ?? []; +} + +export async function setProfiles(current: number, profile: ProfileItem) { + return invoke("set_profiles", { current, profile }); +} diff --git a/src/services/common.ts b/src/services/common.ts index 5240f48250..6ab0d3fc7d 100644 --- a/src/services/common.ts +++ b/src/services/common.ts @@ -1,9 +1,9 @@ import axios from "axios"; -import axiosIns from "./base"; +import { getAxios } from "./base"; /// Get Version export async function getVersion() { - return axiosIns.get("/version") as Promise<{ + return (await getAxios()).get("/version") as Promise<{ premium: boolean; version: string; }>; @@ -20,12 +20,12 @@ export interface ConfigType { /// Get current base configs export async function getConfigs() { - return axiosIns.get("/configs") as Promise; + return (await getAxios()).get("/configs") as Promise; } /// Update current configs export async function updateConfigs(config: Partial) { - return axiosIns.patch("/configs", config); + return (await getAxios()).patch("/configs", config); } interface RuleItem { @@ -36,14 +36,14 @@ interface RuleItem { /// Get current rules export async function getRules() { - return axiosIns.get("/rules") as Promise; + return (await getAxios()).get("/rules") as Promise; } /// Get logs stream export async function getLogs(callback: (t: any) => void) { const source = axios.CancelToken.source(); - axiosIns.get("/logs", { + (await getAxios()).get("/logs", { cancelToken: source.token, onDownloadProgress: (progressEvent) => { const data = progressEvent.currentTarget.response || ""; diff --git a/src/services/proxy.ts b/src/services/proxy.ts index c9d6d75243..3dbdae8780 100644 --- a/src/services/proxy.ts +++ b/src/services/proxy.ts @@ -1,4 +1,4 @@ -import axiosIns from "./base"; +import { getAxios } from "./base"; export interface ProxyItem { name: string; @@ -18,7 +18,8 @@ export type ProxyGroupItem = Omit & { /// Get the Proxy infomation export async function getProxyInfo() { - const response = (await axiosIns.get("/proxies")) as any; + const axiosIns = await getAxios(); + const response = await axiosIns.get("/proxies"); const proxies = (response?.proxies ?? {}) as Record; const global = proxies["GLOBAL"]; @@ -49,5 +50,5 @@ export async function getProxyInfo() { /// Update the Proxy Choose export async function updateProxy(group: string, proxy: string) { - return axiosIns.put(`/proxies/${group}`, { name: proxy }); + return (await getAxios()).put(`/proxies/${group}`, { name: proxy }); } diff --git a/src/services/traffic.ts b/src/services/traffic.ts index 09e0e348d1..a4c760feb5 100644 --- a/src/services/traffic.ts +++ b/src/services/traffic.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import axiosIns from "./base"; +import { getAxios } from "./base"; export interface TrafficData { up: number; @@ -10,7 +10,7 @@ export interface TrafficData { export async function getTraffic(callback: (data: TrafficData) => void) { const source = axios.CancelToken.source(); - axiosIns.get("/traffic", { + (await getAxios()).get("/traffic", { cancelToken: source.token, onDownloadProgress: (progressEvent) => { const data = progressEvent.currentTarget.response || "";