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 0CFAB799F6 for ; Wed, 5 May 2021 11:59:04 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F2DE613274 for ; Wed, 5 May 2021 11:59:03 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id 77B321326B for ; Wed, 5 May 2021 11:59:03 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 34E8EAEB0A8; Wed, 5 May 2021 11:59:03 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 5 May 2021 11:58:59 +0200 Message-Id: <20210505095859.27699-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.389 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [http.rs] Subject: [pbs-devel] [PATH proxmox-backup] http proxy: add necessary brackets for IPv6 proxy 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, 05 May 2021 09:59:04 -0000 --- src/tools/http.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/tools/http.rs b/src/tools/http.rs index 8656e685..cfdd9b16 100644 --- a/src/tools/http.rs +++ b/src/tools/http.rs @@ -5,9 +5,9 @@ use std::collections::HashMap; use std::pin::Pin; use std::sync::Arc; -use hyper::{Uri, Body}; +use hyper::Body; use hyper::client::{Client, HttpConnector}; -use http::{Request, Response, HeaderValue}; +use http::{Uri, uri::Authority, Request, Response, HeaderValue}; use openssl::ssl::{SslConnector, SslMethod}; use futures::*; use tokio::{ @@ -28,6 +28,18 @@ use crate::tools::{ }, }; +// Build a http::uri::Authority ("host:port"), use '[..]' around IPv6 addresses +fn build_authority(host: &str, port: u16) -> Result { + let bytes = host.as_bytes(); + let len = bytes.len(); + let authority = if len > 3 && bytes.contains(&b':') && bytes[0] != b'[' && bytes[len-1] != b']' { + format!("[{}]:{}", host, port).parse()? + } else { + format!("{}:{}", host, port).parse()? + }; + Ok(authority) +} + /// HTTP Proxy Configuration #[derive(Clone)] pub struct ProxyConfig { @@ -329,10 +341,14 @@ impl hyper::service::Service for HttpsConnector { let use_connect = is_https || proxy.force_connect; - let proxy_url = format!("{}:{}", proxy.host, proxy.port); + let proxy_authority = match build_authority(&proxy.host, proxy.port) { + Ok(authority) => authority, + Err(err) => return futures::future::err(err).boxed(), + }; + let proxy_uri = match Uri::builder() .scheme("http") - .authority(proxy_url.as_str()) + .authority(proxy_authority.as_str()) .path_and_query("/") .build() { @@ -348,7 +364,7 @@ impl hyper::service::Service for HttpsConnector { let mut tcp_stream = connector .call(proxy_uri) .await - .map_err(|err| format_err!("error connecting to {} - {}", proxy_url, err))?; + .map_err(|err| format_err!("error connecting to {} - {}", proxy_authority, err))?; let _ = set_tcp_keepalive(tcp_stream.as_raw_fd(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME); @@ -374,7 +390,7 @@ impl hyper::service::Service for HttpsConnector { let tcp_stream = connector .call(proxy_uri) .await - .map_err(|err| format_err!("error connecting to {} - {}", proxy_url, err))?; + .map_err(|err| format_err!("error connecting to {} - {}", proxy_authority, err))?; let _ = set_tcp_keepalive(tcp_stream.as_raw_fd(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME); -- 2.20.1