|
| 1 | +//! Cross-platform access to system proxy settings. |
| 2 | +//! |
| 3 | +//! The API is intentionally small and capability-oriented: you describe the |
| 4 | +//! desired [`ProxyState`] and the crate applies it using the native facilities |
| 5 | +//! of the underlying platform. Only macOS is implemented right now; other |
| 6 | +//! platforms fall back to [`Error::Unsupported`]. |
| 7 | +//! |
| 8 | +//! The abstractions are designed to be forward-compatible with additional |
| 9 | +//! platforms such as Linux, Windows, Android, iOS, and OpenHarmony. |
| 10 | +
|
| 11 | +mod error; |
| 12 | +mod sys; |
| 13 | + |
| 14 | +pub use crate::error::{Error, Result}; |
| 15 | + |
| 16 | +/// Uniform representation of a proxy endpoint. |
| 17 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 18 | +pub struct ProxyEndpoint { |
| 19 | + /// Hostname or IP of the proxy. |
| 20 | + pub host: String, |
| 21 | + /// TCP port on which the proxy listens. |
| 22 | + pub port: u16, |
| 23 | + /// Optional credentials when the proxy requires authentication. |
| 24 | + pub credentials: Option<Credentials>, |
| 25 | +} |
| 26 | + |
| 27 | +impl ProxyEndpoint { |
| 28 | + pub fn new(host: impl Into<String>, port: u16) -> Self { |
| 29 | + Self { |
| 30 | + host: host.into(), |
| 31 | + port, |
| 32 | + credentials: None, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub fn with_credentials( |
| 37 | + host: impl Into<String>, |
| 38 | + port: u16, |
| 39 | + username: impl Into<String>, |
| 40 | + password: Option<String>, |
| 41 | + ) -> Self { |
| 42 | + Self { |
| 43 | + host: host.into(), |
| 44 | + port, |
| 45 | + credentials: Some(Credentials { |
| 46 | + username: username.into(), |
| 47 | + password, |
| 48 | + }), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Credentials for proxies that require authentication. |
| 54 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 55 | +pub struct Credentials { |
| 56 | + pub username: String, |
| 57 | + pub password: Option<String>, |
| 58 | +} |
| 59 | + |
| 60 | +/// List of hosts, domains, or CIDRs that should bypass the proxy. |
| 61 | +#[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 62 | +pub struct BypassList { |
| 63 | + pub entries: Vec<String>, |
| 64 | +} |
| 65 | + |
| 66 | +impl BypassList { |
| 67 | + pub fn new(entries: impl Into<Vec<String>>) -> Self { |
| 68 | + Self { |
| 69 | + entries: entries.into(), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Proxy configuration for manual modes. |
| 75 | +#[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 76 | +pub struct ProxySettings { |
| 77 | + pub http: Option<ProxyEndpoint>, |
| 78 | + pub https: Option<ProxyEndpoint>, |
| 79 | + pub socks: Option<ProxyEndpoint>, |
| 80 | + pub bypass: BypassList, |
| 81 | +} |
| 82 | + |
| 83 | +/// Modes supported by the abstraction layer. |
| 84 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 85 | +pub enum ProxyMode { |
| 86 | + /// No proxy usage. |
| 87 | + Direct, |
| 88 | + /// User-specified endpoints. |
| 89 | + Manual(ProxySettings), |
| 90 | + /// Proxy auto-configuration (PAC) script URL. |
| 91 | + AutoConfigUrl { |
| 92 | + url: String, |
| 93 | + bypass: BypassList, |
| 94 | + }, |
| 95 | + /// WPAD / automatic discovery. |
| 96 | + AutoDiscovery { |
| 97 | + bypass: BypassList, |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +/// High-level proxy state exposed to callers. |
| 102 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 103 | +pub struct ProxyState { |
| 104 | + pub mode: ProxyMode, |
| 105 | +} |
| 106 | + |
| 107 | +impl ProxyState { |
| 108 | + pub fn direct() -> Self { |
| 109 | + Self { |
| 110 | + mode: ProxyMode::Direct, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn manual(settings: ProxySettings) -> Self { |
| 115 | + Self { |
| 116 | + mode: ProxyMode::Manual(settings), |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + pub fn pac(url: impl Into<String>, bypass: BypassList) -> Self { |
| 121 | + Self { |
| 122 | + mode: ProxyMode::AutoConfigUrl { |
| 123 | + url: url.into(), |
| 124 | + bypass, |
| 125 | + }, |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + pub fn auto_discovery(bypass: BypassList) -> Self { |
| 130 | + Self { |
| 131 | + mode: ProxyMode::AutoDiscovery { bypass }, |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// Entry point for interacting with system proxy settings. |
| 137 | +pub struct ProxyManager { |
| 138 | + inner: sys::Manager, |
| 139 | +} |
| 140 | + |
| 141 | +impl ProxyManager { |
| 142 | + /// Constructs a manager bound to the current platform. |
| 143 | + pub fn new() -> Result<Self> { |
| 144 | + Ok(Self { |
| 145 | + inner: sys::Manager::new()?, |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + /// Reads the current proxy state from the system. |
| 150 | + pub fn current(&self) -> Result<ProxyState> { |
| 151 | + self.inner.current() |
| 152 | + } |
| 153 | + |
| 154 | + /// Applies the given proxy state to the system. |
| 155 | + pub fn apply(&self, state: ProxyState) -> Result<()> { |
| 156 | + self.inner.apply(state) |
| 157 | + } |
| 158 | + |
| 159 | + /// Convenience helper to disable all proxies. |
| 160 | + pub fn disable(&self) -> Result<()> { |
| 161 | + self.apply(ProxyState::direct()) |
| 162 | + } |
| 163 | +} |
0 commit comments