123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- import { forwardRef, useImperativeHandle, useState } from "react";
- import { useLockFn } from "ahooks";
- import { useTranslation } from "react-i18next";
- import {
- Box,
- InputAdornment,
- List,
- ListItem,
- ListItemText,
- styled,
- TextField,
- Typography,
- } from "@mui/material";
- import { useVerge } from "@/hooks/use-verge";
- import { getSystemProxy } from "@/services/cmds";
- import { BaseDialog, DialogRef, Notice, Switch } from "@/components/base";
- export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
- const { t } = useTranslation();
- const [open, setOpen] = useState(false);
- const { verge, patchVerge } = useVerge();
- type SysProxy = Awaited<ReturnType<typeof getSystemProxy>>;
- const [sysproxy, setSysproxy] = useState<SysProxy>();
- const {
- enable_system_proxy: enabled,
- 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,
- });
- useImperativeHandle(ref, () => ({
- open: () => {
- setOpen(true);
- setValue({
- guard: enable_proxy_guard,
- bypass: system_proxy_bypass,
- duration: proxy_guard_duration ?? 10,
- });
- getSystemProxy().then((p) => setSysproxy(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<IVergeConfig> = {};
- 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;
- }
- try {
- await patchVerge(patch);
- setOpen(false);
- } catch (err: any) {
- Notice.error(err.message || err.toString());
- }
- });
- return (
- <BaseDialog
- open={open}
- title={t("System Proxy Setting")}
- contentSx={{ width: 450, maxHeight: 300 }}
- okBtn={t("Save")}
- cancelBtn={t("Cancel")}
- onClose={() => setOpen(false)}
- onCancel={() => setOpen(false)}
- onOk={onSave}
- >
- <List>
- <ListItem sx={{ padding: "5px 2px" }}>
- <ListItemText primary={t("Proxy Guard")} />
- <Switch
- edge="end"
- disabled={!enabled}
- checked={value.guard}
- onChange={(_, e) => setValue((v) => ({ ...v, guard: e }))}
- />
- </ListItem>
- <ListItem sx={{ padding: "5px 2px" }}>
- <ListItemText primary={t("Guard Duration")} />
- <TextField
- disabled={!enabled}
- size="small"
- value={value.duration}
- sx={{ width: 100 }}
- InputProps={{
- endAdornment: <InputAdornment position="end">s</InputAdornment>,
- }}
- onChange={(e) => {
- setValue((v) => ({
- ...v,
- duration: +e.target.value.replace(/\D/, ""),
- }));
- }}
- />
- </ListItem>
- <ListItem sx={{ padding: "5px 2px", alignItems: "start" }}>
- <ListItemText primary={t("Proxy Bypass")} sx={{ padding: "3px 0" }} />
- <TextField
- disabled={!enabled}
- size="small"
- autoComplete="off"
- multiline
- rows={3}
- sx={{ width: 280 }}
- value={value.bypass}
- placeholder={sysproxy?.bypass || `-`}
- onChange={(e) =>
- setValue((v) => ({ ...v, bypass: e.target.value }))
- }
- />
- </ListItem>
- </List>
- <Box sx={{ mt: 2.5 }}>
- <Typography variant="body1" sx={{ fontSize: "18px", mb: 1 }}>
- {t("Current System Proxy")}
- </Typography>
- <FlexBox>
- <Typography className="label">{t("Enable status")}</Typography>
- <Typography className="value">
- {(!!sysproxy?.enable).toString()}
- </Typography>
- </FlexBox>
- <FlexBox>
- <Typography className="label">{t("Server Addr")}</Typography>
- <Typography className="value">{sysproxy?.server || "-"}</Typography>
- </FlexBox>
- <FlexBox>
- <Typography className="label">{t("Bypass")}</Typography>
- <Typography className="value" style={{ overflowWrap: "anywhere" }}>
- {sysproxy?.bypass || "-"}
- </Typography>
- </FlexBox>
- </Box>
- </BaseDialog>
- );
- });
- const FlexBox = styled("div")`
- display: flex;
- margin-top: 4px;
- .label {
- flex: none;
- width: 85px;
- }
- `;
|