1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::{env, vec};

use url::Url;

fn get_env(name: &str) -> Option<String> {
    match env::var(name.to_ascii_lowercase()).or_else(|_| env::var(name.to_ascii_uppercase())) {
        Ok(s) => Some(s),
        Err(env::VarError::NotPresent) => None,
        Err(env::VarError::NotUnicode(_)) => {
            warn!(
                "Environment variable {} contains non-unicode characters",
                name.to_ascii_uppercase()
            );
            None
        }
    }
}

fn get_env_url(name: &str) -> Option<Url> {
    match get_env(name) {
        Some(val) if val.trim().is_empty() => None,
        Some(val) => match Url::parse(&val) {
            Ok(url) => match url.scheme() {
                "http" | "https" => Some(url),
                _ => {
                    warn!(
                        "Environment variable {} contains unsupported proxy scheme: {}",
                        name.to_ascii_uppercase(),
                        url.scheme()
                    );
                    None
                }
            },
            Err(err) => {
                warn!(
                    "Environment variable {} contains invalid URL: {}",
                    name.to_ascii_uppercase(),
                    err
                );
                None
            }
        },
        None => None,
    }
}

/// Contains proxy settings and utilities to find which proxy to use for a given URL.
#[derive(Clone, Debug)]
pub struct ProxySettings {
    http_proxy: Option<Url>,
    https_proxy: Option<Url>,
    disable_proxies: bool,
    no_proxy_hosts: Vec<String>,
}

impl ProxySettings {
    /// Get a new builder for ProxySettings.
    pub fn builder() -> ProxySettingsBuilder {
        ProxySettingsBuilder::new()
    }

    /// Get the proxy configuration from the environment using the `curl`/Unix proxy conventions.
    ///
    /// Only `ALL_PROXY`, `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` are supported.
    /// Proxies can be disabled on all requests by setting `NO_PROXY` to `*`, similar to `curl`.
    /// `HTTP_PROXY` or `HTTPS_PROXY` take precedence over values set by `ALL_PROXY` for their
    /// respective schemes.
    ///
    /// See <https://curl.se/docs/manpage.html#--noproxy>
    pub fn from_env() -> ProxySettings {
        let all_proxy = get_env_url("all_proxy");
        let http_proxy = get_env_url("http_proxy");
        let https_proxy = get_env_url("https_proxy");
        let no_proxy = get_env("no_proxy");

        let disable_proxies = no_proxy.as_deref().unwrap_or("") == "*";
        let mut no_proxy_hosts = vec![];

        if !disable_proxies {
            if let Some(no_proxy) = no_proxy {
                no_proxy_hosts.extend(no_proxy.split(',').map(|s|
                    s.trim().trim_start_matches('.').to_lowercase()));
            }
        }

        ProxySettings {
            http_proxy: http_proxy.or_else(|| all_proxy.clone()),
            https_proxy: https_proxy.or(all_proxy),
            disable_proxies,
            no_proxy_hosts,
        }
    }

    /// Get the proxy URL to use for the given URL.
    ///
    /// None is returned if there is no proxy configured for the scheme or if the hostname
    /// matches a pattern in the no proxy list.
    pub fn for_url(&self, url: &Url) -> Option<&Url> {
        if self.disable_proxies {
            return None;
        }

        if let Some(host) = url.host_str() {
            if !self.no_proxy_hosts.iter().any(|x| host.ends_with(x.to_lowercase().as_str())) {
                return match url.scheme() {
                    "http" => self.http_proxy.as_ref(),
                    "https" => self.https_proxy.as_ref(),
                    _ => None,
                };
            }
        }
        None
    }
}

/// Utility to build ProxySettings easily.
#[derive(Clone, Debug)]
pub struct ProxySettingsBuilder {
    inner: ProxySettings,
}

impl ProxySettingsBuilder {
    /// Create a new ProxySetting builder with no initial configuration.
    pub fn new() -> Self {
        ProxySettingsBuilder {
            inner: ProxySettings {
                http_proxy: None,
                https_proxy: None,
                disable_proxies: false,
                no_proxy_hosts: vec![],
            },
        }
    }

    /// Set the proxy for http requests.
    pub fn http_proxy<V>(mut self, val: V) -> Self
    where
        V: Into<Option<Url>>,
    {
        self.inner.http_proxy = val.into();
        self
    }

    /// Set the proxy for https requests.
    pub fn https_proxy<V>(mut self, val: V) -> Self
    where
        V: Into<Option<Url>>,
    {
        self.inner.https_proxy = val.into();
        self
    }

