添加代理主机的设置,允许代理设置为其他IP(非127.0.0.1) (#2963)

允许下拉选择ip地址(支持IPv6)、localhost、以及当前系统的主机名,同时兼容手工输入
This commit is contained in:
逐雁南飛
2025-04-16 10:22:53 +08:00
committed by Tunglies
parent 1282cc56bf
commit b70cad537c
17 changed files with 223 additions and 21 deletions

13
src-tauri/Cargo.lock generated
View File

@@ -1056,6 +1056,7 @@ dependencies = [
"dirs 6.0.0",
"dunce",
"futures",
"gethostname 1.0.1",
"getrandom 0.3.2",
"image",
"imageproc",
@@ -2410,6 +2411,16 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "gethostname"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed7131e57abbde63513e0e6636f76668a1ca9798dcae2df4e283cae9ee83859e"
dependencies = [
"rustix 1.0.3",
"windows-targets 0.52.6",
]
[[package]]
name = "getrandom"
version = "0.1.16"
@@ -9288,7 +9299,7 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12"
dependencies = [
"gethostname",
"gethostname 0.4.3",
"rustix 0.38.44",
"x11rb-protocol",
]

View File

@@ -75,6 +75,7 @@ mihomo_api = { path = "src_crates/crate_mihomo_api" }
ab_glyph = "0.2.29"
tungstenite = "0.26.2"
libc = "0.2"
gethostname = "1.0.1"
[target.'cfg(windows)'.dependencies]
runas = "=1.2.0"

View File

@@ -31,6 +31,25 @@ pub fn get_auto_proxy() -> CmdResult<Mapping> {
Ok(map)
}
/// 获取系统主机名
#[tauri::command]
pub fn get_system_hostname() -> CmdResult<String> {
use gethostname::gethostname;
// 获取系统主机名处理可能的非UTF-8字符
let hostname = match gethostname().into_string() {
Ok(name) => name,
Err(os_string) => {
// 对于包含非UTF-8的主机名使用调试格式化
let fallback = format!("{:?}", os_string);
// 去掉可能存在的引号
fallback.trim_matches('"').to_string()
}
};
Ok(hostname)
}
/// 获取网络接口列表
#[tauri::command]
pub fn get_network_interfaces() -> Vec<String> {

View File

@@ -88,6 +88,9 @@ pub struct IVerge {
/// pac script content
pub pac_file_content: Option<String>,
/// proxy host address
pub proxy_host: Option<String>,
/// theme setting
pub theme_setting: Option<IVergeTheme>,
@@ -275,6 +278,7 @@ impl IVerge {
enable_system_proxy: Some(false),
proxy_auto_config: Some(false),
pac_file_content: Some(DEFAULT_PAC.into()),
proxy_host: Some("127.0.0.1".into()),
enable_random_port: Some(false),
#[cfg(not(target_os = "windows"))]
verge_redir_port: Some(7895),
@@ -368,7 +372,7 @@ impl IVerge {
patch!(proxy_guard_duration);
patch!(proxy_auto_config);
patch!(pac_file_content);
patch!(proxy_host);
patch!(theme_setting);
patch!(web_ui_list);
patch!(clash_core);
@@ -452,6 +456,7 @@ pub struct IVergeResponse {
pub proxy_guard_duration: Option<u64>,
pub proxy_auto_config: Option<bool>,
pub pac_file_content: Option<String>,
pub proxy_host: Option<String>,
pub theme_setting: Option<IVergeTheme>,
pub web_ui_list: Option<Vec<String>>,
pub clash_core: Option<String>,
@@ -520,6 +525,7 @@ impl From<IVerge> for IVergeResponse {
proxy_guard_duration: verge.proxy_guard_duration,
proxy_auto_config: verge.proxy_auto_config,
pac_file_content: verge.pac_file_content,
proxy_host: verge.proxy_host,
theme_setting: verge.theme_setting,
web_ui_list: verge.web_ui_list,
clash_core: verge.clash_core,

View File

@@ -76,12 +76,13 @@ impl Sysopt {
.unwrap_or(Config::clash().data().get_mixed_port());
let pac_port = IVerge::get_singleton_port();
let (sys_enable, pac_enable) = {
let (sys_enable, pac_enable, proxy_host) = {
let verge = Config::verge();
let verge = verge.latest();
(
verge.enable_system_proxy.unwrap_or(false),
verge.proxy_auto_config.unwrap_or(false),
verge.proxy_host.clone().unwrap_or_else(|| String::from("127.0.0.1")),
)
};
@@ -89,13 +90,13 @@ impl Sysopt {
{
let mut sys = Sysproxy {
enable: false,
host: String::from("127.0.0.1"),
host: proxy_host.clone(),
port,
bypass: get_bypass(),
};
let mut auto = Autoproxy {
enable: false,
url: format!("http://127.0.0.1:{pac_port}/commands/pac"),
url: format!("http://{}:{}/commands/pac", proxy_host, pac_port),
};
if !sys_enable {
@@ -139,7 +140,7 @@ impl Sysopt {
let shell = app_handle.shell();
let output = if pac_enable {
let address = format!("http://{}:{}/commands/pac", "127.0.0.1", pac_port);
let address = format!("http://{}:{}/commands/pac", proxy_host, pac_port);
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["pac", address.as_str()])
@@ -148,7 +149,7 @@ impl Sysopt {
.unwrap();
output
} else {
let address = format!("{}:{}", "127.0.0.1", port);
let address = format!("{}:{}", proxy_host, port);
let bypass = get_bypass();
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
@@ -256,7 +257,7 @@ impl Sysopt {
loop {
sleep(Duration::from_secs(wait_secs)).await;
let (enable, guard, guard_duration, pac) = {
let (enable, guard, guard_duration, pac, proxy_host) = {
let verge = Config::verge();
let verge = verge.latest();
(
@@ -264,6 +265,7 @@ impl Sysopt {
verge.enable_proxy_guard.unwrap_or(false),
verge.proxy_guard_duration.unwrap_or(10),
verge.proxy_auto_config.unwrap_or(false),
verge.proxy_host.clone().unwrap_or_else(|| String::from("127.0.0.1")),
)
};
@@ -303,13 +305,13 @@ impl Sysopt {
if pac {
let autoproxy = Autoproxy {
enable: true,
url: format!("http://127.0.0.1:{pac_port}/commands/pac"),
url: format!("http://{}:{}/commands/pac", proxy_host, pac_port),
};
logging_error!(Type::System, true, autoproxy.set_auto_proxy());
} else {
let sysproxy = Sysproxy {
enable: true,
host: "127.0.0.1".into(),
host: proxy_host.into(),
port,
bypass: get_bypass(),
};
@@ -333,7 +335,7 @@ impl Sysopt {
let shell = app_handle.shell();
let output = if pac {
let address = format!("http://{}:{}/commands/pac", "127.0.0.1", pac_port);
let address = format!("http://{}:{}/commands/pac", proxy_host, pac_port);
shell
.command(sysproxy_exe.as_path().to_str().unwrap())
@@ -342,7 +344,7 @@ impl Sysopt {
.await
.unwrap()
} else {
let address = format!("{}:{}", "127.0.0.1", port);
let address = format!("{}:{}", proxy_host, port);
let bypass = get_bypass();
shell

View File

@@ -63,9 +63,14 @@ pub fn toggle_tun_mode(not_save_file: Option<bool>) {
/// Copy proxy environment variables to clipboard
pub fn copy_clash_env() {
// 从环境变量获取IP地址默认127.0.0.1
let clash_verge_rev_ip =
env::var("CLASH_VERGE_REV_IP").unwrap_or_else(|_| "127.0.0.1".to_string());
// 从环境变量获取IP地址如果没有则从配置中获取 proxy_host默认为 127.0.0.1
let clash_verge_rev_ip = env::var("CLASH_VERGE_REV_IP").unwrap_or_else(|_| {
Config::verge()
.latest()
.proxy_host
.clone()
.unwrap_or_else(|| "127.0.0.1".to_string())
});
let app_handle = handle::Handle::global().app_handle().unwrap();
let port = { Config::verge().latest().verge_mixed_port.unwrap_or(7897) };

View File

@@ -156,6 +156,7 @@ pub fn run() {
cmd::open_core_dir,
cmd::get_portable_flag,
cmd::get_network_interfaces,
cmd::get_system_hostname,
cmd::restart_core,
cmd::restart_app,
// 添加新的命令