From 427f6ee9996529e13fc7df3db9970bc10ca24e55 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Mon, 21 Jul 2025 01:18:34 +0100 Subject: TLS and Unix socket support --- src/config.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 570994c..d56ac8e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,10 +44,6 @@ fn default_host() -> String { "localhost".to_string() } -fn default_port() -> u16 { - 1883 -} - fn default_qos() -> QoS { QoS::ExactlyOnce } @@ -76,12 +72,22 @@ pub struct Route { pub qos: Option, } +#[derive(Deserialize, Debug, PartialEq, Default)] +#[serde(rename_all = "kebab-case")] +pub enum Transport { + #[default] + Tcp, + Tls, + Unix, +} + #[derive(Deserialize, Debug)] pub struct Config { #[serde(default = "default_host")] pub host: String, - #[serde(default = "default_port")] - pub port: u16, + pub port: Option, + #[serde(default)] + pub transport: Transport, #[serde(with = "QoSDef", default = "default_qos")] pub qos: QoS, #[serde(default = "default_timeout", deserialize_with = "deserialize_timeout")] @@ -97,10 +103,17 @@ pub struct Config { impl Config { pub fn mqtt_client(&self) -> (AsyncClient, EventLoop) { let client_id = self.id.to_string(); - let mut options = MqttOptions::new(client_id, &self.host, self.port); + let (transport, default_port) = match self.transport { + Transport::Tcp => (rumqttc::Transport::tcp(), 1883), + Transport::Tls => (rumqttc::Transport::tls_with_default_config(), 8883), + Transport::Unix => (rumqttc::Transport::unix(), 0), + }; + let mut options = + MqttOptions::new(client_id, &self.host, self.port.unwrap_or(default_port)); if let Some(credentials) = &self.credentials { options.set_credentials(&credentials.username, &credentials.password); } + options.set_transport(transport); // TODO: Make configurable options.set_keep_alive(Duration::from_secs(5)); options.set_max_packet_size(10 * 1024 * 1024, 10 * 1024 * 1024); @@ -158,6 +171,7 @@ mod tests { qos = "at-most-once" id = "custom-id" timeout = 15.5 + transport = "tls" [credentials] username = "testuser" @@ -182,10 +196,11 @@ mod tests { let config: Config = toml::from_str(toml_str).expect("Failed to parse full config"); assert_eq!(config.host, "foo.bar.baz"); - assert_eq!(config.port, 1234); + assert_eq!(config.port, Some(1234)); assert_eq!(config.qos, QoS::AtMostOnce); assert_eq!(config.id, "custom-id"); assert_eq!(config.timeout, Duration::from_secs_f64(15.5)); + assert_eq!(config.transport, Transport::Tls); let creds = config.credentials.expect("Credentials should be present"); assert_eq!(creds.username, "testuser"); @@ -225,10 +240,11 @@ mod tests { let config: Config = toml::from_str("[routes]").expect("Failed to parse minimal config"); assert_eq!(config.host, default_host()); - assert_eq!(config.port, default_port()); + assert_eq!(config.port, None); assert_eq!(config.qos, default_qos()); assert_eq!(config.id, default_id()); assert_eq!(config.timeout, default_timeout()); + assert_eq!(config.transport, Transport::default()); assert!(config.credentials.is_none()); assert_eq!(config.log, Logging::default()); assert!(config.routes.is_empty()); -- cgit v1.2.3-70-g09d2