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 CEA2E8921 for ; Thu, 22 Jun 2023 11:15:37 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B3AB0264F1 for ; Thu, 22 Jun 2023 11:15:37 +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 for ; Thu, 22 Jun 2023 11:15:37 +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 C475C42A5F for ; Thu, 22 Jun 2023 11:15:36 +0200 (CEST) From: Max Carrara To: pbs-devel@lists.proxmox.com Date: Thu, 22 Jun 2023 11:15:26 +0200 Message-Id: <20230622091526.812422-4-m.carrara@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230622091526.812422-1-m.carrara@proxmox.com> References: <20230622091526.812422-1-m.carrara@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.010 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox-backup 3/3] proxy: redirect HTTP requests to HTTPS 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: Thu, 22 Jun 2023 09:15:37 -0000 Signed-off-by: Max Carrara --- src/bin/proxmox-backup-proxy.rs | 45 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index f38a02bd..a5187bcb 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -23,8 +23,8 @@ use proxmox_sys::{task_log, task_warn}; use pbs_datastore::DataStore; use proxmox_rest_server::{ - cleanup_old_tasks, cookie_from_header, rotate_task_log_archive, ApiConfig, RestEnvironment, - RestServer, WorkerTask, + cleanup_old_tasks, cookie_from_header, rotate_task_log_archive, ApiConfig, Redirector, + RestEnvironment, RestServer, WorkerTask, }; use proxmox_backup::rrd_cache::{ @@ -253,6 +253,7 @@ async fn run() -> Result<(), Error> { )?; let rest_server = RestServer::new(config); + let redirector = Redirector::new(); proxmox_rest_server::init_worker_tasks( pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(), file_opts.clone(), @@ -288,23 +289,53 @@ async fn run() -> Result<(), Error> { Ok(Value::Null) })?; - let connections = proxmox_rest_server::connection::AcceptBuilder::with_acceptor(acceptor) + let connections = proxmox_rest_server::connection::BiAcceptBuilder::with_acceptor(acceptor) .debug(debug) .rate_limiter_lookup(Arc::new(lookup_rate_limiter)) .tcp_keepalive_time(PROXMOX_BACKUP_TCP_KEEPALIVE_TIME); + let server = daemon::create_daemon( ([0, 0, 0, 0, 0, 0, 0, 0], 8007).into(), move |listener| { - let connections = connections.accept(listener); + let (secure_connections, insecure_connections) = connections.accept(listener); Ok(async { daemon::systemd_notify(daemon::SystemdNotify::Ready)?; - hyper::Server::builder(connections) + let secure_server = hyper::Server::builder(secure_connections) .serve(rest_server) .with_graceful_shutdown(proxmox_rest_server::shutdown_future()) - .map_err(Error::from) - .await + .map_err(Error::from); + + let insecure_server = hyper::Server::builder(insecure_connections) + .serve(redirector) + .with_graceful_shutdown(proxmox_rest_server::shutdown_future()) + .map_err(Error::from); + + let handles = vec![tokio::spawn(secure_server), tokio::spawn(insecure_server)]; + + let mut results: Vec> = vec![]; + + for res_handle in futures::future::join_all(handles).await.into_iter() { + let flattened_res = match res_handle { + Ok(inner) => inner, + Err(err) => Err(format_err!(err)), + }; + + results.push(flattened_res); + } + + if results.iter().any(Result::is_err) { + let cat_errors = results + .into_iter() + .filter_map(|res| res.err().map(|err| err.to_string())) + .collect::>() + .join("\n"); + + return Err(format_err!(cat_errors)); + } + + Ok(()) }) }, Some(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN), -- 2.30.2