clash-verge/src-tauri/src/utils/sysopt.rs

86 lines
2.3 KiB
Rust
Raw Normal View History

2021-12-04 09:31:26 +03:00
use serde::{Deserialize, Serialize};
2021-12-09 18:26:11 +03:00
use std::io;
2021-12-06 05:31:17 +03:00
2022-01-07 18:29:20 +03:00
static DEFAULT_BYPASS: &str = "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;<local>";
2021-12-26 21:29:28 +03:00
#[derive(Debug, Deserialize, Serialize, Clone)]
2021-12-12 13:02:41 +03:00
pub struct SysProxyConfig {
2021-12-16 21:15:40 +03:00
pub enable: bool,
pub server: String,
pub bypass: String,
2021-12-04 09:31:26 +03:00
}
2021-12-26 21:29:28 +03:00
impl Default for SysProxyConfig {
fn default() -> Self {
SysProxyConfig {
enable: false,
server: String::from(""),
bypass: String::from(""),
}
}
}
2022-01-07 18:29:20 +03:00
impl SysProxyConfig {
pub fn new(enable: bool, port: String, bypass: Option<String>) -> Self {
SysProxyConfig {
enable,
server: format!("127.0.0.1:{}", port),
bypass: bypass.unwrap_or(DEFAULT_BYPASS.into()),
}
}
2021-12-12 13:02:41 +03:00
/// Get the windows system proxy config
2022-01-07 18:29:20 +03:00
#[cfg(target_os = "windows")]
pub fn get_sys() -> io::Result<Self> {
use winreg::enums::*;
use winreg::RegKey;
2021-12-12 13:02:41 +03:00
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
KEY_READ,
)?;
Ok(SysProxyConfig {
enable: cur_var.get_value::<u32, _>("ProxyEnable")? == 1u32,
server: cur_var.get_value("ProxyServer")?,
bypass: cur_var.get_value("ProxyOverride")?,
})
}
2022-01-07 18:29:20 +03:00
#[cfg(target_os = "windows")]
2021-12-12 13:02:41 +03:00
/// Set the windows system proxy config
2022-01-07 18:29:20 +03:00
pub fn set_sys(&self) -> io::Result<()> {
use winreg::enums::*;
use winreg::RegKey;
2021-12-12 13:02:41 +03:00
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
KEY_SET_VALUE,
)?;
2022-01-07 18:29:20 +03:00
let enable: u32 = if self.enable { 1u32 } else { 0u32 };
2021-12-12 13:02:41 +03:00
cur_var.set_value("ProxyEnable", &enable)?;
2022-01-07 18:29:20 +03:00
cur_var.set_value("ProxyServer", &self.server)?;
cur_var.set_value("ProxyOverride", &self.bypass)
2021-12-12 13:02:41 +03:00
}
2021-12-04 09:31:26 +03:00
}
2022-01-07 18:29:20 +03:00
// #[cfg(target_os = "macos")]
// mod macos {
// use super::*;
2021-12-12 13:02:41 +03:00
2022-01-07 18:29:20 +03:00
// pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
// Ok(SysProxyConfig {
// enable: false,
// server: "server".into(),
// bypass: "bypass".into(),
// })
// }
2021-12-12 13:02:41 +03:00
2022-01-07 18:29:20 +03:00
// pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
// Ok(())
// }
// }