Files
clash-proxy/src/services/api.ts

194 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-12-25 22:33:29 +08:00
import axios, { AxiosInstance } from "axios";
2021-12-25 23:20:59 +08:00
import { getClashInfo } from "./cmds";
2021-12-25 22:33:29 +08:00
let axiosIns: AxiosInstance = null!;
2022-09-24 18:48:11 +08:00
/// initialize some information
/// enable force update axiosIns
2022-12-15 12:22:20 +08:00
export const getAxios = async (force: boolean = false) => {
if (axiosIns && !force) return axiosIns;
2021-12-25 23:20:59 +08:00
2022-12-15 12:22:20 +08:00
let server = "";
let secret = "";
2021-12-25 23:20:59 +08:00
try {
const info = await getClashInfo();
2022-03-10 02:19:06 +08:00
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}`;
}
2022-01-07 23:29:20 +08:00
if (info?.secret) secret = info?.secret;
2021-12-25 23:20:59 +08:00
} catch {}
2021-12-25 22:33:29 +08:00
axiosIns = axios.create({
baseURL: `http://${server}`,
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
2023-03-16 17:03:12 +08:00
timeout: 15000,
2021-12-25 22:33:29 +08:00
});
axiosIns.interceptors.response.use((r) => r.data);
2021-12-25 23:20:59 +08:00
return axiosIns;
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
/// Get Version
2022-12-15 12:22:20 +08:00
export const getVersion = async () => {
2021-12-25 23:20:59 +08:00
const instance = await getAxios();
return instance.get("/version") as Promise<{
2021-12-25 22:33:29 +08:00
premium: boolean;
2022-11-23 17:44:40 +08:00
meta?: boolean;
2021-12-25 22:33:29 +08:00
version: string;
}>;
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
/// Get current base configs
2022-12-15 12:22:20 +08:00
export const getClashConfig = async () => {
2021-12-25 23:20:59 +08:00
const instance = await getAxios();
2022-11-19 17:22:29 +08:00
return instance.get("/configs") as Promise<IConfigData>;
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
/// Update current configs
2022-12-15 12:22:20 +08:00
export const updateConfigs = async (config: Partial<IConfigData>) => {
2021-12-25 23:20:59 +08:00
const instance = await getAxios();
return instance.patch("/configs", config);
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
/// Get current rules
2022-12-15 12:22:20 +08:00
export const getRules = async () => {
2021-12-25 23:20:59 +08:00
const instance = await getAxios();
2022-09-18 23:19:02 +08:00
const response = await instance.get<any, any>("/rules");
2022-11-19 17:22:29 +08:00
return response?.rules as IRuleItem[];
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
2022-02-16 02:22:01 +08:00
/// Get Proxy delay
2022-12-15 12:22:20 +08:00
export const getProxyDelay = async (name: string, url?: string) => {
2022-02-16 02:22:01 +08:00
const params = {
timeout: 10000,
url: url || "http://1.1.1.1",
2022-02-16 02:22:01 +08:00
};
const instance = await getAxios();
2022-12-15 12:22:20 +08:00
const result = await instance.get(
`/proxies/${encodeURIComponent(name)}/delay`,
{ params }
);
return result as any as { delay: number };
};
2022-02-16 02:22:01 +08:00
2021-12-25 22:33:29 +08:00
/// Update the Proxy Choose
2022-12-15 12:22:20 +08:00
export const updateProxy = async (group: string, proxy: string) => {
2021-12-25 23:20:59 +08:00
const instance = await getAxios();
2022-02-16 02:22:01 +08:00
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy });
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
// get proxy
2022-12-15 12:22:20 +08:00
export const getProxiesInner = async () => {
2022-10-11 22:51:18 +08:00
const instance = await getAxios();
const response = await instance.get<any, any>("/proxies");
2022-11-19 17:22:29 +08:00
return (response?.proxies || {}) as Record<string, IProxyItem>;
2022-12-15 12:22:20 +08:00
};
2022-09-24 18:48:11 +08:00
/// Get the Proxy information
2022-12-15 12:22:20 +08:00
export const getProxies = async () => {
const [proxyRecord, providerRecord] = await Promise.all([
getProxiesInner(),
getProviders(),
]);
// provider name map
const providerMap = Object.fromEntries(
Object.entries(providerRecord).flatMap(([provider, item]) =>
item.proxies.map((p) => [p.name, { ...p, provider }])
)
);
2021-12-25 22:33:29 +08:00
// compatible with proxy-providers
const generateItem = (name: string) => {
if (proxyRecord[name]) return proxyRecord[name];
if (providerMap[name]) return providerMap[name];
return { name, type: "unknown", udp: false, history: [] };
};
const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord;
2022-11-19 17:22:29 +08:00
let groups: IProxyGroupItem[] = [];
if (global?.all) {
groups = global.all
.filter((name) => proxyRecord[name]?.all)
.map((name) => proxyRecord[name])
2021-12-25 22:33:29 +08:00
.map((each) => ({
...each,
all: each.all!.map((item) => generateItem(item)),
2021-12-25 22:33:29 +08:00
}));
} else {
groups = Object.values(proxyRecord)
2021-12-25 22:33:29 +08:00
.filter((each) => each.name !== "GLOBAL" && each.all)
.map((each) => ({
...each,
all: each.all!.map((item) => generateItem(item)),
}))
.sort((a, b) => b.name.localeCompare(a.name));
2021-12-25 22:33:29 +08:00
}
2022-02-26 17:39:08 +08:00
const proxies = [direct, reject].concat(
Object.values(proxyRecord).filter(
2022-02-26 17:39:08 +08:00
(p) => !p.all?.length && p.name !== "DIRECT" && p.name !== "REJECT"
)
);
2022-11-19 17:22:29 +08:00
const _global: IProxyGroupItem = {
...global,
all: global?.all?.map((item) => generateItem(item)) || [],
};
return { global: _global, direct, groups, records: proxyRecord, proxies };
2022-12-15 12:22:20 +08:00
};
// get proxy providers
2022-12-15 12:22:20 +08:00
export const getProviders = async () => {
2022-10-11 22:51:18 +08:00
const instance = await getAxios();
const response = await instance.get<any, any>("/providers/proxies");
2022-11-19 17:22:29 +08:00
const providers = (response.providers || {}) as Record<string, IProviderItem>;
2022-10-11 22:51:18 +08:00
return Object.fromEntries(
Object.entries(providers).filter(([key, item]) => {
const type = item.vehicleType.toLowerCase();
return type === "http" || type === "file";
})
);
2022-12-15 12:22:20 +08:00
};
2022-03-18 14:43:22 +08:00
// proxy providers health check
2022-12-15 12:22:20 +08:00
export const providerHealthCheck = async (name: string) => {
2022-03-30 01:30:22 +08:00
const instance = await getAxios();
return instance.get(
`/providers/proxies/${encodeURIComponent(name)}/healthcheck`
);
2022-12-15 12:22:20 +08:00
};
2022-03-30 01:30:22 +08:00
2023-08-05 21:38:44 +08:00
export const providerUpdate = async (name: string) => {
const instance = await getAxios();
return instance.put(`/providers/proxies/${encodeURIComponent(name)}`);
};
2022-12-15 12:22:20 +08:00
export const getConnections = async () => {
const instance = await getAxios();
const result = await instance.get("/connections");
2022-11-19 17:22:29 +08:00
return result as any as IConnections;
2022-12-15 12:22:20 +08:00
};
2022-03-18 14:43:22 +08:00
// Close specific connection
2022-12-15 12:22:20 +08:00
export const deleteConnection = async (id: string) => {
2022-03-18 14:43:22 +08:00
const instance = await getAxios();
await instance.delete<any, any>(`/connections/${encodeURIComponent(id)}`);
2022-12-15 12:22:20 +08:00
};
2022-03-18 14:43:22 +08:00
// Close all connections
2022-12-15 12:22:20 +08:00
export const closeAllConnections = async () => {
2022-03-18 14:43:22 +08:00
const instance = await getAxios();
await instance.delete<any, any>(`/connections`);
2022-12-15 12:22:20 +08:00
};