feat: Support PAC Mode

This commit is contained in:
MystiPanda
2024-05-26 17:59:39 +08:00
parent fc1675575a
commit b9ec94d835
15 changed files with 311 additions and 85 deletions

View File

@@ -10,23 +10,34 @@ import {
styled,
TextField,
Typography,
Button,
} from "@mui/material";
import { useVerge } from "@/hooks/use-verge";
import { getSystemProxy } from "@/services/cmds";
import { getSystemProxy, getAutotemProxy } from "@/services/cmds";
import { BaseDialog, DialogRef, Notice, Switch } from "@/components/base";
import { Edit } from "@mui/icons-material";
import { EditorViewer } from "@/components/profile/editor-viewer";
const DEFAULT_PAC = `function FindProxyForURL(url, host) {
return "PROXY 127.0.0.1:%mixed-port%; SOCKS5 127.0.0.1:%mixed-port%; DIRECT;"
}`;
export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [editorOpen, setEditorOpen] = useState(false);
const { verge, patchVerge } = useVerge();
type SysProxy = Awaited<ReturnType<typeof getSystemProxy>>;
const [sysproxy, setSysproxy] = useState<SysProxy>();
type AutoProxy = Awaited<ReturnType<typeof getAutotemProxy>>;
const [autoproxy, setAutoproxy] = useState<AutoProxy>();
const {
enable_system_proxy: enabled,
proxy_auto_config,
pac_file_content,
enable_proxy_guard,
system_proxy_bypass,
proxy_guard_duration,
@@ -36,6 +47,8 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
guard: enable_proxy_guard,
bypass: system_proxy_bypass,
duration: proxy_guard_duration ?? 10,
pac: proxy_auto_config,
pac_content: pac_file_content ?? DEFAULT_PAC,
});
useImperativeHandle(ref, () => ({
@@ -45,8 +58,11 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
guard: enable_proxy_guard,
bypass: system_proxy_bypass,
duration: proxy_guard_duration ?? 10,
pac: proxy_auto_config,
pac_content: pac_file_content ?? DEFAULT_PAC,
});
getSystemProxy().then((p) => setSysproxy(p));
getAutotemProxy().then((p) => setAutoproxy(p));
},
close: () => setOpen(false),
}));
@@ -68,6 +84,12 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
if (value.bypass !== system_proxy_bypass) {
patch.system_proxy_bypass = value.bypass;
}
if (value.pac !== proxy_auto_config) {
patch.proxy_auto_config = value.pac;
}
if (value.pac_content !== pac_file_content) {
patch.pac_file_content = value.pac_content;
}
try {
await patchVerge(patch);
@@ -89,6 +111,15 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
onOk={onSave}
>
<List>
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Use PAC Mode")} />
<Switch
edge="end"
disabled={!enabled}
checked={value.pac}
onChange={(_, e) => setValue((v) => ({ ...v, pac: e }))}
/>
</ListItem>
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary={t("Proxy Guard")} />
<Switch
@@ -117,25 +148,63 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
}}
/>
</ListItem>
<ListItem sx={{ padding: "5px 2px", alignItems: "start" }}>
<ListItemText primary={t("Proxy Bypass")} sx={{ padding: "3px 0" }} />
</ListItem>
<ListItem sx={{ padding: "5px 2px" }}>
<TextField
disabled={!enabled}
size="small"
autoComplete="off"
multiline
rows={4}
sx={{ width: "100%" }}
value={value.bypass}
placeholder={sysproxy?.bypass || `-`}
onChange={(e) =>
setValue((v) => ({ ...v, bypass: e.target.value }))
}
/>
</ListItem>
{!value.pac && (
<>
<ListItem sx={{ padding: "5px 2px", alignItems: "start" }}>
<ListItemText
primary={t("Proxy Bypass")}
sx={{ padding: "3px 0" }}
/>
</ListItem>
<ListItem sx={{ padding: "5px 2px" }}>
<TextField
disabled={!enabled}
size="small"
autoComplete="off"
multiline
rows={4}
sx={{ width: "100%" }}
value={value.bypass}
placeholder={sysproxy?.bypass || `-`}
onChange={(e) =>
setValue((v) => ({ ...v, bypass: e.target.value }))
}
/>
</ListItem>
</>
)}
{value.pac && (
<>
<ListItem sx={{ padding: "5px 2px", alignItems: "start" }}>
<ListItemText
primary={t("PAC Script Content")}
sx={{ padding: "3px 0" }}
/>
<Button
startIcon={<Edit />}
variant="outlined"
onClick={() => {
setEditorOpen(true);
}}
>
{t("Edit")} PAC
</Button>
<EditorViewer
title={`${t("Edit")} PAC`}
mode="text"
property={value.pac_content ?? ""}
open={editorOpen}
language="javascript"
onChange={(content) => {
setValue((v) => ({ ...v, pac_content: content ?? "" }));
}}
onClose={() => {
setEditorOpen(false);
}}
/>
</ListItem>
</>
)}
</List>
<Box sx={{ mt: 2.5 }}>
@@ -146,29 +215,42 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
<FlexBox>
<Typography className="label">{t("Enable status")}</Typography>
<Typography className="value">
{(!!sysproxy?.enable).toString()}
{value.pac
? (!!autoproxy?.enable).toString()
: (!!sysproxy?.enable).toString()}
</Typography>
</FlexBox>
{!value.pac && (
<>
<FlexBox>
<Typography className="label">{t("Server Addr")}</Typography>
<Typography className="value">
{sysproxy?.server || "-"}
</Typography>
</FlexBox>
<FlexBox>
<Typography className="label">{t("Server Addr")}</Typography>
<Typography className="value">{sysproxy?.server || "-"}</Typography>
</FlexBox>
<FlexBox>
<Typography className="label">{t("Bypass")}</Typography>
</FlexBox>
<FlexBox>
<TextField
disabled={true}
size="small"
autoComplete="off"
multiline
rows={4}
sx={{ width: "100%" }}
value={sysproxy?.bypass || "-"}
/>
</FlexBox>
<FlexBox>
<Typography className="label">{t("Bypass")}</Typography>
</FlexBox>
<FlexBox>
<TextField
disabled={true}
size="small"
autoComplete="off"
multiline
rows={4}
sx={{ width: "100%" }}
value={sysproxy?.bypass || "-"}
/>
</FlexBox>
</>
)}
{value.pac && (
<FlexBox>
<Typography className="label">{t("PAC URL")}</Typography>
<Typography className="value">{autoproxy?.url || "-"}</Typography>
</FlexBox>
)}
</Box>
</BaseDialog>
);

View File

@@ -118,6 +118,9 @@
"System Proxy Setting": "System Proxy Setting",
"Open UWP tool": "Open UWP tool",
"Update GeoData": "Update GeoData",
"Use PAC Mode": "Use PAC Mode",
"PAC URL": "PAC URL",
"PAC Script Content": "PAC Script Content",
"Proxy Guard": "Proxy Guard",
"Guard Duration": "Guard Duration",
"Proxy Bypass": "Proxy Bypass",

View File

@@ -118,6 +118,9 @@
"System Proxy Setting": "تنظیمات پراکسی سیستم",
"Open UWP tool": "باز کردن ابزار UWP",
"Update GeoData": "به‌روزرسانی GeoData",
"Use PAC Mode": "استفاده از حالت PAC",
"PAC URL": "PAC URL",
"PAC Script Content": "محتوای اسکریپت PAC",
"Proxy Guard": "محافظ پراکسی",
"Guard Duration": "مدت محافظت",
"Proxy Bypass": "دور زدن پراکسی",

View File

@@ -118,6 +118,9 @@
"System Proxy Setting": "Настройка системного прокси",
"Open UWP tool": "Открыть UWP инструмент",
"Update GeoData": "Обновление GeoData",
"Use PAC Mode": "Используйте режим PAC.",
"PAC URL": "Адрес PAC",
"PAC Script Content": "Содержание сценария PAC",
"Proxy Guard": "Защита прокси",
"Guard Duration": "Период защиты",
"Proxy Bypass": "Игнорирование прокси",

View File

@@ -118,6 +118,9 @@
"System Proxy Setting": "系统代理设置",
"Open UWP tool": "UWP 工具",
"Update GeoData": "更新 GeoData",
"Use PAC Mode": "使用PAC模式",
"PAC URL": "PAC 地址",
"PAC Script Content": "PAC 脚本内容",
"Proxy Guard": "系统代理守卫",
"Guard Duration": "代理守卫间隔",
"Proxy Bypass": "代理绕过",

View File

@@ -127,6 +127,13 @@ export async function getSystemProxy() {
}>("get_sys_proxy");
}
export async function getAutotemProxy() {
return invoke<{
enable: boolean;
url: string;
}>("get_auto_proxy");
}
export async function changeClashCore(clashCore: string) {
return invoke<any>("change_clash_core", { clashCore });
}

View File

@@ -216,6 +216,8 @@ interface IVergeConfig {
enable_service_mode?: boolean;
enable_silent_start?: boolean;
enable_system_proxy?: boolean;
proxy_auto_config?: boolean;
pac_file_content?: string;
enable_random_port?: boolean;
verge_mixed_port?: number;
verge_socks_port?: number;