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
|
|
|
|
2021-12-04 09:31:26 +03:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
2021-12-12 13:02:41 +03:00
|
|
|
mod win {
|
|
|
|
use super::*;
|
|
|
|
use winreg::enums::*;
|
|
|
|
use winreg::RegKey;
|
|
|
|
|
|
|
|
/// Get the windows system proxy config
|
|
|
|
pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
|
|
|
|
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")?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the windows system proxy config
|
|
|
|
pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
|
|
|
|
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 };
|
|
|
|
|
|
|
|
cur_var.set_value("ProxyEnable", &enable)?;
|
|
|
|
cur_var.set_value("ProxyServer", &config.server)?;
|
|
|
|
cur_var.set_value("ProxyOverride", &config.bypass)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
}
|
|
|
|
|
2021-12-12 13:02:41 +03:00
|
|
|
#[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 set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
}
|
2021-12-12 13:02:41 +03:00
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
pub use win::*;
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub use macos::*;
|