From cec9361ecdb01855b322ad6ef1e83cebdcdac6c8 Mon Sep 17 00:00:00 2001 From: "Mr.Panda" Date: Sat, 5 Aug 2023 10:59:27 +0800 Subject: [PATCH] fix: multi-interface xor peer address; --- turn/src/processor/channel_data.rs | 2 +- turn/src/processor/indication.rs | 4 +- turn/src/processor/mod.rs | 6 +-- turn/src/router/interfaces.rs | 62 +++++++++++++++++++----------- turn/src/router/mod.rs | 49 +++++++++++++---------- 5 files changed, 73 insertions(+), 50 deletions(-) diff --git a/turn/src/processor/channel_data.rs b/turn/src/processor/channel_data.rs index 499aee4..a0519df 100644 --- a/turn/src/processor/channel_data.rs +++ b/turn/src/processor/channel_data.rs @@ -33,6 +33,6 @@ use faster_stun::ChannelData; pub fn process(ctx: Context, data: ChannelData<'_>) -> Option> { let addr = ctx.env.router.get_channel_bound(&ctx.addr, data.number)?; let interface = ctx.env.router.get_interface(&addr)?; - let to = (&ctx.env.interface != interface.as_ref()).then(|| interface); + let to = (&ctx.env.interface != &interface.addr).then(|| interface.addr); Some(Response::new(data.buf, StunClass::Channel, Some(addr), to)) } diff --git a/turn/src/processor/indication.rs b/turn/src/processor/indication.rs index 5740786..9fa3e81 100644 --- a/turn/src/processor/indication.rs +++ b/turn/src/processor/indication.rs @@ -88,10 +88,10 @@ pub async fn process<'a>( let method = Method::DataIndication; let mut pack = MessageWriter::extend(method, &reader, bytes); - pack.append::(SocketAddr::new(interface.ip(), port)); + pack.append::(SocketAddr::new(interface.external.ip(), port)); pack.append::(data); pack.flush(None)?; - let to = (&ctx.env.interface != interface.as_ref()).then(|| interface); + let to = (&ctx.env.interface != &interface.addr).then(|| interface.addr); Ok(Some(Response::new(bytes, StunClass::Msg, Some(addr), to))) } diff --git a/turn/src/processor/mod.rs b/turn/src/processor/mod.rs index 8eb73eb..7e26349 100644 --- a/turn/src/processor/mod.rs +++ b/turn/src/processor/mod.rs @@ -324,7 +324,7 @@ pub struct Response<'a> { pub data: &'a [u8], pub kind: StunClass, pub relay: Option, - pub interface: Option>, + pub interface: Option, } impl<'a> Response<'a> { @@ -333,7 +333,7 @@ impl<'a> Response<'a> { data: &'a [u8], kind: StunClass, relay: Option, - interface: Option>, + interface: Option, ) -> Self { Self { data, @@ -403,7 +403,7 @@ pub(crate) async fn verify_message<'a>( let key = ctx .env .router - .get_key(&ctx.addr, &ctx.env.interface, username) + .get_key(&ctx.addr, &ctx.env.interface, &ctx.env.external, username) .await?; reader.integrity(&key).ok()?; diff --git a/turn/src/router/interfaces.rs b/turn/src/router/interfaces.rs index 5eb20f5..eae6bbc 100644 --- a/turn/src/router/interfaces.rs +++ b/turn/src/router/interfaces.rs @@ -6,8 +6,14 @@ use std::sync::Arc; use ahash::AHashMap; use parking_lot::RwLock; +#[derive(Clone, Copy, Debug)] +pub struct Interface { + pub addr: SocketAddr, + pub external: SocketAddr, +} + pub struct Interfaces { - map: RwLock>>, + map: RwLock>>, } impl Default for Interfaces { @@ -30,13 +36,20 @@ impl Interfaces { /// let addr = "127.0.0.1:8080".parse::().unwrap(); /// let interface = "127.0.0.1:8081".parse::().unwrap(); /// let interfaces = Interfaces::default(); - /// - /// interfaces.insert(addr, interface); - /// let ret = interfaces.get(&addr); - /// assert_eq!(ret, Some(interface)); + /// + /// interfaces.insert(addr, interface, interface); + /// let ret = interfaces.get(&addr).unwrap(); + /// assert_eq!(ret.addr, interface); + /// assert_eq!(ret.external, interface); /// ``` - pub fn insert(&self, addr: SocketAddr, interface: SocketAddr) { - self.map.write().insert(addr, Arc::new(interface)); + pub fn insert(&self, addr: SocketAddr, interface: SocketAddr, external: SocketAddr) { + self.map.write().insert( + addr, + Arc::new(Interface { + addr: interface, + external, + }), + ); } /// get interface from addr. @@ -50,12 +63,13 @@ impl Interfaces { /// let addr = "127.0.0.1:8080".parse::().unwrap(); /// let interface = "127.0.0.1:8081".parse::().unwrap(); /// let interfaces = Interfaces::default(); - /// - /// interfaces.insert(addr, interface); - /// let ret = interfaces.get(&addr); - /// assert_eq!(ret, Some(interface)); + /// + /// interfaces.insert(addr, interface, interface); + /// let ret = interfaces.get(&addr).unwrap(); + /// assert_eq!(ret.addr, interface); + /// assert_eq!(ret.external, interface); /// ``` - pub fn get(&self, addr: &SocketAddr) -> Option { + pub fn get(&self, addr: &SocketAddr) -> Option { self.map.read().get(addr).map(|item| *item.as_ref()) } @@ -71,15 +85,16 @@ impl Interfaces { /// let addr = "127.0.0.1:8080".parse::().unwrap(); /// let interface = "127.0.0.1:8081".parse::().unwrap(); /// let interfaces = Interfaces::default(); - /// - /// interfaces.insert(addr, interface); - /// let ret = interfaces.get_ref(&addr); - /// assert_eq!(ret, Some(Arc::new(interface))); + /// + /// interfaces.insert(addr, interface, interface); + /// let ret = interfaces.get_ref(&addr).unwrap(); + /// assert_eq!(ret.addr, interface); + /// assert_eq!(ret.external, interface); /// ``` - pub fn get_ref(&self, addr: &SocketAddr) -> Option> { + pub fn get_ref(&self, addr: &SocketAddr) -> Option> { self.map.read().get(addr).cloned() } - + /// remove interface from addr. /// /// # Examples @@ -92,15 +107,16 @@ impl Interfaces { /// let addr = "127.0.0.1:8080".parse::().unwrap(); /// let interface = "127.0.0.1:8081".parse::().unwrap(); /// let interfaces = Interfaces::default(); - /// - /// interfaces.insert(addr, interface); - /// let ret = interfaces.get(&addr); - /// assert_eq!(ret, Some(interface)); + /// + /// interfaces.insert(addr, interface, interface); + /// let ret = interfaces.get(&addr).unwrap(); + /// assert_eq!(ret.addr, interface); + /// assert_eq!(ret.external, interface); /// /// interfaces.remove(&addr); /// /// let ret = interfaces.get(&addr); - /// assert_eq!(ret, None); + /// assert!(ret.is_none()); /// ``` pub fn remove(&self, addr: &SocketAddr) { self.map.write().remove(addr); diff --git a/turn/src/router/mod.rs b/turn/src/router/mod.rs index e2a2615..1a3214c 100644 --- a/turn/src/router/mod.rs +++ b/turn/src/router/mod.rs @@ -4,9 +4,14 @@ pub mod nodes; pub mod nonces; pub mod ports; +#[rustfmt::skip] use crate::Observer; use self::{ - channels::Channels, interfaces::Interfaces, nodes::Nodes, nonces::Nonces, ports::Ports, + channels::Channels, + interfaces::{Interface, Interfaces}, + nodes::Nodes, + nonces::Nonces, + ports::Ports, }; use std::{net::SocketAddr, sync::Arc, thread, time::Duration}; @@ -155,14 +160,14 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// - /// let interface = router.get_interface(&addr); - /// assert_eq!(interface, Some(Arc::new(addr))); + /// let interface = router.get_interface(&addr).unwrap(); + /// assert_eq!(interface.addr, addr); /// ``` - pub fn get_interface(&self, addr: &SocketAddr) -> Option> { + pub fn get_interface(&self, addr: &SocketAddr) -> Option> { self.interfaces.get_ref(addr) } @@ -190,7 +195,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -225,7 +230,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -264,7 +269,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -299,7 +304,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -336,7 +341,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// ``` @@ -344,6 +349,7 @@ impl Router { &self, addr: &SocketAddr, interface: &SocketAddr, + external: &SocketAddr, username: &str, ) -> Option> { let key = self.nodes.get_secret(addr); @@ -353,7 +359,7 @@ impl Router { let pwd = self.observer.auth_block(addr, username)?; let key = self.nodes.insert(addr, &self.realm, username, &pwd)?; - self.interfaces.insert(*addr, *interface); + self.interfaces.insert(*addr, *interface, *external); Some(key) } @@ -364,6 +370,7 @@ impl Router { &self, addr: &SocketAddr, interface: &SocketAddr, + external: &SocketAddr, username: &str, ) -> Option> { let key = self.nodes.get_secret(addr); @@ -373,7 +380,7 @@ impl Router { let pwd = self.observer.auth(addr, username).await?; let key = self.nodes.insert(addr, &self.realm, username, &pwd)?; - self.interfaces.insert(*addr, *interface); + self.interfaces.insert(*addr, *interface, *external); Some(key) } @@ -402,7 +409,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -439,7 +446,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -476,7 +483,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -552,7 +559,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// assert!(router.alloc_port(&addr).is_some()); @@ -592,7 +599,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -637,7 +644,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// @@ -707,7 +714,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// router.refresh(&addr, 0); @@ -746,7 +753,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// assert!(router.remove(&addr).is_some()); @@ -789,7 +796,7 @@ impl Router { /// ]; /// /// let router = Router::new("test".to_string(), Arc::new(ObserverTest)); - /// let key = router.get_key_block(&addr, &addr, "test").unwrap(); + /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap(); /// /// assert_eq!(key.as_slice(), &secret); /// router.remove_from_user("test");