public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Max Carrara <m.carrara@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox 2/3] rest-server: Add `Redirector`
Date: Fri, 14 Jul 2023 11:24:23 +0200	[thread overview]
Message-ID: <yw2qpmplshpanea2hvcg3u7q4e7yrmkapwss6eaayycrrgl4re@x554qzggrwu2> (raw)
In-Reply-To: <20230622091526.812422-3-m.carrara@proxmox.com>

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 <m.carrara@proxmox.com>
> ---
>  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<T: PeerAddress> Service<&T> for RestServer {
>      }
>  }
>  
> +pub struct Redirector {}
> +
> +impl Redirector {
> +    pub fn new() -> Self {
> +        Self {}
> +    }
> +}
> +
> +impl<T: PeerAddress> Service<&T> for Redirector {
> +    type Response = RedirectService;
> +    type Error = Error;
> +    type Future = std::future::Ready<Result<Self::Response, Self::Error>>;
> +
> +    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
> +        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? :-)

> +            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<Request<Body>> for RedirectService {
> +    type Response = Response<Body>;
> +    type Error = anyhow::Error;
> +    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
> +
> +    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
> +        Poll::Ready(Ok(()))
> +    }
> +
> +    fn call(&mut self, req: Request<Body>) -> 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<std::net::SocketAddr, Error>;
>  }
> -- 
> 2.30.2
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
> 
> 




  reply	other threads:[~2023-07-14  9:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-22  9:15 [pbs-devel] [PATCH proxmox, proxmox-backup 0/3] Add support for HTTP to HTTPS redirection Max Carrara
2023-06-22  9:15 ` [pbs-devel] [PATCH proxmox 1/3] rest-server: Add `BiAcceptBuilder` Max Carrara
2023-07-14  9:20   ` Wolfgang Bumiller
2023-07-18  5:46     ` Max Carrara
2023-06-22  9:15 ` [pbs-devel] [PATCH proxmox 2/3] rest-server: Add `Redirector` Max Carrara
2023-07-14  9:24   ` Wolfgang Bumiller [this message]
2023-07-18  5:59     ` Max Carrara
2023-06-22  9:15 ` [pbs-devel] [PATCH proxmox-backup 3/3] proxy: redirect HTTP requests to HTTPS Max Carrara
2023-06-23 10:15 ` [pbs-devel] [PATCH proxmox, proxmox-backup 0/3] Add support for HTTP to HTTPS redirection Max Carrara
2023-06-23 10:55   ` Thomas Lamprecht
2023-06-27  9:39     ` Max Carrara
2023-06-23 11:40 ` Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yw2qpmplshpanea2hvcg3u7q4e7yrmkapwss6eaayycrrgl4re@x554qzggrwu2 \
    --to=w.bumiller@proxmox.com \
    --cc=m.carrara@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal