From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 15650776E2 for ; Wed, 28 Apr 2021 10:26:48 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1210C27D78 for ; Wed, 28 Apr 2021 10:26:48 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id E055C27D6D for ; Wed, 28 Apr 2021 10:26:46 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 067B5AEB21B; Wed, 28 Apr 2021 10:26:40 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 28 Apr 2021 10:26:30 +0200 Message-Id: <20210428082631.8295-2-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210428082631.8295-1-dietmar@proxmox.com> References: <20210428082631.8295-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 AWL -0.147 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RDNS_NONE 1.274 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 2/2] http: add helper to parse proxy configuration X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Apr 2021 08:26:48 -0000 --- 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, 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://][:port] + /// + /// Default port is 1080 (like curl) + pub fn parse_proxy_url(http_proxy: &str) -> Result { + 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, -- 2.20.1