From 01490ea612aec716973267d73c14dc8eb79b43e9 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 31 Dec 2024 08:11:06 +1300 Subject: [PATCH] config.base_url (and fixes related to wrong URLs) --- contrib/chorus.toml | 7 +++++++ docs/CONFIG.md | 7 +++++++ src/config.rs | 33 ++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/contrib/chorus.toml b/contrib/chorus.toml index e360bd6..c0594f3 100644 --- a/contrib/chorus.toml +++ b/contrib/chorus.toml @@ -43,6 +43,13 @@ hostname = "localhost" chorus_is_behind_a_proxy = false +# If chorus is behing a proxy, it can't compute it's Internet-visible URL. So set it here. +# This is used currently by web management and blossom and maybe more in the future. +# +# Default is not set. +# +# base_url = + # If true, chorus will handle TLS, running over HTTPS. If false, chorus run over HTTP. # # If you are proxying via nginx, normally you will set this to false and allow nginx to handle TLS. diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 3de5492..f58b46f 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -44,6 +44,13 @@ or the connection will not be served. Default is false. +### base_url + +If chorus is behing a proxy, it can't compute it's Internet-visible URL. So set it here. +This is used currently by web management and blossom and maybe more in the future. + +Default is not set. + ### use_tls If true, chorus will handle TLS, running over HTTPS. If false, chorus run over HTTP. diff --git a/src/config.rs b/src/config.rs index dd83e77..b2ba592 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub struct FriendlyConfig { pub port: u16, pub hostname: String, pub chorus_is_behind_a_proxy: bool, + pub base_url: Option, pub use_tls: bool, pub certchain_pem_path: String, pub key_pem_path: String, @@ -51,6 +52,7 @@ impl Default for FriendlyConfig { port: 443, hostname: "localhost".to_string(), chorus_is_behind_a_proxy: false, + base_url: None, use_tls: true, certchain_pem_path: "/opt/chorus/etc/tls/fullchain.pem".to_string(), key_pem_path: "/opt/chorus/etc/tls/privkey.pem".to_string(), @@ -91,6 +93,7 @@ impl FriendlyConfig { port, hostname, chorus_is_behind_a_proxy, + base_url, use_tls, certchain_pem_path, key_pem_path, @@ -151,6 +154,7 @@ impl FriendlyConfig { port, hostname, chorus_is_behind_a_proxy, + base_url, use_tls, certchain_pem_path, key_pem_path, @@ -192,6 +196,7 @@ pub struct Config { pub port: u16, pub hostname: Host, pub chorus_is_behind_a_proxy: bool, + pub base_url: Option, pub use_tls: bool, pub certchain_pem_path: String, pub key_pem_path: String, @@ -236,15 +241,25 @@ impl Default for Config { impl Config { pub fn uri_parts(&self, inner: Uri, http: bool) -> Result { let mut uri_parts = inner.into_parts(); - let scheme = match (self.use_tls, http) { - (false, false) => Scheme::from_str("ws").unwrap(), - (true, false) => Scheme::from_str("wss").unwrap(), - (false, true) => Scheme::HTTP, - (true, true) => Scheme::HTTPS, - }; - uri_parts.scheme = Some(scheme); - let authority = Authority::from_str(&format!("{}:{}", self.hostname, self.port))?; - uri_parts.authority = Some(authority); + + if let Some(s) = &self.base_url { + let base_uri = s.parse::()?; + let base_uri_parts = base_uri.into_parts(); + uri_parts.scheme = base_uri_parts.scheme; + uri_parts.authority = base_uri_parts.authority; + } else { + let scheme = match (self.use_tls, http) { + (false, false) => Scheme::from_str("ws").unwrap(), + (true, false) => Scheme::from_str("wss").unwrap(), + (false, true) => Scheme::HTTP, + (true, true) => Scheme::HTTPS, + }; + uri_parts.scheme = Some(scheme); + + let authority = Authority::from_str(&format!("{}:{}", self.hostname, self.port))?; + uri_parts.authority = Some(authority); + } + Ok(uri_parts) } }