From a2f1fbc488aeb7d8c44adb311ca425db9c59eb5c Mon Sep 17 00:00:00 2001 From: akiroz Date: Mon, 29 Jan 2024 02:21:24 +0900 Subject: [PATCH] log server tunnel allocations --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/client.rs | 3 +-- src/lookup_pool.rs | 6 +++--- src/remote.rs | 2 +- src/server.rs | 10 +++++----- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 809d240..bb2fc6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1315,7 +1315,7 @@ checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" [[package]] name = "zika" -version = "3.3.4" +version = "3.3.5" dependencies = [ "base64", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 1ef7cba..d937c02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zika" -version = "3.3.4" +version = "3.3.5" license = "MIT" description = "IP Tunneling over MQTT" repository = "https://github.com/akiroz/zika" diff --git a/src/client.rs b/src/client.rs index 83bccf2..975e9e3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -18,7 +18,6 @@ use crate::config; use crate::remote; use crate::ip_iter::SizedIpv4NetworkIterator; - type TunSink = SplitSink, TunPacket>; pub struct Client { @@ -81,7 +80,7 @@ impl Client { if !ip_network.contains(bind_addr) { panic!("tunnel bind_addr outside subnet"); } - log::info!("bind {:?} -> {:?}", &bind_addr, &topic); + log::info!("bind {:?} -> {} ({})", &bind_addr, topic_base, base64_id); let subscribe_result = remote.subscribe(topic.clone()).await; if let Err(err) = subscribe_result { diff --git a/src/lookup_pool.rs b/src/lookup_pool.rs index 267da24..a1934bb 100644 --- a/src/lookup_pool.rs +++ b/src/lookup_pool.rs @@ -28,9 +28,9 @@ where } } - pub fn get_forward(&mut self, a: &A) -> B { + pub fn get_forward(&mut self, a: &A) -> (bool, B) { match self.forward.get(a) { - Some(b) => *b, + Some(b) => (true, *b), None => { let b = self .pool @@ -48,7 +48,7 @@ where } } self.reverse.insert(b, a.clone()); - b + (false, b) } } } diff --git a/src/remote.rs b/src/remote.rs index 8d3ad53..a7b54a1 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -156,7 +156,7 @@ impl Remote { let mut properties: PublishProperties = Default::default(); let topic_to_send = if let Some(ref mut pool) = client.alias_pool { let already_sent_alias = pool.contains(topic); - let alias = pool.get_forward(topic); + let (_exists, alias) = pool.get_forward(topic); properties.topic_alias = Some(alias); if already_sent_alias { "" } else { topic } } else { // Alias not used diff --git a/src/server.rs b/src/server.rs index 5d2b91a..b03321a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -120,13 +120,13 @@ impl Server { // mqtt -> tun async fn handle_remote_message(&self, tun_sink: &mut TunSink, id: &[u8], msg: &[u8]) -> Result<(), Box> { let base64_id = general_purpose::URL_SAFE_NO_PAD.encode(id); - let topic_base = &self.topic; - let topic = format!("{topic_base}/{base64_id}"); - let ip: Ipv4Addr = { + let (existing_tunnel, ip) = { let mut ip_pool = self.ip_pool.lock().await; - let ip = ip_pool.get_forward(&topic).into(); - ip + ip_pool.get_forward(&base64_id) }; + if !existing_tunnel { + log::info!("alloc tunnel {} (IP {})", base64_id, ip); + } let pkt = nat::do_nat(msg, ip, self.local_addr)?; tun_sink.send(TunPacket::new(pkt)).await?; Ok(())