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 DC40170B4B for ; Fri, 14 May 2021 15:46:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 536F317CD7 for ; Fri, 14 May 2021 15:45:20 +0200 (CEST) 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 514F517CCB for ; Fri, 14 May 2021 15:45:19 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2B52D46552 for ; Fri, 14 May 2021 15:45:19 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Fri, 14 May 2021 15:44:57 +0200 Message-Id: <20210514134457.1447930-22-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210514134457.1447930-1-f.gruenbichler@proxmox.com> References: <20210514134457.1447930-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.012 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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. [client.rs, tools.rs, subscription.rs] Subject: [pbs-devel] [PATCH proxmox-backup 8/8] move SimpleHttp to proxmox_http 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: Fri, 14 May 2021 13:46:10 -0000 Signed-off-by: Fabian Grünbichler --- Notes: requires proxmox patch #10 src/acme/client.rs | 3 +- src/tools.rs | 10 +- src/tools/simple_http_client.rs | 157 -------------------------------- src/tools/subscription.rs | 2 +- 4 files changed, 8 insertions(+), 164 deletions(-) delete mode 100644 src/tools/simple_http_client.rs diff --git a/src/acme/client.rs b/src/acme/client.rs index 1a6ca46f..d1f46617 100644 --- a/src/acme/client.rs +++ b/src/acme/client.rs @@ -16,10 +16,11 @@ use proxmox_acme_rs::account::AccountData as AcmeAccountData; use proxmox_acme_rs::order::{Order, OrderData}; use proxmox_acme_rs::Request as AcmeRequest; use proxmox_acme_rs::{Account, Authorization, Challenge, Directory, Error, ErrorResponse}; +use proxmox_http::http::client::SimpleHttp; use crate::api2::types::AcmeAccountName; use crate::config::acme::account_path; -use crate::tools::{pbs_simple_http, SimpleHttp}; +use crate::tools::pbs_simple_http; /// Our on-disk format inherited from PVE's proxmox-acme code. #[derive(Deserialize, Serialize)] diff --git a/src/tools.rs b/src/tools.rs index 4253c054..a9f57b88 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -18,7 +18,11 @@ use percent_encoding::{utf8_percent_encode, AsciiSet}; pub use proxmox::tools::fd::Fd; use proxmox::tools::fs::{create_path, CreateOptions}; -use proxmox_http::http::ProxyConfig; +use proxmox_http::http::{ + client::SimpleHttp, + client::SimpleHttpOptions, + ProxyConfig, +}; pub mod acl; pub mod apt; @@ -34,10 +38,6 @@ pub mod format; pub mod fs; pub mod fuse_loop; -mod simple_http_client; -pub use simple_http_client::SimpleHttp; -pub use simple_http_client::SimpleHttpOptions; - pub mod json; pub mod logrotate; pub mod loopdev; diff --git a/src/tools/simple_http_client.rs b/src/tools/simple_http_client.rs deleted file mode 100644 index 84e9b69f..00000000 --- a/src/tools/simple_http_client.rs +++ /dev/null @@ -1,157 +0,0 @@ -use anyhow::{Error, format_err, bail}; -use std::collections::HashMap; - -use hyper::Body; -use hyper::client::{Client, HttpConnector}; -use http::{Request, Response, HeaderValue}; -use openssl::ssl::{SslConnector, SslMethod}; -use futures::*; - -use proxmox_http::http::{ - ProxyConfig, - client::HttpsConnector, -}; - -/// Options for a SimpleHttp client. -#[derive(Default)] -pub struct SimpleHttpOptions { - /// Proxy configuration - pub proxy_config: Option, - /// `User-Agent` header value, defaults to `proxmox-simple-http-client/0.1` - pub user_agent: Option, - /// TCP keepalive time, defaults to 7200 - pub tcp_keepalive: Option, -} - -impl SimpleHttpOptions { - fn get_proxy_authorization(&self) -> Option { - if let Some(ref proxy_config) = self.proxy_config { - if !proxy_config.force_connect { - return proxy_config.authorization.clone(); - } - } - - None - } -} - -/// Asyncrounous HTTP client implementation -pub struct SimpleHttp { - client: Client, - options: SimpleHttpOptions, -} - -impl SimpleHttp { - pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-simple-http-client/0.1"; - - pub fn new() -> Self { - Self::with_options(SimpleHttpOptions::default()) - } - - pub fn with_options(options: SimpleHttpOptions) -> Self { - let ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); - Self::with_ssl_connector(ssl_connector, options) - } - - pub fn with_ssl_connector(ssl_connector: SslConnector, options: SimpleHttpOptions) -> Self { - let connector = HttpConnector::new(); - let mut https = HttpsConnector::with_connector(connector, ssl_connector, options.tcp_keepalive.unwrap_or(7200)); - if let Some(ref proxy_config) = options.proxy_config { - https.set_proxy(proxy_config.clone()); - } - let client = Client::builder().build(https); - Self { client, options } - } - - pub fn set_user_agent(&mut self, user_agent: &str) -> Result<(), Error> { - self.options.user_agent = Some(user_agent.to_owned()); - Ok(()) - } - - fn add_proxy_headers(&self, request: &mut Request) -> Result<(), Error> { - if request.uri().scheme() != Some(&http::uri::Scheme::HTTPS) { - if let Some(ref authorization) = self.options.get_proxy_authorization() { - request - .headers_mut() - .insert( - http::header::PROXY_AUTHORIZATION, - HeaderValue::from_str(authorization)?, - ); - } - } - Ok(()) - } - - pub async fn request(&self, mut request: Request) -> Result, Error> { - let user_agent = if let Some(ref user_agent) = self.options.user_agent { - HeaderValue::from_str(&user_agent)? - } else { - HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)? - }; - - request.headers_mut().insert(hyper::header::USER_AGENT, user_agent); - - self.add_proxy_headers(&mut request)?; - - self.client.request(request) - .map_err(Error::from) - .await - } - - pub async fn post( - &mut self, - uri: &str, - body: Option, - content_type: Option<&str>, - ) -> Result, Error> { - - let body = if let Some(body) = body { - Body::from(body) - } else { - Body::empty() - }; - let content_type = content_type.unwrap_or("application/json"); - - let request = Request::builder() - .method("POST") - .uri(uri) - .header(hyper::header::CONTENT_TYPE, content_type) - .body(body)?; - - self.request(request).await - } - - pub async fn get_string( - &mut self, - uri: &str, - extra_headers: Option<&HashMap>, - ) -> Result { - - let mut request = Request::builder() - .method("GET") - .uri(uri); - - if let Some(hs) = extra_headers { - for (h, v) in hs.iter() { - request = request.header(h, v); - } - } - - let request = request.body(Body::empty())?; - - let res = self.request(request).await?; - - let status = res.status(); - if !status.is_success() { - bail!("Got bad status '{}' from server", status) - } - - Self::response_body_string(res).await - } - - pub async fn response_body_string(res: Response) -> Result { - let buf = hyper::body::to_bytes(res).await?; - String::from_utf8(buf.to_vec()) - .map_err(|err| format_err!("Error converting HTTP result data: {}", err)) - } -} diff --git a/src/tools/subscription.rs b/src/tools/subscription.rs index ed42e630..c549c3dd 100644 --- a/src/tools/subscription.rs +++ b/src/tools/subscription.rs @@ -10,9 +10,9 @@ use crate::config::node; use crate::tools::{ self, pbs_simple_http, - SimpleHttp, }; use proxmox::tools::fs::{replace_file, CreateOptions}; +use proxmox_http::http::client::SimpleHttp; /// How long the local key is valid for in between remote checks pub const MAX_LOCAL_KEY_AGE: i64 = 15 * 24 * 3600; -- 2.20.1