public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 2/2] http: add helper to parse proxy configuration
Date: Wed, 28 Apr 2021 10:26:30 +0200	[thread overview]
Message-ID: <20210428082631.8295-2-dietmar@proxmox.com> (raw)
In-Reply-To: <20210428082631.8295-1-dietmar@proxmox.com>

---
 src/tools/http.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/src/tools/http.rs b/src/tools/http.rs
index e36e6c51..f4ccefc9 100644
--- a/src/tools/http.rs
+++ b/src/tools/http.rs
@@ -37,6 +37,68 @@ pub struct ProxyConfig {
     pub force_connect: bool,
 }
 
+impl ProxyConfig {
+
+    /// Parse proxy config from ALL_PROXY environment var
+    pub fn from_proxy_env() -> Result<Option<ProxyConfig>, Error> {
+
+        // We only support/use ALL_PROXY environment
+
+        match std::env::var_os("ALL_PROXY") {
+            None => return Ok(None),
+            Some(all_proxy) => {
+                let all_proxy = match all_proxy.to_str() {
+                    Some(s) => String::from(s),
+                    None => bail!("non UTF-8 content in env ALL_PROXY"),
+                };
+                if all_proxy.is_empty() {
+                    return Ok(None);
+                }
+                let config = Self::parse_proxy_url(&all_proxy)?;
+                Ok(Some(config))
+            }
+        }
+    }
+
+    /// Parse proxy configuration string [http://]<host>[:port]
+    ///
+    /// Default port is 1080 (like curl)
+    pub fn parse_proxy_url(http_proxy: &str) -> Result<ProxyConfig, Error> {
+        proxmox::try_block!({
+            let proxy_uri: Uri = http_proxy.parse()?;
+            let proxy_authority = match proxy_uri.authority() {
+                Some(authority) => authority,
+                None => bail!("missing proxy authority"),
+            };
+            let host = proxy_authority.host().to_owned();
+            let port = match proxy_uri.port() {
+                Some(port) => port.as_u16(),
+                None => 1080, // CURL default port
+            };
+
+            match proxy_uri.scheme_str() {
+                Some("http") => { /* Ok */ }
+                Some(scheme) => bail!("unsupported proxy scheme '{}'", scheme),
+                None => { /* assume HTTP */ }
+            }
+
+            let authority_vec: Vec<&str> = proxy_authority.as_str().rsplitn(2, '@').collect();
+            let authorization = if authority_vec.len() == 2 {
+                Some(format!("Basic {}", base64::encode(authority_vec[1])))
+            } else {
+                None
+            };
+
+            Ok(ProxyConfig {
+                host,
+                port,
+                authorization,
+                force_connect: false,
+            })
+        }).map_err(|err| format_err!("parse_proxy_url failed: {}", err))
+    }
+}
+
 /// Asyncrounous HTTP client implementation
 pub struct SimpleHttp {
     client: Client<HttpsConnector, Body>,
-- 
2.20.1




      reply	other threads:[~2021-04-28  8:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-28  8:26 [pbs-devel] [PATCH proxmox-backup 1/2] HttpsConnector: add proxy authorization support Dietmar Maurer
2021-04-28  8:26 ` Dietmar Maurer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210428082631.8295-2-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal