import { forwardRef, useImperativeHandle, useState } from "react"; import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; import { Box, InputAdornment, List, ListItem, ListItemText, styled, TextField, Typography, Button, } from "@mui/material"; import { useVerge } from "@/hooks/use-verge"; 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((props, ref) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [editorOpen, setEditorOpen] = useState(false); const { verge, patchVerge } = useVerge(); type SysProxy = Awaited>; const [sysproxy, setSysproxy] = useState(); type AutoProxy = Awaited>; const [autoproxy, setAutoproxy] = useState(); const { enable_system_proxy: enabled, proxy_auto_config, pac_file_content, enable_proxy_guard, system_proxy_bypass, proxy_guard_duration, } = verge ?? {}; const [value, setValue] = useState({ 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, () => ({ open: () => { setOpen(true); setValue({ 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), })); const onSave = useLockFn(async () => { if (value.duration < 1) { Notice.error(t("Proxy Daemon Duration Cannot be Less than 1 Second")); return; } const patch: Partial = {}; if (value.guard !== enable_proxy_guard) { patch.enable_proxy_guard = value.guard; } if (value.duration !== proxy_guard_duration) { patch.proxy_guard_duration = value.duration; } 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); setOpen(false); } catch (err: any) { Notice.error(err.message || err.toString()); } }); return ( setOpen(false)} onCancel={() => setOpen(false)} onOk={onSave} > setValue((v) => ({ ...v, pac: e }))} /> setValue((v) => ({ ...v, guard: e }))} /> s, }} onChange={(e) => { setValue((v) => ({ ...v, duration: +e.target.value.replace(/\D/, ""), })); }} /> {!value.pac && ( <> setValue((v) => ({ ...v, bypass: e.target.value })) } /> )} {value.pac && ( <> { setValue((v) => ({ ...v, pac_content: content ?? "" })); }} onClose={() => { setEditorOpen(false); }} /> )} {t("Current System Proxy")} {t("Enable status")} {value.pac ? (!!autoproxy?.enable).toString() : (!!sysproxy?.enable).toString()} {!value.pac && ( <> {t("Server Addr")} {sysproxy?.server || "-"} {t("Bypass")} )} {value.pac && ( {t("PAC URL")} {autoproxy?.url || "-"} )} ); }); const FlexBox = styled("div")` display: flex; margin-top: 4px; .label { flex: none; width: 85px; } `;