    /// Add a hostname pattern to ignore when finding the proxy to use for a URL.
    ///
    /// For instance `mycompany.local` will make requests with the hostname `mycompany.local`
    /// not go trough the proxy.
    pub fn add_no_proxy_host(mut self, pattern: impl AsRef<str>) -> Self {
        self.inner.no_proxy_hosts.push(pattern.as_ref().to_lowercase());
        self
    }

    /// Build the settings.
    pub fn build(self) -> ProxySettings {
        self.inner
    }
}

impl Default for ProxySettingsBuilder {
    fn default() -> Self {
        ProxySettingsBuilder::new()
    }
}

#[test]
fn test_proxy_for_url() {
    let s = ProxySettings {
        http_proxy: Some("http://proxy1:3128".parse().unwrap()),
        https_proxy: Some("http://proxy2:3128".parse().unwrap()),
        disable_proxies: false,
        no_proxy_hosts: vec!["reddit.com".into()],
    };

    assert_eq!(
        s.for_url(&Url::parse("http://google.ca").unwrap()),
        Some(&"http://proxy1:3128".parse().unwrap())
    );

    assert_eq!(
        s.for_url(&Url::parse("https://google.ca").unwrap()),
        Some(&"http://proxy2:3128".parse().unwrap())
    );

    assert_eq!(s.for_url(&Url::parse("https://reddit.com").unwrap()), None);
}

#[test]
fn test_proxy_for_url_disabled() {
    let s = ProxySettings {
        http_proxy: Some("http://proxy1:3128".parse().unwrap()),
        https_proxy: Some("http://proxy2:3128".parse().unwrap()),
        disable_proxies: true,
        no_proxy_hosts: vec![],
    };

    assert_eq!(s.for_url(&Url::parse("https://reddit.com").unwrap()), None);
    assert_eq!(s.for_url(&Url::parse("https://www.google.ca").unwrap()), None);
}

#[cfg(test)]
fn with_reset_proxy_vars<T>(test: T)
where
    T: FnOnce() + std::panic::UnwindSafe,
{
    use std::sync::Mutex;

    lazy_static::lazy_static! {
        static ref LOCK: Mutex<()> = Mutex::new(());
    };

    let _guard = LOCK.lock().unwrap();

    env::remove_var("ALL_PROXY");
    env::remove_var("HTTP_PROXY");
    env::remove_var("HTTPS_PROXY");
    env::remove_var("NO_PROXY");

    let result = std::panic::catch_unwind(test);

    // teardown if ever needed

    if let Err(ctx) = result {
        std::panic::resume_unwind(ctx);
    }
}

#[test]
fn test_proxy_from_env_all_proxy() {
    with_reset_proxy_vars(|| {
        env::set_var("ALL_PROXY", "http://proxy:3128");

        let s = ProxySettings::from_env();

        assert_eq!(s.http_proxy.unwrap().as_str(), "http://proxy:3128/");
        assert_eq!(s.https_proxy.unwrap().as_str(), "http://proxy:3128/");
    });
}

#[test]
fn test_proxy_from_env_override() {
    with_reset_proxy_vars(|| {
        env::set_var("ALL_PROXY", "http://proxy:3128");
        env::set_var("HTTP_PROXY", "http://proxy:3129");
        env::set_var("HTTPS_PROXY", "http://proxy:3130");

        let s = ProxySettings::from_env();

        assert_eq!(s.http_proxy.unwrap().as_str(), "http://proxy:3129/");
        assert_eq!(s.https_proxy.unwrap().as_str(), "http://proxy:3130/");
    });
}

#[test]
fn test_proxy_from_env_no_proxy_wildcard() {
    with_reset_proxy_vars(|| {
        env::set_var("NO_PROXY", "*");

        let s = ProxySettings::from_env();

        assert!(s.disable_proxies);
    });
}

#[test]
fn test_proxy_from_env_no_proxy_root_domain() {
    with_reset_proxy_vars(|| {
        env::set_var("NO_PROXY", ".myroot.com");

        let s = ProxySettings::from_env();

        let url = Url::parse("https://mysub.myroot.com").unwrap();
        assert!(s.for_url(&url).is_none());
        assert_eq!(s.no_proxy_hosts[0], "myroot.com");
    });
}

#[test]
fn test_proxy_from_env_no_proxy() {
    with_reset_proxy_vars(|| {
        env::set_var("NO_PROXY", "example.com, www.reddit.com, google.ca ");

        let s = ProxySettings::from_env();

        assert_eq!(s.no_proxy_hosts, vec!["example.com", "www.reddit.com", "google.ca"]);
    });
}