refactor: impl structs methods

This commit is contained in:
GyDi
2022-01-07 23:29:20 +08:00
parent 72ff261fe3
commit e369311fc2
21 changed files with 676 additions and 762 deletions

View File

@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use std::io;
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>";
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SysProxyConfig {
pub enable: bool,
@@ -18,16 +20,21 @@ impl Default for SysProxyConfig {
}
}
pub 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>";
#[cfg(target_os = "windows")]
mod win {
use super::*;
use winreg::enums::*;
use winreg::RegKey;
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()),
}
}
/// Get the windows system proxy config
pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
#[cfg(target_os = "windows")]
pub fn get_sys() -> io::Result<Self> {
use winreg::enums::*;
use winreg::RegKey;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
@@ -41,43 +48,38 @@ mod win {
})
}
#[cfg(target_os = "windows")]
/// Set the windows system proxy config
pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
pub fn set_sys(&self) -> io::Result<()> {
use winreg::enums::*;
use winreg::RegKey;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
KEY_SET_VALUE,
)?;
let enable: u32 = if config.enable { 1u32 } else { 0u32 };
let enable: u32 = if self.enable { 1u32 } else { 0u32 };
cur_var.set_value("ProxyEnable", &enable)?;
cur_var.set_value("ProxyServer", &config.server)?;
cur_var.set_value("ProxyOverride", &config.bypass)?;
Ok(())
cur_var.set_value("ProxyServer", &self.server)?;
cur_var.set_value("ProxyOverride", &self.bypass)
}
}
#[cfg(target_os = "macos")]
mod macos {
use super::*;
// #[cfg(target_os = "macos")]
// mod macos {
// use super::*;
pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
Ok(SysProxyConfig {
enable: false,
server: "server".into(),
bypass: "bypass".into(),
})
}
// pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
// Ok(SysProxyConfig {
// enable: false,
// server: "server".into(),
// bypass: "bypass".into(),
// })
// }
pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
Ok(())
}
}
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "macos")]
pub use macos::*;
// pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
// Ok(())
// }
// }