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

47 lines
1.2 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
#[cfg(target_os = "windows")]
2021-12-04 09:31:26 +03:00
use winreg::enums::*;
2021-12-06 05:31:17 +03:00
#[cfg(target_os = "windows")]
2021-12-04 09:31:26 +03:00
use winreg::RegKey;
#[derive(Debug, Deserialize, Serialize)]
pub struct ProxyConfig {
enable: u32,
server: String,
bypass: String,
}
#[cfg(target_os = "windows")]
/// Get the windows system proxy config
pub fn get_proxy_config() -> io::Result<ProxyConfig> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
KEY_READ,
)?;
Ok(ProxyConfig {
enable: cur_var.get_value("ProxyEnable")?,
server: cur_var.get_value("ProxyServer")?,
bypass: cur_var.get_value("ProxyOverride")?,
})
}
#[cfg(target_os = "windows")]
/// Set the windows system proxy config
pub fn set_proxy_config(config: &ProxyConfig) -> 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,
)?;
cur_var.set_value("ProxyEnable", &config.enable)?;
cur_var.set_value("ProxyServer", &config.server)?;
cur_var.set_value("ProxyOverride", &config.bypass)?;
Ok(())
}