Skip to content

Commit

Permalink
Add --root-redirect-url proxy setting (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb authored Feb 6, 2024
1 parent 59dffcd commit 2e3cb8a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plane/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ enum Command {

#[clap(long)]
acme_email: Option<String>,

/// URL to redirect the root path to.
#[clap(long)]
root_redirect_url: Option<Url>,
},
Dns {
#[clap(long)]
Expand Down Expand Up @@ -188,6 +192,7 @@ async fn run(opts: Opts) -> Result<()> {
cert_path,
acme_endpoint,
acme_email,
root_redirect_url,
} => {
let name = name.or_random();
tracing::info!(?name, "Starting proxy");
Expand Down Expand Up @@ -243,6 +248,7 @@ async fn run(opts: Opts) -> Result<()> {
cert_path.as_deref(),
port_config,
acme_config,
root_redirect_url,
)
.await?;
}
Expand Down
3 changes: 3 additions & 0 deletions plane/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub async fn run_proxy(
cert_path: Option<&Path>,
port_config: ServerPortConfig,
acme_config: Option<AcmeConfig>,
root_redirect_url: Option<Url>,
) -> Result<()> {
let (cert_watcher, cert_manager) =
watcher_manager_pair(cluster.clone(), cert_path, acme_config)?;
Expand All @@ -100,13 +101,15 @@ pub async fn run_proxy(
let http_handle = ProxyMakeService {
state: proxy_connection.state(),
https_redirect,
root_redirect_url: root_redirect_url.clone(),
}
.serve_http(port_config.http_port, shutdown_signal.subscribe())?;

let https_handle = if let Some(https_port) = port_config.https_port {
let https_handle = ProxyMakeService {
state: proxy_connection.state(),
https_redirect: false,
root_redirect_url,
}
.serve_https(https_port, cert_watcher, shutdown_signal.subscribe())?;

Expand Down
14 changes: 14 additions & 0 deletions plane/src/proxy/proxy_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
use tokio::io::copy_bidirectional;
use tokio::task::JoinHandle;
use tokio_rustls::rustls::ServerConfig;
use url::Url;

const PLANE_BACKEND_ID_HEADER: &str = "x-plane-backend-id";

Expand Down Expand Up @@ -62,6 +63,7 @@ struct RequestHandler {
state: Arc<ProxyState>,
https_redirect: bool,
remote_meta: ForwardableRequestInfo,
root_redirect_url: Option<Url>,
}

impl RequestHandler {
Expand Down Expand Up @@ -103,6 +105,15 @@ impl RequestHandler {
.body(hyper::Body::empty())?);
}

if req.uri().path() == "/" {
if let Some(root_redirect_url) = &self.root_redirect_url {
return Ok(hyper::Response::builder()
.status(hyper::StatusCode::MOVED_PERMANENTLY)
.header(hyper::header::LOCATION, root_redirect_url.to_string())
.body(hyper::Body::empty())?);
}
}

self.handle_proxy_request(req).await
}

Expand Down Expand Up @@ -230,6 +241,7 @@ impl Service<Request<Body>> for ProxyService {
pub struct ProxyMakeService {
pub state: Arc<ProxyState>,
pub https_redirect: bool,
pub root_redirect_url: Option<Url>,
}

impl ProxyMakeService {
Expand Down Expand Up @@ -298,6 +310,7 @@ impl<'a> Service<&'a AddrStream> for ProxyMakeService {
ip: remote_ip,
protocol: Protocol::Http,
},
root_redirect_url: self.root_redirect_url.clone(),
});
ready(Ok(ProxyService { handler })).boxed()
}
Expand All @@ -321,6 +334,7 @@ impl<'a> Service<&'a TlsStream> for ProxyMakeService {
ip: remote_ip,
protocol: Protocol::Https,
},
root_redirect_url: self.root_redirect_url.clone(),
});
ready(Ok(ProxyService { handler })).boxed()
}
Expand Down

0 comments on commit 2e3cb8a

Please sign in to comment.