feat: compatible with proxy providers health check
This commit is contained in:
parent
3bdc98bd12
commit
71e6900375
@ -2,7 +2,7 @@ import useSWR, { useSWRConfig } from "swr";
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { useLockFn } from "ahooks";
|
import { useLockFn } from "ahooks";
|
||||||
import { Virtuoso } from "react-virtuoso";
|
import { Virtuoso } from "react-virtuoso";
|
||||||
import { updateProxy } from "@/services/api";
|
import { providerHealthCheck, updateProxy } from "@/services/api";
|
||||||
import { getProfiles, patchProfile } from "@/services/cmds";
|
import { getProfiles, patchProfile } from "@/services/cmds";
|
||||||
import delayManager from "@/services/delay";
|
import delayManager from "@/services/delay";
|
||||||
import useSortProxy from "./use-sort-proxy";
|
import useSortProxy from "./use-sort-proxy";
|
||||||
@ -74,10 +74,23 @@ const ProxyGlobal = (props: Props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onCheckAll = useLockFn(async () => {
|
const onCheckAll = useLockFn(async () => {
|
||||||
const names = sortedProxies.map((p) => p.name);
|
const providers = new Set(
|
||||||
|
sortedProxies.map((p) => p.provider!).filter(Boolean)
|
||||||
|
);
|
||||||
|
|
||||||
await delayManager.checkListDelay({ names, groupName, skipNum: 8 }, () =>
|
if (providers.size) {
|
||||||
mutate("getProxies")
|
Promise.allSettled(
|
||||||
|
[...providers].map((p) => providerHealthCheck(p))
|
||||||
|
).then(() => mutate("getProxies"));
|
||||||
|
}
|
||||||
|
|
||||||
|
await delayManager.checkListDelay(
|
||||||
|
{
|
||||||
|
names: sortedProxies.filter((p) => !p.provider).map((p) => p.name),
|
||||||
|
groupName,
|
||||||
|
skipNum: 16,
|
||||||
|
},
|
||||||
|
() => mutate("getProxies")
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ import {
|
|||||||
ExpandLessRounded,
|
ExpandLessRounded,
|
||||||
ExpandMoreRounded,
|
ExpandMoreRounded,
|
||||||
} from "@mui/icons-material";
|
} from "@mui/icons-material";
|
||||||
import { updateProxy } from "@/services/api";
|
import { providerHealthCheck, updateProxy } from "@/services/api";
|
||||||
import { getProfiles, patchProfile } from "@/services/cmds";
|
import { getProfiles, patchProfile } from "@/services/cmds";
|
||||||
import delayManager from "@/services/delay";
|
import delayManager from "@/services/delay";
|
||||||
import useSortProxy from "./use-sort-proxy";
|
import useSortProxy from "./use-sort-proxy";
|
||||||
@ -94,11 +94,23 @@ const ProxyGroup = ({ group }: Props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onCheckAll = useLockFn(async () => {
|
const onCheckAll = useLockFn(async () => {
|
||||||
const names = sortedProxies.map((p) => p.name);
|
const providers = new Set(
|
||||||
const groupName = group.name;
|
sortedProxies.map((p) => p.provider!).filter(Boolean)
|
||||||
|
);
|
||||||
|
|
||||||
await delayManager.checkListDelay({ names, groupName, skipNum: 16 }, () =>
|
if (providers.size) {
|
||||||
mutate("getProxies")
|
Promise.allSettled(
|
||||||
|
[...providers].map((p) => providerHealthCheck(p))
|
||||||
|
).then(() => mutate("getProxies"));
|
||||||
|
}
|
||||||
|
|
||||||
|
await delayManager.checkListDelay(
|
||||||
|
{
|
||||||
|
names: sortedProxies.filter((p) => !p.provider).map((p) => p.name),
|
||||||
|
groupName: group.name,
|
||||||
|
skipNum: 16,
|
||||||
|
},
|
||||||
|
() => mutate("getProxies")
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -46,8 +46,17 @@ const ProxyItem = (props: Props) => {
|
|||||||
const [delay, setDelay] = useState(-1);
|
const [delay, setDelay] = useState(-1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (proxy) {
|
if (!proxy) return;
|
||||||
|
|
||||||
|
if (!proxy.provider) {
|
||||||
setDelay(delayManager.getDelay(proxy.name, groupName));
|
setDelay(delayManager.getDelay(proxy.name, groupName));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { history = [] } = proxy;
|
||||||
|
if (history.length > 0) {
|
||||||
|
// 0ms以error显示
|
||||||
|
setDelay(history[history.length - 1].delay || 1e6);
|
||||||
}
|
}
|
||||||
}, [proxy]);
|
}, [proxy]);
|
||||||
|
|
||||||
@ -95,6 +104,9 @@ const ProxyItem = (props: Props) => {
|
|||||||
<>
|
<>
|
||||||
{proxy.name}
|
{proxy.name}
|
||||||
|
|
||||||
|
{showType && !!proxy.provider && (
|
||||||
|
<TypeBox component="span">{proxy.provider}</TypeBox>
|
||||||
|
)}
|
||||||
{showType && <TypeBox component="span">{proxy.type}</TypeBox>}
|
{showType && <TypeBox component="span">{proxy.type}</TypeBox>}
|
||||||
{showType && proxy.udp && <TypeBox component="span">UDP</TypeBox>}
|
{showType && proxy.udp && <TypeBox component="span">UDP</TypeBox>}
|
||||||
</>
|
</>
|
||||||
@ -104,23 +116,27 @@ const ProxyItem = (props: Props) => {
|
|||||||
<ListItemIcon
|
<ListItemIcon
|
||||||
sx={{ justifyContent: "flex-end", color: "primary.main" }}
|
sx={{ justifyContent: "flex-end", color: "primary.main" }}
|
||||||
>
|
>
|
||||||
<Widget
|
{!proxy.provider && (
|
||||||
className="the-check"
|
<Widget
|
||||||
onClick={(e) => {
|
className="the-check"
|
||||||
e.preventDefault();
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.preventDefault();
|
||||||
onDelay();
|
e.stopPropagation();
|
||||||
}}
|
onDelay();
|
||||||
sx={(theme) => ({
|
}}
|
||||||
":hover": { bgcolor: alpha(theme.palette.primary.main, 0.15) },
|
sx={(theme) => ({
|
||||||
})}
|
":hover": { bgcolor: alpha(theme.palette.primary.main, 0.15) },
|
||||||
>
|
})}
|
||||||
Check
|
>
|
||||||
</Widget>
|
Check
|
||||||
|
</Widget>
|
||||||
|
)}
|
||||||
|
|
||||||
<Widget
|
<Widget
|
||||||
className="the-delay"
|
className="the-delay"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
|
if (proxy.provider) return;
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
onDelay();
|
onDelay();
|
||||||
@ -132,9 +148,11 @@ const ProxyItem = (props: Props) => {
|
|||||||
? "success.main"
|
? "success.main"
|
||||||
: "text.secondary"
|
: "text.secondary"
|
||||||
}
|
}
|
||||||
sx={(theme) => ({
|
sx={({ palette }) =>
|
||||||
":hover": { bgcolor: alpha(theme.palette.primary.main, 0.15) },
|
!proxy.provider
|
||||||
})}
|
? { ":hover": { bgcolor: alpha(palette.primary.main, 0.15) } }
|
||||||
|
: {}
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{delay > 1e5 ? "Error" : delay > 3000 ? "Timeout" : `${delay}ms`}
|
{delay > 1e5 ? "Error" : delay > 3000 ? "Timeout" : `${delay}ms`}
|
||||||
</Widget>
|
</Widget>
|
||||||
|
@ -85,64 +85,93 @@ export async function updateProxy(group: string, proxy: string) {
|
|||||||
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy });
|
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get proxy
|
||||||
|
async function getProxiesInner() {
|
||||||
|
try {
|
||||||
|
const instance = await getAxios();
|
||||||
|
const response = await instance.get<any, any>("/proxies");
|
||||||
|
return (response?.proxies || {}) as Record<string, ApiType.ProxyItem>;
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the Proxy infomation
|
/// Get the Proxy infomation
|
||||||
export async function getProxies() {
|
export async function getProxies() {
|
||||||
const instance = await getAxios();
|
const [proxyRecord, providerRecord] = await Promise.all([
|
||||||
const response = await instance.get<any, any>("/proxies");
|
getProxiesInner(),
|
||||||
const records = (response?.proxies ?? {}) as Record<
|
getProviders(),
|
||||||
string,
|
]);
|
||||||
ApiType.ProxyItem
|
|
||||||
>;
|
|
||||||
|
|
||||||
const global = records["GLOBAL"];
|
// provider name map
|
||||||
const direct = records["DIRECT"];
|
const providerMap = Object.fromEntries(
|
||||||
const reject = records["REJECT"];
|
Object.entries(providerRecord).flatMap(([provider, item]) =>
|
||||||
const order = global?.all;
|
item.proxies.map((p) => [p.name, { ...p, provider }])
|
||||||
|
)
|
||||||
let groups: ApiType.ProxyGroupItem[] = [];
|
);
|
||||||
|
|
||||||
// compatible with proxy-providers
|
// compatible with proxy-providers
|
||||||
const generateItem = (name: string) => {
|
const generateItem = (name: string) => {
|
||||||
if (records[name]) return records[name];
|
if (proxyRecord[name]) return proxyRecord[name];
|
||||||
|
if (providerMap[name]) return providerMap[name];
|
||||||
return { name, type: "unknown", udp: false, history: [] };
|
return { name, type: "unknown", udp: false, history: [] };
|
||||||
};
|
};
|
||||||
|
|
||||||
if (order) {
|
const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord;
|
||||||
groups = order
|
|
||||||
.filter((name) => records[name]?.all)
|
let groups: ApiType.ProxyGroupItem[] = [];
|
||||||
.map((name) => records[name])
|
|
||||||
|
if (global?.all) {
|
||||||
|
groups = global.all
|
||||||
|
.filter((name) => proxyRecord[name]?.all)
|
||||||
|
.map((name) => proxyRecord[name])
|
||||||
.map((each) => ({
|
.map((each) => ({
|
||||||
...each,
|
...each,
|
||||||
all: each.all!.map((item) => generateItem(item)),
|
all: each.all!.map((item) => generateItem(item)),
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
groups = Object.values(records)
|
groups = Object.values(proxyRecord)
|
||||||
.filter((each) => each.name !== "GLOBAL" && each.all)
|
.filter((each) => each.name !== "GLOBAL" && each.all)
|
||||||
.map((each) => ({
|
.map((each) => ({
|
||||||
...each,
|
...each,
|
||||||
all: each.all!.map((item) => generateItem(item)),
|
all: each.all!.map((item) => generateItem(item)),
|
||||||
}));
|
}))
|
||||||
groups.sort((a, b) => b.name.localeCompare(a.name));
|
.sort((a, b) => b.name.localeCompare(a.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
const proxies = [direct, reject].concat(
|
const proxies = [direct, reject].concat(
|
||||||
Object.values(records).filter(
|
Object.values(proxyRecord).filter(
|
||||||
(p) => !p.all?.length && p.name !== "DIRECT" && p.name !== "REJECT"
|
(p) => !p.all?.length && p.name !== "DIRECT" && p.name !== "REJECT"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return { global, direct, groups, records, proxies };
|
return { global, direct, groups, records: proxyRecord, proxies };
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: get proxy providers
|
// get proxy providers
|
||||||
export async function getProviders() {
|
export async function getProviders() {
|
||||||
const instance = await getAxios();
|
try {
|
||||||
const response = await instance.get<any, any>("/providers/proxies");
|
const instance = await getAxios();
|
||||||
return response.providers as any;
|
const response = await instance.get<any, any>("/providers/proxies");
|
||||||
|
|
||||||
|
const providers = (response.providers || {}) as Record<
|
||||||
|
string,
|
||||||
|
ApiType.ProviderItem
|
||||||
|
>;
|
||||||
|
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(providers).filter(([key, item]) => {
|
||||||
|
const type = item.vehicleType.toLowerCase();
|
||||||
|
return type === "http" || type === "file";
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: proxy providers health check
|
// proxy providers health check
|
||||||
export async function getProviderHealthCheck(name: string) {
|
export async function providerHealthCheck(name: string) {
|
||||||
const instance = await getAxios();
|
const instance = await getAxios();
|
||||||
return instance.get(
|
return instance.get(
|
||||||
`/providers/proxies/${encodeURIComponent(name)}/healthcheck`
|
`/providers/proxies/${encodeURIComponent(name)}/healthcheck`
|
||||||
|
9
src/services/types.d.ts
vendored
9
src/services/types.d.ts
vendored
@ -31,12 +31,21 @@ declare namespace ApiType {
|
|||||||
}[];
|
}[];
|
||||||
all?: string[];
|
all?: string[];
|
||||||
now?: string;
|
now?: string;
|
||||||
|
provider?: string; // 记录是否来自provider
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProxyGroupItem = Omit<ProxyItem, "all"> & {
|
type ProxyGroupItem = Omit<ProxyItem, "all"> & {
|
||||||
all: ProxyItem[];
|
all: ProxyItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface ProviderItem {
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
proxies: ProxyItem[];
|
||||||
|
updatedAt: string;
|
||||||
|
vehicleType: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface TrafficItem {
|
interface TrafficItem {
|
||||||
up: number;
|
up: number;
|
||||||
down: number;
|
down: number;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user