feat: use external controller field

This commit is contained in:
GyDi 2021-12-15 01:25:56 +08:00
parent d75fb152c2
commit da41ac5b8b
5 changed files with 81 additions and 19 deletions

View File

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

46
src/services/command.ts Normal file
View File

@ -0,0 +1,46 @@
import { invoke } from "@tauri-apps/api/tauri";
export async function restartSidecar() {
return invoke<void>("restart_sidebar");
}
export interface ClashInfo {
status: string;
controller?: { server?: string; secret?: string };
message?: string;
}
export async function getClashInfo() {
return invoke<ClashInfo | null>("get_clash_info");
}
export async function importProfile(url: string) {
return invoke<string>("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<ProfilesConfig[] | null>("get_profiles")) ?? [];
}
export async function setProfiles(current: number, profile: ProfileItem) {
return invoke<void>("set_profiles", { current, profile });
}

View File

@ -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<ConfigType>;
return (await getAxios()).get("/configs") as Promise<ConfigType>;
}
/// Update current configs
export async function updateConfigs(config: Partial<ConfigType>) {
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<RuleItem[]>;
return (await getAxios()).get("/rules") as Promise<RuleItem[]>;
}
/// 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 || "";

View File

@ -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<ProxyItem, "all"> & {
/// 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<any, any>("/proxies");
const proxies = (response?.proxies ?? {}) as Record<string, ProxyItem>;
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 });
}

View File

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