feat: use external controller field
This commit is contained in:
parent
d75fb152c2
commit
da41ac5b8b
@ -1,11 +1,26 @@
|
|||||||
import axios from "axios";
|
import axios, { AxiosInstance } from "axios";
|
||||||
|
import { getClashInfo } from "./command";
|
||||||
|
|
||||||
const axiosIns = axios.create({
|
let axiosIns: AxiosInstance | null = null;
|
||||||
baseURL: "http://127.0.0.1:9090",
|
|
||||||
});
|
|
||||||
|
|
||||||
axiosIns.interceptors.response.use((respone) => {
|
export async function getAxios() {
|
||||||
return respone.data;
|
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
46
src/services/command.ts
Normal 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 });
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import axiosIns from "./base";
|
import { getAxios } from "./base";
|
||||||
|
|
||||||
/// Get Version
|
/// Get Version
|
||||||
export async function getVersion() {
|
export async function getVersion() {
|
||||||
return axiosIns.get("/version") as Promise<{
|
return (await getAxios()).get("/version") as Promise<{
|
||||||
premium: boolean;
|
premium: boolean;
|
||||||
version: string;
|
version: string;
|
||||||
}>;
|
}>;
|
||||||
@ -20,12 +20,12 @@ export interface ConfigType {
|
|||||||
|
|
||||||
/// Get current base configs
|
/// Get current base configs
|
||||||
export async function getConfigs() {
|
export async function getConfigs() {
|
||||||
return axiosIns.get("/configs") as Promise<ConfigType>;
|
return (await getAxios()).get("/configs") as Promise<ConfigType>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update current configs
|
/// Update current configs
|
||||||
export async function updateConfigs(config: Partial<ConfigType>) {
|
export async function updateConfigs(config: Partial<ConfigType>) {
|
||||||
return axiosIns.patch("/configs", config);
|
return (await getAxios()).patch("/configs", config);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RuleItem {
|
interface RuleItem {
|
||||||
@ -36,14 +36,14 @@ interface RuleItem {
|
|||||||
|
|
||||||
/// Get current rules
|
/// Get current rules
|
||||||
export async function getRules() {
|
export async function getRules() {
|
||||||
return axiosIns.get("/rules") as Promise<RuleItem[]>;
|
return (await getAxios()).get("/rules") as Promise<RuleItem[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get logs stream
|
/// Get logs stream
|
||||||
export async function getLogs(callback: (t: any) => void) {
|
export async function getLogs(callback: (t: any) => void) {
|
||||||
const source = axios.CancelToken.source();
|
const source = axios.CancelToken.source();
|
||||||
|
|
||||||
axiosIns.get("/logs", {
|
(await getAxios()).get("/logs", {
|
||||||
cancelToken: source.token,
|
cancelToken: source.token,
|
||||||
onDownloadProgress: (progressEvent) => {
|
onDownloadProgress: (progressEvent) => {
|
||||||
const data = progressEvent.currentTarget.response || "";
|
const data = progressEvent.currentTarget.response || "";
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import axiosIns from "./base";
|
import { getAxios } from "./base";
|
||||||
|
|
||||||
export interface ProxyItem {
|
export interface ProxyItem {
|
||||||
name: string;
|
name: string;
|
||||||
@ -18,7 +18,8 @@ export type ProxyGroupItem = Omit<ProxyItem, "all"> & {
|
|||||||
|
|
||||||
/// Get the Proxy infomation
|
/// Get the Proxy infomation
|
||||||
export async function getProxyInfo() {
|
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 proxies = (response?.proxies ?? {}) as Record<string, ProxyItem>;
|
||||||
|
|
||||||
const global = proxies["GLOBAL"];
|
const global = proxies["GLOBAL"];
|
||||||
@ -49,5 +50,5 @@ export async function getProxyInfo() {
|
|||||||
|
|
||||||
/// Update the Proxy Choose
|
/// Update the Proxy Choose
|
||||||
export async function updateProxy(group: string, proxy: string) {
|
export async function updateProxy(group: string, proxy: string) {
|
||||||
return axiosIns.put(`/proxies/${group}`, { name: proxy });
|
return (await getAxios()).put(`/proxies/${group}`, { name: proxy });
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import axiosIns from "./base";
|
import { getAxios } from "./base";
|
||||||
|
|
||||||
export interface TrafficData {
|
export interface TrafficData {
|
||||||
up: number;
|
up: number;
|
||||||
@ -10,7 +10,7 @@ export interface TrafficData {
|
|||||||
export async function getTraffic(callback: (data: TrafficData) => void) {
|
export async function getTraffic(callback: (data: TrafficData) => void) {
|
||||||
const source = axios.CancelToken.source();
|
const source = axios.CancelToken.source();
|
||||||
|
|
||||||
axiosIns.get("/traffic", {
|
(await getAxios()).get("/traffic", {
|
||||||
cancelToken: source.token,
|
cancelToken: source.token,
|
||||||
onDownloadProgress: (progressEvent) => {
|
onDownloadProgress: (progressEvent) => {
|
||||||
const data = progressEvent.currentTarget.response || "";
|
const data = progressEvent.currentTarget.response || "";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user