Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed May 6, 2024
1 parent 9cee94e commit b8f0c62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
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"] }
50 changes: 41 additions & 9 deletions src/lib.rs
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,
})
}
}

0 comments on commit b8f0c62

Please sign in to comment.