feat: Allow disable unused ports

This commit is contained in:
MystiPanda 2024-05-09 09:01:04 +08:00
parent dddca7bf4b
commit 1f7acd4027
5 changed files with 181 additions and 34 deletions

View File

@ -130,14 +130,24 @@ pub struct IVerge {
#[cfg(not(target_os = "windows"))]
pub verge_redir_port: Option<u16>,
#[cfg(not(target_os = "windows"))]
pub verge_redir_enabled: Option<bool>,
#[cfg(target_os = "linux")]
pub verge_tproxy_port: Option<u16>,
#[cfg(target_os = "linux")]
pub verge_tproxy_enabled: Option<bool>,
pub verge_mixed_port: Option<u16>,
pub verge_socks_port: Option<u16>,
pub verge_socks_enabled: Option<bool>,
pub verge_port: Option<u16>,
pub verge_http_enabled: Option<bool>,
}
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
@ -198,11 +208,17 @@ impl IVerge {
enable_random_port: Some(false),
#[cfg(not(target_os = "windows"))]
verge_redir_port: Some(7895),
#[cfg(not(target_os = "windows"))]
verge_redir_enabled: Some(true),
#[cfg(target_os = "linux")]
verge_tproxy_port: Some(7896),
#[cfg(target_os = "linux")]
verge_tproxy_enabled: Some(true),
verge_mixed_port: Some(7897),
verge_socks_port: Some(7898),
verge_socks_enabled: Some(true),
verge_port: Some(7899),
verge_http_enabled: Some(true),
enable_proxy_guard: Some(false),
proxy_guard_duration: Some(30),
auto_close_connection: Some(true),
@ -251,11 +267,17 @@ impl IVerge {
patch!(enable_random_port);
#[cfg(not(target_os = "windows"))]
patch!(verge_redir_port);
#[cfg(not(target_os = "windows"))]
patch!(verge_redir_enabled);
#[cfg(target_os = "linux")]
patch!(verge_tproxy_port);
#[cfg(target_os = "linux")]
patch!(verge_tproxy_enabled);
patch!(verge_mixed_port);
patch!(verge_socks_port);
patch!(verge_socks_enabled);
patch!(verge_port);
patch!(verge_http_enabled);
patch!(enable_system_proxy);
patch!(enable_proxy_guard);
patch!(system_proxy_bypass);

View File

@ -22,15 +22,29 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
// config.yaml 的订阅
let clash_config = { Config::clash().latest().0.clone() };
let (clash_core, enable_tun, enable_builtin) = {
let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled) = {
let verge = Config::verge();
let verge = verge.latest();
(
verge.clash_core.clone(),
verge.enable_tun_mode.unwrap_or(false),
verge.enable_builtin_enhanced.unwrap_or(true),
verge.verge_socks_enabled.unwrap_or(true),
verge.verge_http_enabled.unwrap_or(true),
)
};
#[cfg(not(target_os = "windows"))]
let redir_enabled = {
let verge = Config::verge();
let verge = verge.latest();
verge.verge_redir_enabled.unwrap_or(true)
};
#[cfg(target_os = "linux")]
let tproxy_enabled = {
let verge = Config::verge();
let verge = verge.latest();
verge.verge_tproxy_enabled.unwrap_or(true)
};
// 从profiles里拿东西
let (mut config, chain) = {
@ -88,6 +102,28 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
}
config.insert("tun".into(), tun.into());
} else {
if key.as_str() == Some("socks-port") && !socks_enabled {
config.remove("socks-port");
continue;
}
if key.as_str() == Some("port") && !http_enabled {
config.remove("port");
continue;
}
#[cfg(not(target_os = "windows"))]
{
if key.as_str() == Some("redir-port") && !redir_enabled {
config.remove("redir-port");
continue;
}
}
#[cfg(target_os = "linux")]
{
if key.as_str() == Some("tproxy-port") && !tproxy_enabled {
config.remove("tproxy-port");
continue;
}
}
config.insert(key, value);
}
}

