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 6651FE244 for ; Tue, 18 Jul 2023 08:00:17 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 431E116037 for ; Tue, 18 Jul 2023 07:59:47 +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 ; Tue, 18 Jul 2023 07:59:46 +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 D179142F2B for ; Tue, 18 Jul 2023 07:59:45 +0200 (CEST) Message-ID: <635eb4e2-f6b7-9bfd-f446-9f76416364f8@proxmox.com> Date: Tue, 18 Jul 2023 07:59:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Content-Language: en-US To: Wolfgang Bumiller Cc: pbs-devel@lists.proxmox.com References: <20230622091526.812422-1-m.carrara@proxmox.com> <20230622091526.812422-3-m.carrara@proxmox.com> From: Max Carrara In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.038 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 NICE_REPLY_A -0.097 Looks like a legit reply (A) 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 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [proxmox.com, lib.rs, rest.rs] Subject: Re: [pbs-devel] [PATCH proxmox 2/3] rest-server: Add `Redirector` 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: Tue, 18 Jul 2023 06:00:17 -0000 On 7/14/23 11:24, Wolfgang Bumiller wrote: > On Thu, Jun 22, 2023 at 11:15:25AM +0200, Max Carrara wrote: >> The `Redirector` is a simple `Service` that redirects HTTP requests >> to HTTPS and can be served by a `hyper::Server`. >> >> Signed-off-by: Max Carrara >> --- >> proxmox-rest-server/src/lib.rs | 2 +- >> proxmox-rest-server/src/rest.rs | 76 +++++++++++++++++++++++++++++++++ >> 2 files changed, 77 insertions(+), 1 deletion(-) >> >> diff --git a/proxmox-rest-server/src/lib.rs b/proxmox-rest-server/src/lib.rs >> index bc5be01..1c64ffb 100644 >> --- a/proxmox-rest-server/src/lib.rs >> +++ b/proxmox-rest-server/src/lib.rs >> @@ -48,7 +48,7 @@ mod api_config; >> pub use api_config::{ApiConfig, AuthError, AuthHandler, IndexHandler}; >> >> mod rest; >> -pub use rest::RestServer; >> +pub use rest::{Redirector, RestServer}; >> >> pub mod connection; >> >> diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs >> index 100c93c..2584e96 100644 >> --- a/proxmox-rest-server/src/rest.rs >> +++ b/proxmox-rest-server/src/rest.rs >> @@ -97,6 +97,82 @@ impl Service<&T> for RestServer { >> } >> } >> >> +pub struct Redirector {} >> + >> +impl Redirector { >> + pub fn new() -> Self { >> + Self {} >> + } >> +} >> + >> +impl Service<&T> for Redirector { >> + type Response = RedirectService; >> + type Error = Error; >> + type Future = std::future::Ready>; >> + >> + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { >> + Poll::Ready(Ok(())) >> + } >> + >> + fn call(&mut self, ctx: &T) -> Self::Future { >> + std::future::ready(match ctx.peer_addr() { > > ^ In theory we don't even need to bother with the address (although it's > unlikely to fail), since we never use it in the RedirectService? > Shouldn't RedirectService work just fine as a ZST? :-) > It does work as ZST indeed! This is (yet) another story of me wanting to conform to existing patterns / not breaking anything. :-) I'll throw it out for now, if we need it again later I can always put it back in. >> + Err(err) => Err(format_err!("unable to get peer address - {err}")), >> + Ok(peer) => Ok(RedirectService { peer }), >> + }) >> + } >> +} >> + >> +pub struct RedirectService { >> + pub peer: std::net::SocketAddr, >> +} >> + >> +impl Service> for RedirectService { >> + type Response = Response; >> + type Error = anyhow::Error; >> + type Future = Pin> + Send>>; >> + >> + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { >> + Poll::Ready(Ok(())) >> + } >> + >> + fn call(&mut self, req: Request) -> Self::Future { >> + lazy_static! { >> + static ref RE: Regex = Regex::new(r"((http(s)?://)|^)").unwrap(); >> + } >> + >> + let future = async move { >> + let header_host_value = req >> + .headers() >> + .get("host") >> + .and_then(|value| value.to_str().ok()); >> + >> + let response = if let Some(value) = header_host_value { >> + let location_value = RE.replace(value, "https://"); >> + >> + let status_code = if matches!(*req.method(), http::Method::GET | http::Method::HEAD) >> + { >> + StatusCode::MOVED_PERMANENTLY >> + } else { >> + StatusCode::PERMANENT_REDIRECT >> + }; >> + >> + Response::builder() >> + .status(status_code) >> + .header("Location", String::from(location_value)) >> + .body(Body::empty())? >> + } else { >> + Response::builder() >> + .status(StatusCode::BAD_REQUEST) >> + .body(Body::empty())? >> + }; >> + >> + Ok(response) >> + }; >> + >> + future.boxed() >> + } >> +} >> + >> pub trait PeerAddress { >> fn peer_addr(&self) -> Result; >> } >> -- >> 2.30.2 >> >> >> >> _______________________________________________ >> pbs-devel mailing list >> pbs-devel@lists.proxmox.com >> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel >> >>