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 D3F147BF82 for ; Wed, 3 Nov 2021 13:42:59 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D043013A29 for ; Wed, 3 Nov 2021 13:42:59 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 firstgate.proxmox.com (Proxmox) with ESMTPS id CDB2613A1F for ; Wed, 3 Nov 2021 13:42:58 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A684E458E3; Wed, 3 Nov 2021 13:42:58 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 3 Nov 2021 13:42:47 +0100 Message-Id: <20211103124247.1727711-3-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211103124247.1727711-1-dietmar@proxmox.com> References: <20211103124247.1727711-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.475 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far 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. [connector.rs] Subject: [pbs-devel] [PATCH proxmox 3/3] HttpsConnector: use RateLimitedStream 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, 03 Nov 2021 12:42:59 -0000 So that we can limit used bandwidth. Signed-off-by: Dietmar Maurer --- proxmox-http/src/client/connector.rs | 51 ++++++++++++++++--- .../src/client/rate_limited_stream.rs | 8 +++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/proxmox-http/src/client/connector.rs b/proxmox-http/src/client/connector.rs index acbb992..71704d5 100644 --- a/proxmox-http/src/client/connector.rs +++ b/proxmox-http/src/client/connector.rs @@ -1,14 +1,14 @@ use anyhow::{bail, format_err, Error}; use std::os::unix::io::AsRawFd; use std::pin::Pin; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::task::{Context, Poll}; use futures::*; use http::Uri; use hyper::client::HttpConnector; use openssl::ssl::SslConnector; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt}; +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use tokio::net::TcpStream; use tokio_openssl::SslStream; @@ -18,12 +18,16 @@ use crate::proxy_config::ProxyConfig; use crate::tls::MaybeTlsStream; use crate::uri::build_authority; +use super::{RateLimiter, RateLimitedStream}; + #[derive(Clone)] pub struct HttpsConnector { connector: HttpConnector, ssl_connector: Arc, proxy: Option, tcp_keepalive: u32, + read_limiter: Option>>, + write_limiter: Option>>, } impl HttpsConnector { @@ -38,6 +42,8 @@ impl HttpsConnector { ssl_connector: Arc::new(ssl_connector), proxy: None, tcp_keepalive, + read_limiter: None, + write_limiter: None, } } @@ -45,13 +51,21 @@ impl HttpsConnector { self.proxy = Some(proxy); } - async fn secure_stream( - tcp_stream: TcpStream, + pub fn set_read_limiter(&mut self, limiter: Option>>) { + self.read_limiter = limiter; + } + + pub fn set_write_limiter(&mut self, limiter: Option>>) { + self.write_limiter = limiter; + } + + async fn secure_stream( + tcp_stream: S, ssl_connector: &SslConnector, host: &str, - ) -> Result, Error> { + ) -> Result, Error> { let config = ssl_connector.configure()?; - let mut conn: SslStream = SslStream::new(config.into_ssl(host)?, tcp_stream)?; + let mut conn: SslStream = SslStream::new(config.into_ssl(host)?, tcp_stream)?; Pin::new(&mut conn).connect().await?; Ok(MaybeTlsStream::Secured(conn)) } @@ -107,7 +121,7 @@ impl HttpsConnector { } impl hyper::service::Service for HttpsConnector { - type Response = MaybeTlsStream; + type Response = MaybeTlsStream>; type Error = Error; #[allow(clippy::type_complexity)] type Future = @@ -129,6 +143,9 @@ impl hyper::service::Service for HttpsConnector { }; let port = dst.port_u16().unwrap_or(if is_https { 443 } else { 80 }); let keepalive = self.tcp_keepalive; + let read_limiter = self.read_limiter.clone(); + let write_limiter = self.write_limiter.clone(); + if let Some(ref proxy) = self.proxy { let use_connect = is_https || proxy.force_connect; @@ -152,12 +169,18 @@ impl hyper::service::Service for HttpsConnector { if use_connect { async move { - let mut tcp_stream = connector.call(proxy_uri).await.map_err(|err| { + let tcp_stream = connector.call(proxy_uri).await.map_err(|err| { format_err!("error connecting to {} - {}", proxy_authority, err) })?; let _ = set_tcp_keepalive(tcp_stream.as_raw_fd(), keepalive); + let mut tcp_stream = RateLimitedStream::with_limiter( + tcp_stream, + read_limiter, + write_limiter, + ); + let mut connect_request = format!("CONNECT {0}:{1} HTTP/1.1\r\n", host, port); if let Some(authorization) = authorization { connect_request @@ -185,6 +208,12 @@ impl hyper::service::Service for HttpsConnector { let _ = set_tcp_keepalive(tcp_stream.as_raw_fd(), keepalive); + let tcp_stream = RateLimitedStream::with_limiter( + tcp_stream, + read_limiter, + write_limiter, + ); + Ok(MaybeTlsStream::Proxied(tcp_stream)) } .boxed() @@ -199,6 +228,12 @@ impl hyper::service::Service for HttpsConnector { let _ = set_tcp_keepalive(tcp_stream.as_raw_fd(), keepalive); + let tcp_stream = RateLimitedStream::with_limiter( + tcp_stream, + read_limiter, + write_limiter, + ); + if is_https { Self::secure_stream(tcp_stream, &ssl_connector, &host).await } else { diff --git a/proxmox-http/src/client/rate_limited_stream.rs b/proxmox-http/src/client/rate_limited_stream.rs index 8b4123f..d21f55c 100644 --- a/proxmox-http/src/client/rate_limited_stream.rs +++ b/proxmox-http/src/client/rate_limited_stream.rs @@ -7,6 +7,7 @@ use std::io::IoSlice; use futures::Future; use tokio::io::{ReadBuf, AsyncRead, AsyncWrite}; use tokio::time::Sleep; +use hyper::client::connect::{Connection, Connected}; use std::task::{Context, Poll}; @@ -174,3 +175,10 @@ impl AsyncRead for RateLimitedStream { } } + +// we need this for the hyper http client +impl Connection for RateLimitedStream { + fn connected(&self) -> Connected { + self.stream.connected() + } +} -- 2.30.2