View File

@ -183,7 +183,12 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
let common_tray_icon = patch.common_tray_icon;
let sysproxy_tray_icon = patch.sysproxy_tray_icon;
let tun_tray_icon = patch.tun_tray_icon;
#[cfg(not(target_os = "windows"))]
let redir_enabled = patch.verge_redir_enabled;
#[cfg(target_os = "linux")]
let tproxy_enabled = patch.verge_tproxy_enabled;
let socks_enabled = patch.verge_socks_enabled;
let http_enabled = patch.verge_http_enabled;
match {
let service_mode = patch.enable_service_mode;
@ -195,7 +200,20 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
} else if tun_mode.is_some() {
update_core_config().await?;
}
#[cfg(not(target_os = "windows"))]
if redir_enabled.is_some() {
Config::generate()?;
CoreManager::global().run_core().await?;
}
#[cfg(target_os = "linux")]
if tproxy_enabled.is_some() {
Config::generate()?;
CoreManager::global().run_core().await?;
}
if socks_enabled.is_some() || http_enabled.is_some() {
Config::generate()?;
CoreManager::global().run_core().await?;
}
if auto_launch.is_some() {
sysopt::Sysopt::global().update_launch()?;
}

View File

@ -3,7 +3,7 @@ import { useTranslation } from "react-i18next";
import { useLockFn } from "ahooks";
import { List, ListItem, ListItemText, TextField } from "@mui/material";
import { useClashInfo } from "@/hooks/use-clash";
import { BaseDialog, DialogRef, Notice } from "@/components/base";
import { BaseDialog, DialogRef, Notice, Switch } from "@/components/base";
import { useVerge } from "@/hooks/use-verge";
import getSystem from "@/utils/get-system";
const OS = getSystem();
@ -18,26 +18,42 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
const [redirPort, setRedirPort] = useState(
verge?.verge_redir_port ?? clashInfo?.redir_port ?? 7895
);
const [redirEnabled, setRedirEnabled] = useState(
verge?.verge_redir_enabled ?? true
);
const [tproxyPort, setTproxyPort] = useState(
verge?.verge_tproxy_port ?? clashInfo?.tproxy_port ?? 7896
);
const [tproxyEnabled, setTproxyEnabled] = useState(
verge?.verge_tproxy_enabled ?? true
);
const [mixedPort, setMixedPort] = useState(
verge?.verge_mixed_port ?? clashInfo?.mixed_port ?? 7897
);
const [socksPort, setSocksPort] = useState(
verge?.verge_socks_port ?? clashInfo?.socks_port ?? 7898
);
const [socksEnabled, setSocksEnabled] = useState(
verge?.verge_socks_enabled ?? true
);
const [port, setPort] = useState(
verge?.verge_port ?? clashInfo?.port ?? 7899
);
const [httpEnabled, setHttpEnabled] = useState(
verge?.verge_http_enabled ?? true
);
useImperativeHandle(ref, () => ({
open: () => {
if (verge?.verge_redir_port) setRedirPort(verge?.verge_redir_port);
setRedirEnabled(verge?.verge_redir_enabled ?? true);
if (verge?.verge_tproxy_port) setTproxyPort(verge?.verge_tproxy_port);
setTproxyEnabled(verge?.verge_tproxy_enabled ?? true);
if (verge?.verge_mixed_port) setMixedPort(verge?.verge_mixed_port);
if (verge?.verge_socks_port) setSocksPort(verge?.verge_socks_port);
setSocksEnabled(verge?.verge_socks_enabled ?? true);
if (verge?.verge_port) setPort(verge?.verge_port);
setHttpEnabled(verge?.verge_http_enabled ?? true);
setOpen(true);
},
close: () => setOpen(false),
@ -49,7 +65,11 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
tproxyPort === verge?.verge_tproxy_port &&
mixedPort === verge?.verge_mixed_port &&
socksPort === verge?.verge_socks_port &&
port === verge?.verge_port
port === verge?.verge_port &&
redirEnabled === verge?.verge_redir_enabled &&
tproxyEnabled === verge?.verge_tproxy_enabled &&
socksEnabled === verge?.verge_socks_enabled &&
httpEnabled === verge?.verge_http_enabled
) {
setOpen(false);
return;
@ -77,10 +97,12 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
if (OS !== "windows") {
await patchInfo({ "redir-port": redirPort });
await patchVerge({ verge_redir_port: redirPort });
await patchVerge({ verge_redir_enabled: redirEnabled });
}
if (OS === "linux") {
await patchInfo({ "tproxy-port": tproxyPort });
await patchVerge({ verge_tproxy_port: tproxyPort });
await patchVerge({ verge_tproxy_enabled: tproxyEnabled });
}
await patchInfo({ "mixed-port": mixedPort });
await patchInfo({ "socks-port": socksPort });
@ -88,6 +110,8 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
await patchVerge({ verge_mixed_port: mixedPort });
await patchVerge({ verge_socks_port: socksPort });
await patchVerge({ verge_port: port });
await patchVerge({ verge_socks_enabled: socksEnabled });
await patchVerge({ verge_http_enabled: httpEnabled });
setOpen(false);
Notice.success("Change Clash port successfully!", 1000);
} catch (err: any) {
@ -107,35 +131,6 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
onOk={onSave}
>
<List>
{OS !== "windows" && (
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Redir Port" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 135 }}
value={redirPort}
onChange={(e) =>
setRedirPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
/>
</ListItem>
)}
{OS === "linux" && (
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Tproxy Port" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 135 }}
value={tproxyPort}
onChange={(e) =>
setTproxyPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
/>
</ListItem>
)}
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Mixed Port" />
<TextField
@ -158,6 +153,17 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
onChange={(e) =>
setSocksPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
InputProps={{
sx: { pr: 1 },
endAdornment: (
<Switch
checked={socksEnabled}
onChange={(_, c) => {
setSocksEnabled(c);
}}
/>
),
}}
/>
</ListItem>
<ListItem sx={{ padding: "5px 2px" }}>
@ -170,8 +176,69 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
onChange={(e) =>
setPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
InputProps={{
sx: { pr: 1 },
endAdornment: (
<Switch
checked={httpEnabled}
onChange={(_, c) => {
setHttpEnabled(c);
}}
/>
),
}}
/>
</ListItem>
{OS !== "windows" && (
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Redir Port" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 135 }}
value={redirPort}
onChange={(e) =>
setRedirPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
InputProps={{
sx: { pr: 1 },
endAdornment: (
<Switch
checked={redirEnabled}
onChange={(_, c) => {
setRedirEnabled(c);
}}
/>
),
}}
/>
</ListItem>
)}
{OS === "linux" && (
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Tproxy Port" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 135 }}
value={tproxyPort}
onChange={(e) =>
setTproxyPort(+e.target.value?.replace(/\D+/, "").slice(0, 5))
}
InputProps={{
sx: { pr: 1 },
endAdornment: (
<Switch
checked={tproxyEnabled}
onChange={(_, c) => {
setTproxyEnabled(c);
}}
/>
),
}}
/>
</ListItem>
)}
</List>
</BaseDialog>
);

View File

@ -221,6 +221,10 @@ interface IVergeConfig {
verge_redir_port?: number;
verge_tproxy_port?: number;
verge_port?: number;
verge_redir_enabled?: boolean;
verge_tproxy_enabled?: boolean;
verge_socks_enabled?: boolean;
verge_http_enabled?: boolean;
enable_proxy_guard?: boolean;
proxy_guard_duration?: number;
system_proxy_bypass?: string;