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

221 lines
5.5 KiB
TypeScript
Raw Normal View History

2021-12-25 23:20:59 +08:00
import { getClashInfo } from "./cmds";
2024-01-14 18:35:10 +08:00
import {
fetch as tauriFetch,
HttpVerb,
Body,
Response,
} from "@tauri-apps/api/http";
let clashInfo: IClashInfo | null;
export const refreshClashInfo = async () => {
clashInfo = await getClashInfo();
return clashInfo;
};
export const fetch = async (
path: string,
method: HttpVerb,
body?: any
): Promise<Response<any>> => {
2022-12-15 12:22:20 +08:00
let server = "";
let secret = "";
2021-12-25 23:20:59 +08:00
try {
2024-01-14 18:35:10 +08:00
const info = clashInfo ?? (await refreshClashInfo());
2021-12-25 23:20:59 +08:00
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
2024-01-14 18:35:10 +08:00
return tauriFetch(`http://${server}${path}`, {
method,
2021-12-25 22:33:29 +08:00
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
2023-03-16 17:03:12 +08:00
timeout: 15000,
2024-01-14 18:35:10 +08:00
body: body ? Body.json(body) : undefined,
2021-12-25 22:33:29 +08:00
});
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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/version", "GET");
return res.data 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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/configs", "GET");
return res.data 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>) => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/configs", "PATCH", config);
return res;
2022-12-15 12:22:20 +08:00
};
2021-12-25 22:33:29 +08:00
2023-12-10 15:22:04 +08:00
/// Update geo data
export const updateGeoData = async () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/configs/geo", "POST");
return res;
2023-12-10 15:22:04 +08:00
};
2023-12-10 15:57:10 +08:00
/// Upgrade clash core
export const upgradeCore = async () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/upgrade", "POST");
return res;
2023-12-10 15:57:10 +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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/rules", "GET");
return res?.data?.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
};
2024-01-14 18:35:10 +08:00
const result = await fetch(
2022-12-15 12:22:20 +08:00
`/proxies/${encodeURIComponent(name)}/delay`,
2024-01-14 18:35:10 +08:00
"GET",
2022-12-15 12:22:20 +08:00
{ params }
);
2024-01-14 18:35:10 +08:00
return result.data as any as { delay: number };
2022-12-15 12:22:20 +08:00
};
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) => {
2024-01-14 18:35:10 +08:00
const res = await fetch(`/proxies/${encodeURIComponent(group)}`, "PUT", {
name: proxy,
});
return res;
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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/proxies", "GET");
return (res?.data?.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];
2024-01-11 12:34:05 +08:00
return {
name,
type: "unknown",
udp: false,
xudp: false,
tfo: 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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/providers/proxies", "GET");
const providers = (res.data.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) => {
2024-01-14 18:35:10 +08:00
const res = await fetch(
`/providers/proxies/${encodeURIComponent(name)}/healthcheck`,
"GET"
2022-03-30 01:30:22 +08:00
);
2024-01-14 18:35:10 +08:00
return res;
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) => {
2024-01-14 18:35:10 +08:00
const res = await fetch(
`/providers/proxies/${encodeURIComponent(name)}`,
"PUT"
);
return res;
2023-08-05 21:38:44 +08:00
};
2022-12-15 12:22:20 +08:00
export const getConnections = async () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/connections", "GET");
return res.data 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) => {
2024-01-14 18:35:10 +08:00
const res = await fetch(`/connections/${encodeURIComponent(id)}`, "DELETE");
return res;
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 () => {
2024-01-14 18:35:10 +08:00
const res = await fetch("/connections", "DELETE");
return res;
2022-12-15 12:22:20 +08:00
};