From 098a6cf2cdb9b0370ab7358b005f731b65e9981c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 8 Dec 2021 11:11:22 +0100 Subject: Implement glob pattern hostnames no wildcard certificates: one certificate per matching hostname that actually recieves requests --- src/proxy_config.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/proxy_config.rs') diff --git a/src/proxy_config.rs b/src/proxy_config.rs index d4fe039..9092b59 100644 --- a/src/proxy_config.rs +++ b/src/proxy_config.rs @@ -15,11 +15,34 @@ use crate::consul::*; // ---- Extract proxy config from Consul catalog ---- +#[derive(Debug)] +pub enum HostDescription { + Hostname(String), + Pattern(glob::Pattern), +} + +impl HostDescription { + fn new(desc: &str) -> Result { + if desc.chars().any(|x| matches!(x, '*' | '?' | '[' | ']')) { + Ok(Self::Pattern(glob::Pattern::new(desc)?)) + } else { + Ok(Self::Hostname(desc.to_string())) + } + } + + pub fn matches(&self, v: &str) -> bool { + match self { + Self::Pattern(p) => p.matches(v), + Self::Hostname(s) => s == v, + } + } +} + #[derive(Debug)] pub struct ProxyEntry { pub target_addr: SocketAddr, - pub host: String, + pub host: HostDescription, pub path_prefix: Option, pub priority: u32, pub add_headers: Vec<(String, String)>, @@ -65,9 +88,17 @@ fn parse_tricot_tag( _ => 100, }; + let host = match HostDescription::new(host) { + Ok(h) => h, + Err(e) => { + warn!("Invalid hostname pattern {}: {}", host, e); + return None; + } + }; + Some(ProxyEntry { target_addr, - host: host.to_string(), + host, path_prefix, priority, add_headers: add_headers.to_vec(), -- cgit v1.2.3