Skip to content

Commit

Permalink
fix: multi-interface xor peer address;
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr.Panda committed Aug 5, 2023
1 parent bbc48b8 commit cec9361
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 50 deletions.
2 changes: 1 addition & 1 deletion turn/src/processor/channel_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ use faster_stun::ChannelData;
pub fn process(ctx: Context, data: ChannelData<'_>) -> Option<Response<'_>> {
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))
}
4 changes: 2 additions & 2 deletions turn/src/processor/indication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ pub async fn process<'a>(

let method = Method::DataIndication;
let mut pack = MessageWriter::extend(method, &reader, bytes);
pack.append::<XorPeerAddress>(SocketAddr::new(interface.ip(), port));
pack.append::<XorPeerAddress>(SocketAddr::new(interface.external.ip(), port));
pack.append::<Data>(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)))
}
6 changes: 3 additions & 3 deletions turn/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub struct Response<'a> {
pub data: &'a [u8],
pub kind: StunClass,
pub relay: Option<SocketAddr>,
pub interface: Option<Arc<SocketAddr>>,
pub interface: Option<SocketAddr>,
}

impl<'a> Response<'a> {
Expand All @@ -333,7 +333,7 @@ impl<'a> Response<'a> {
data: &'a [u8],
kind: StunClass,
relay: Option<SocketAddr>,
interface: Option<Arc<SocketAddr>>,
interface: Option<SocketAddr>,
) -> Self {
Self {
data,
Expand Down Expand Up @@ -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()?;
Expand Down
62 changes: 39 additions & 23 deletions turn/src/router/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AHashMap<SocketAddr, Arc<SocketAddr>>>,
map: RwLock<AHashMap<SocketAddr, Arc<Interface>>>,
}

impl Default for Interfaces {
Expand All @@ -30,13 +36,20 @@ impl Interfaces {
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().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.
Expand All @@ -50,12 +63,13 @@ impl Interfaces {
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().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<SocketAddr> {
pub fn get(&self, addr: &SocketAddr) -> Option<Interface> {
self.map.read().get(addr).map(|item| *item.as_ref())
}

Expand All @@ -71,15 +85,16 @@ impl Interfaces {
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().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<Arc<SocketAddr>> {
pub fn get_ref(&self, addr: &SocketAddr) -> Option<Arc<Interface>> {
self.map.read().get(addr).cloned()
}

/// remove interface from addr.
///
/// # Examples
Expand All @@ -92,15 +107,16 @@ impl Interfaces {
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().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);
Expand Down
49 changes: 28 additions & 21 deletions turn/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Arc<SocketAddr>> {
pub fn get_interface(&self, addr: &SocketAddr) -> Option<Arc<Interface>> {
self.interfaces.get_ref(addr)
}

Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -336,14 +341,15 @@ 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);
/// ```
pub fn get_key_block(
&self,
addr: &SocketAddr,
interface: &SocketAddr,
external: &SocketAddr,
username: &str,
) -> Option<Arc<[u8; 16]>> {
let key = self.nodes.get_secret(addr);
Expand All @@ -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)
}

Expand All @@ -364,6 +370,7 @@ impl Router {
&self,
addr: &SocketAddr,
interface: &SocketAddr,
external: &SocketAddr,
username: &str,
) -> Option<Arc<[u8; 16]>> {
let key = self.nodes.get_secret(addr);
Expand All @@ -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)
}

Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
///
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit cec9361

Please sign in to comment.