mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 05:03:45 +08:00
feat: Optimizing Provider Support
This commit is contained in:
parent
b6481cfcda
commit
cd92b34ef1
@ -7,25 +7,30 @@ import {
|
|||||||
List,
|
List,
|
||||||
ListItem,
|
ListItem,
|
||||||
ListItemText,
|
ListItemText,
|
||||||
|
styled,
|
||||||
|
Box,
|
||||||
|
alpha,
|
||||||
|
Typography,
|
||||||
|
Divider,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { RefreshRounded } from "@mui/icons-material";
|
import { RefreshRounded } from "@mui/icons-material";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useLockFn } from "ahooks";
|
import { useLockFn } from "ahooks";
|
||||||
import { getProviders, providerUpdate } from "@/services/api";
|
import { getProxyProviders, proxyProviderUpdate } from "@/services/api";
|
||||||
import { BaseDialog } from "../base";
|
import { BaseDialog } from "../base";
|
||||||
|
|
||||||
export const ProviderButton = () => {
|
export const ProviderButton = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { data } = useSWR("getProviders", getProviders);
|
const { data } = useSWR("getProxyProviders", getProxyProviders);
|
||||||
|
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
const hasProvider = Object.keys(data || {}).length > 0;
|
const hasProvider = Object.keys(data || {}).length > 0;
|
||||||
|
|
||||||
const handleUpdate = useLockFn(async (key: string) => {
|
const handleUpdate = useLockFn(async (key: string) => {
|
||||||
await providerUpdate(key);
|
await proxyProviderUpdate(key);
|
||||||
await mutate("getProxies");
|
await mutate("getProxies");
|
||||||
await mutate("getProviders");
|
await mutate("getProxyProviders");
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!hasProvider) return null;
|
if (!hasProvider) return null;
|
||||||
@ -43,7 +48,23 @@ export const ProviderButton = () => {
|
|||||||
|
|
||||||
<BaseDialog
|
<BaseDialog
|
||||||
open={open}
|
open={open}
|
||||||
title={t("Proxy Provider")}
|
title={
|
||||||
|
<Box display="flex" justifyContent="space-between" gap={1}>
|
||||||
|
<Typography variant="h6">{t("Proxy Provider")}</Typography>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={async () => {
|
||||||
|
Object.entries(data || {}).forEach(async ([key, item]) => {
|
||||||
|
await proxyProviderUpdate(key);
|
||||||
|
await mutate("getProxies");
|
||||||
|
await mutate("getProxyProviders");
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("Update All")}
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
}
|
||||||
contentSx={{ width: 400 }}
|
contentSx={{ width: 400 }}
|
||||||
disableOk
|
disableOk
|
||||||
cancelBtn={t("Cancel")}
|
cancelBtn={t("Cancel")}
|
||||||
@ -54,29 +75,43 @@ export const ProviderButton = () => {
|
|||||||
{Object.entries(data || {}).map(([key, item]) => {
|
{Object.entries(data || {}).map(([key, item]) => {
|
||||||
const time = dayjs(item.updatedAt);
|
const time = dayjs(item.updatedAt);
|
||||||
return (
|
return (
|
||||||
<ListItem sx={{ p: 0 }} key={key}>
|
<>
|
||||||
<ListItemText
|
<ListItem sx={{ p: 0 }} key={key}>
|
||||||
primary={key}
|
<ListItemText
|
||||||
secondary={
|
primary={
|
||||||
<>
|
<>
|
||||||
<span style={{ marginRight: "4em" }}>
|
<Typography
|
||||||
Type: {item.vehicleType}
|
variant="h6"
|
||||||
</span>
|
component="span"
|
||||||
<span title={time.format("YYYY-MM-DD HH:mm:ss")}>
|
noWrap
|
||||||
Updated: {time.fromNow()}
|
title={key}
|
||||||
</span>
|
>
|
||||||
</>
|
{key}
|
||||||
}
|
</Typography>
|
||||||
/>
|
</>
|
||||||
<IconButton
|
}
|
||||||
size="small"
|
secondary={
|
||||||
color="inherit"
|
<>
|
||||||
title="Update Provider"
|
<StyledTypeBox component="span">
|
||||||
onClick={() => handleUpdate(key)}
|
{item.vehicleType}
|
||||||
>
|
</StyledTypeBox>
|
||||||
<RefreshRounded />
|
<StyledTypeBox component="span">
|
||||||
</IconButton>
|
{t("Update At")} {time.fromNow()}
|
||||||
</ListItem>
|
</StyledTypeBox>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="inherit"
|
||||||
|
title="Update Provider"
|
||||||
|
onClick={() => handleUpdate(key)}
|
||||||
|
>
|
||||||
|
<RefreshRounded />
|
||||||
|
</IconButton>
|
||||||
|
</ListItem>
|
||||||
|
<Divider />
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</List>
|
</List>
|
||||||
@ -84,3 +119,15 @@ export const ProviderButton = () => {
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const StyledTypeBox = styled(Box)(({ theme }) => ({
|
||||||
|
display: "inline-block",
|
||||||
|
border: "1px solid #ccc",
|
||||||
|
borderColor: alpha(theme.palette.primary.main, 0.5),
|
||||||
|
color: alpha(theme.palette.primary.main, 0.8),
|
||||||
|
borderRadius: 4,
|
||||||
|
fontSize: 10,
|
||||||
|
marginRight: "4px",
|
||||||
|
padding: "0 2px",
|
||||||
|
lineHeight: 1.25,
|
||||||
|
}));
|
||||||
|
150
src/components/rule/provider-button.tsx
Normal file
150
src/components/rule/provider-button.tsx
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
import dayjs from "dayjs";
|
||||||
|
import useSWR, { mutate } from "swr";
|
||||||
|
import { useState } from "react";
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
IconButton,
|
||||||
|
List,
|
||||||
|
ListItem,
|
||||||
|
ListItemText,
|
||||||
|
Typography,
|
||||||
|
styled,
|
||||||
|
Box,
|
||||||
|
alpha,
|
||||||
|
Divider,
|
||||||
|
} from "@mui/material";
|
||||||
|
import { RefreshRounded } from "@mui/icons-material";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { useLockFn } from "ahooks";
|
||||||
|
import { getRuleProviders, ruleProviderUpdate } from "@/services/api";
|
||||||
|
import { BaseDialog } from "../base";
|
||||||
|
|
||||||
|
export const ProviderButton = () => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { data } = useSWR("getRuleProviders", getRuleProviders);
|
||||||
|
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
const hasProvider = Object.keys(data || {}).length > 0;
|
||||||
|
|
||||||
|
const handleUpdate = useLockFn(async (key: string) => {
|
||||||
|
await ruleProviderUpdate(key);
|
||||||
|
await mutate("getRules");
|
||||||
|
await mutate("getRuleProviders");
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!hasProvider) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
sx={{ textTransform: "capitalize" }}
|
||||||
|
onClick={() => setOpen(true)}
|
||||||
|
>
|
||||||
|
{t("Provider")}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<BaseDialog
|
||||||
|
open={open}
|
||||||
|
title={
|
||||||
|
<Box display="flex" justifyContent="space-between" gap={1}>
|
||||||
|
<Typography variant="h6">{t("Rule Provider")}</Typography>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={async () => {
|
||||||
|
Object.entries(data || {}).forEach(async ([key, item]) => {
|
||||||
|
await ruleProviderUpdate(key);
|
||||||
|
await mutate("getRules");
|
||||||
|
await mutate("getRuleProviders");
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("Update All")}
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
}
|
||||||
|
contentSx={{ width: 400 }}
|
||||||
|
disableOk
|
||||||
|
cancelBtn={t("Cancel")}
|
||||||
|
onClose={() => setOpen(false)}
|
||||||
|
onCancel={() => setOpen(false)}
|
||||||
|
>
|
||||||
|
<List sx={{ py: 0, minHeight: 250 }}>
|
||||||
|
{Object.entries(data || {}).map(([key, item]) => {
|
||||||
|
const time = dayjs(item.updatedAt);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<ListItem sx={{ p: 0 }} key={key}>
|
||||||
|
<ListItemText
|
||||||
|
primary={
|
||||||
|
<>
|
||||||
|
<Typography
|
||||||
|
variant="h6"
|
||||||
|
component="span"
|
||||||
|
noWrap
|
||||||
|
title={key}
|
||||||
|
>
|
||||||
|
{key}
|
||||||
|
</Typography>
|
||||||
|
<TypeBox component="span" sx={{ marginLeft: "8px" }}>
|
||||||
|
{item.ruleCount}
|
||||||
|
</TypeBox>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
secondary={
|
||||||
|
<>
|
||||||
|
<StyledTypeBox component="span">
|
||||||
|
{item.vehicleType}
|
||||||
|
</StyledTypeBox>
|
||||||
|
<StyledTypeBox component="span">
|
||||||
|
{item.behavior}
|
||||||
|
</StyledTypeBox>
|
||||||
|
<StyledTypeBox component="span">
|
||||||
|
{t("Update At")} {time.fromNow()}
|
||||||
|
</StyledTypeBox>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="inherit"
|
||||||
|
title="Update Provider"
|
||||||
|
onClick={() => handleUpdate(key)}
|
||||||
|
>
|
||||||
|
<RefreshRounded />
|
||||||
|
</IconButton>
|
||||||
|
</ListItem>
|
||||||
|
<Divider />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</List>
|
||||||
|
</BaseDialog>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
const TypeBox = styled(Box)(({ theme }) => ({
|
||||||
|
display: "inline-block",
|
||||||
|
border: "1px solid #ccc",
|
||||||
|
borderColor: alpha(theme.palette.secondary.main, 0.5),
|
||||||
|
color: alpha(theme.palette.secondary.main, 0.8),
|
||||||
|
borderRadius: 4,
|
||||||
|
fontSize: 10,
|
||||||
|
marginRight: "4px",
|
||||||
|
padding: "0 2px",
|
||||||
|
lineHeight: 1.25,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const StyledTypeBox = styled(Box)(({ theme }) => ({
|
||||||
|
display: "inline-block",
|
||||||
|
border: "1px solid #ccc",
|
||||||
|
borderColor: alpha(theme.palette.primary.main, 0.5),
|
||||||
|
color: alpha(theme.palette.primary.main, 0.8),
|
||||||
|
borderRadius: 4,
|
||||||
|
fontSize: 10,
|
||||||
|
marginRight: "4px",
|
||||||
|
padding: "0 2px",
|
||||||
|
lineHeight: 1.25,
|
||||||
|
}));
|
@ -57,6 +57,8 @@
|
|||||||
"Filter conditions": "Filter conditions",
|
"Filter conditions": "Filter conditions",
|
||||||
"Refresh profiles": "Refresh profiles",
|
"Refresh profiles": "Refresh profiles",
|
||||||
"Rules": "Rules",
|
"Rules": "Rules",
|
||||||
|
"Update All": "Update All",
|
||||||
|
"Update At": "Update At",
|
||||||
|
|
||||||
"Type": "Type",
|
"Type": "Type",
|
||||||
"Name": "Name",
|
"Name": "Name",
|
||||||
|
@ -56,6 +56,9 @@
|
|||||||
"Filter": "Фильтр",
|
"Filter": "Фильтр",
|
||||||
"Filter conditions": "Условия фильтрации",
|
"Filter conditions": "Условия фильтрации",
|
||||||
"Refresh profiles": "Обновить профили",
|
"Refresh profiles": "Обновить профили",
|
||||||
|
"Rules": "Правила",
|
||||||
|
"Update All": "Обновить все",
|
||||||
|
"Update At": "Обновлено в",
|
||||||
|
|
||||||
"Type": "Тип",
|
"Type": "Тип",
|
||||||
"Name": "Название",
|
"Name": "Название",
|
||||||
|
@ -57,6 +57,8 @@
|
|||||||
"Filter conditions": "过滤条件",
|
"Filter conditions": "过滤条件",
|
||||||
"Refresh profiles": "刷新订阅",
|
"Refresh profiles": "刷新订阅",
|
||||||
"Rules": "规则",
|
"Rules": "规则",
|
||||||
|
"Update All": "更新全部",
|
||||||
|
"Update At": "更新于",
|
||||||
|
|
||||||
"Type": "类型",
|
"Type": "类型",
|
||||||
"Name": "名称",
|
"Name": "名称",
|
||||||
|
@ -54,7 +54,7 @@ const Layout = () => {
|
|||||||
mutate("getProxies");
|
mutate("getProxies");
|
||||||
mutate("getVersion");
|
mutate("getVersion");
|
||||||
mutate("getClashConfig");
|
mutate("getClashConfig");
|
||||||
mutate("getProviders");
|
mutate("getProxyProviders");
|
||||||
});
|
});
|
||||||
|
|
||||||
// update the verge config
|
// update the verge config
|
||||||
|
@ -2,10 +2,11 @@ import useSWR from "swr";
|
|||||||
import { useState, useMemo } from "react";
|
import { useState, useMemo } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Virtuoso } from "react-virtuoso";
|
import { Virtuoso } from "react-virtuoso";
|
||||||
import { Box, Paper, TextField } from "@mui/material";
|
import { Box, TextField } from "@mui/material";
|
||||||
import { getRules } from "@/services/api";
|
import { getRules } from "@/services/api";
|
||||||
import { BaseEmpty, BasePage } from "@/components/base";
|
import { BaseEmpty, BasePage } from "@/components/base";
|
||||||
import RuleItem from "@/components/rule/rule-item";
|
import RuleItem from "@/components/rule/rule-item";
|
||||||
|
import { ProviderButton } from "@/components/rule/provider-button";
|
||||||
|
|
||||||
const RulesPage = () => {
|
const RulesPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@ -18,7 +19,16 @@ const RulesPage = () => {
|
|||||||
}, [data, filterText]);
|
}, [data, filterText]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BasePage full title={t("Rules")} contentStyle={{ height: "100%" }}>
|
<BasePage
|
||||||
|
full
|
||||||
|
title={t("Rules")}
|
||||||
|
contentStyle={{ height: "100%" }}
|
||||||
|
header={
|
||||||
|
<Box display="flex" alignItems="center" gap={1}>
|
||||||
|
<ProviderButton />
|
||||||
|
</Box>
|
||||||
|
}
|
||||||
|
>
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
pt: 1,
|
pt: 1,
|
||||||
|
@ -105,7 +105,7 @@ export const getProxiesInner = async () => {
|
|||||||
export const getProxies = async () => {
|
export const getProxies = async () => {
|
||||||
const [proxyRecord, providerRecord] = await Promise.all([
|
const [proxyRecord, providerRecord] = await Promise.all([
|
||||||
getProxiesInner(),
|
getProxiesInner(),
|
||||||
getProviders(),
|
getProxyProviders(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// provider name map
|
// provider name map
|
||||||
@ -166,11 +166,31 @@ export const getProxies = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// get proxy providers
|
// get proxy providers
|
||||||
export const getProviders = async () => {
|
export const getProxyProviders = async () => {
|
||||||
const instance = await getAxios();
|
const instance = await getAxios();
|
||||||
const response = await instance.get<any, any>("/providers/proxies");
|
const response = await instance.get<any, any>("/providers/proxies");
|
||||||
|
|
||||||
const providers = (response.providers || {}) as Record<string, IProviderItem>;
|
const providers = (response.providers || {}) as Record<
|
||||||
|
string,
|
||||||
|
IProxyProviderItem
|
||||||
|
>;
|
||||||
|
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(providers).filter(([key, item]) => {
|
||||||
|
const type = item.vehicleType.toLowerCase();
|
||||||
|
return type === "http" || type === "file";
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRuleProviders = async () => {
|
||||||
|
const instance = await getAxios();
|
||||||
|
const response = await instance.get<any, any>("/providers/rules");
|
||||||
|
|
||||||
|
const providers = (response.providers || {}) as Record<
|
||||||
|
string,
|
||||||
|
IRuleProviderItem
|
||||||
|
>;
|
||||||
|
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
Object.entries(providers).filter(([key, item]) => {
|
Object.entries(providers).filter(([key, item]) => {
|
||||||
@ -188,11 +208,16 @@ export const providerHealthCheck = async (name: string) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const providerUpdate = async (name: string) => {
|
export const proxyProviderUpdate = async (name: string) => {
|
||||||
const instance = await getAxios();
|
const instance = await getAxios();
|
||||||
return instance.put(`/providers/proxies/${encodeURIComponent(name)}`);
|
return instance.put(`/providers/proxies/${encodeURIComponent(name)}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const ruleProviderUpdate = async (name: string) => {
|
||||||
|
const instance = await getAxios();
|
||||||
|
return instance.put(`/providers/rules/${encodeURIComponent(name)}`);
|
||||||
|
};
|
||||||
|
|
||||||
export const getConnections = async () => {
|
export const getConnections = async () => {
|
||||||
const instance = await getAxios();
|
const instance = await getAxios();
|
||||||
const result = await instance.get("/connections");
|
const result = await instance.get("/connections");
|
||||||
|
12
src/services/types.d.ts
vendored
12
src/services/types.d.ts
vendored
@ -61,7 +61,7 @@ type IProxyGroupItem = Omit<IProxyItem, "all"> & {
|
|||||||
all: IProxyItem[];
|
all: IProxyItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IProviderItem {
|
interface IProxyProviderItem {
|
||||||
name: string;
|
name: string;
|
||||||
type: string;
|
type: string;
|
||||||
proxies: IProxyItem[];
|
proxies: IProxyItem[];
|
||||||
@ -69,6 +69,16 @@ interface IProviderItem {
|
|||||||
vehicleType: string;
|
vehicleType: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IRuleProviderItem {
|
||||||
|
name: string;
|
||||||
|
behavior: string;
|
||||||
|
format: string;
|
||||||
|
ruleCount: number;
|
||||||
|
type: string;
|
||||||
|
updatedAt: string;
|
||||||
|
vehicleType: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface ITrafficItem {
|
interface ITrafficItem {
|
||||||
up: number;
|
up: number;
|
||||||
down: number;
|
down: number;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user