Skip to content

Commit

Permalink
feat: use external controller field
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Dec 14, 2021
1 parent d75fb15 commit da41ac5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
31 changes: 23 additions & 8 deletions src/services/base.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions src/services/command.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
12 changes: 6 additions & 6 deletions src/services/common.ts
Original file line number Diff line number Diff line change
@@ -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;
}>;
Expand All @@ -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 {
Expand All @@ -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 || "";
Expand Down
7 changes: 4 additions & 3 deletions src/services/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axiosIns from "./base";
import { getAxios } from "./base";

export interface ProxyItem {
name: string;
Expand All @@ -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"];
Expand Down Expand Up @@ -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 });
}
4 changes: 2 additions & 2 deletions src/services/traffic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import axiosIns from "./base";
import { getAxios } from "./base";

export interface TrafficData {
up: number;
Expand All @@ -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 || "";
Expand Down

0 comments on commit da41ac5

Please sign in to comment.