This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
[package] | ||
edition = "2021" | ||
name = "library-name" | ||
name = "system-proxy" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
thiserror = "1.0.59" | ||
windows = { version = "0.56.0", features = ["Win32_Networking_WinInet", "Win32_Foundation"] } | ||
winreg = "0.52.0" | ||
|
||
[dev-dependencies] | ||
cargo-husky = { version = "1.5.0", features = ["user-hooks"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
use std::{ | ||
net::{AddrParseError, IpAddr, SocketAddr}, | ||
str::FromStr, | ||
}; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("IO error: {0}")] | ||
IOError(#[from] std::io::Error), | ||
#[error("AddrParse error: {0}")] | ||
AddrParse(#[from] AddrParseError), | ||
#[error("Missing system proxy configuration")] | ||
MissingProxyConfig, | ||
} | ||
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SystemProxy { | ||
pub enabled: bool, | ||
pub address: IpAddr, | ||
pub port: u16, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
impl SystemProxy { | ||
pub fn get_system_proxy() -> Result<Self> { | ||
let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); | ||
let key = hklm.open_subkey("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters")?; | ||
let enabled = key.get_value("EnableProxy").unwrap_or(0u32) == 1; | ||
let server: String = key.get_value("ProxyServer")?; | ||
|
||
let (host, port) = if server.is_empty() { | ||
return Err(Error::MissingProxyConfig); | ||
} else { | ||
let socket = SocketAddr::from_str(&server)?; | ||
|
||
(socket.ip(), socket.port()) | ||
}; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
Ok(Self { | ||
enabled, | ||
address: host, | ||
port, | ||
}) | ||
} | ||
} |