Struct Router

Source
pub struct Router { /* private fields */ }
Expand description

Router State Tree.

this state management example maintains the status of all nodes in the current service and adds a node grouping model. it is necessary to specify a group for each node.

The state between groups is isolated. However, it should be noted that the node key only supports long-term valid passwords,does not support short-term valid passwords.

Implementations§

Source§

impl Router

Source

pub fn new(realm: String, observer: Arc<dyn Observer>) -> Arc<Self>

create a router.

§Examples
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;
impl Observer for ObserverTest {}

Router::new("test".to_string(), Arc::new(ObserverTest));
Source

pub fn capacity(&self) -> usize

get router capacity.

§Examples
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;
impl Observer for ObserverTest {}

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
assert_eq!(router.capacity(), 16383);
Source

pub fn len(&self) -> usize

get router allocate size.

§Examples
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;
impl Observer for ObserverTest {}

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
assert_eq!(router.len(), 0);
Source

pub fn is_empty(&self) -> bool

get router allocate size is empty.

§Examples
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;
impl Observer for ObserverTest {}

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
assert_eq!(router.is_empty(), true);
Source

pub fn get_interface(&self, addr: &SocketAddr) -> Option<Arc<Interface>>

get addr interface.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let interface = router.get_interface(&addr).unwrap();
assert_eq!(interface.addr, addr);
Source

pub fn get_users( &self, skip: usize, limit: usize, ) -> Vec<(String, Vec<SocketAddr>)>

get user list.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let users = router.get_users(0, 10);
assert_eq!(users.as_slice(), &[("test".to_string(), vec![addr])]);
Source

pub fn get_node(&self, addr: &SocketAddr) -> Option<Node>

get node.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let node = router.get_node(&addr).unwrap();
assert_eq!(node.username.as_str(), "test");
assert_eq!(node.password.as_str(), "test");
assert_eq!(node.secret.as_slice(), &secret);
assert_eq!(node.channels, vec![]);
assert_eq!(node.ports, vec![]);
Source

pub fn get_node_addrs(&self, username: &str) -> Vec<SocketAddr>

get node bound list.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let ret = router.get_node_addrs("test");
assert_eq!(ret, vec![addr]);
Source

pub fn get_nonce(&self, addr: &SocketAddr) -> Arc<String>

get the nonce of the node SocketAddr.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let nonce = router.get_nonce(&addr);
assert_eq!(nonce.len(), 16);
Source

pub fn get_key_block( &self, addr: &SocketAddr, interface: &SocketAddr, external: &SocketAddr, username: &str, ) -> Option<Arc<[u8; 16]>>

get the password of the node SocketAddr.

require remote control service to distribute keys.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);
Source

pub async fn get_key( &self, addr: &SocketAddr, interface: &SocketAddr, external: &SocketAddr, username: &str, ) -> Option<Arc<[u8; 16]>>

get the password of the node SocketAddr.

require remote control service to distribute keys.

Source

pub fn get_channel_bound( &self, addr: &SocketAddr, channel: u16, ) -> Option<SocketAddr>

obtain the peer address bound to the current node according to the channel number.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let port = router.alloc_port(&addr).unwrap();
assert!(router.bind_port(&addr, port).is_some());
assert_eq!(router.get_port_bound(port), Some(addr));
Source

pub fn get_port_bound(&self, port: u16) -> Option<SocketAddr>

obtain the peer address bound to the current node according to the port number.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let port = router.alloc_port(&addr).unwrap();
assert!(router.bind_port(&addr, port).is_some());
assert_eq!(router.get_port_bound(port), Some(addr));
Source

pub fn get_bound_port( &self, addr: &SocketAddr, peer: &SocketAddr, ) -> Option<u16>

get node the port.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let peer = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let port = router.alloc_port(&addr).unwrap();
assert!(router.bind_port(&addr, port).is_some());
assert!(router.bind_port(&peer, port).is_some());
assert_eq!(router.get_bound_port(&addr, &peer), Some(port));
Source

pub fn alloc_port(&self, addr: &SocketAddr) -> Option<u16>

alloc a port from State.

In all cases, the server SHOULD only allocate ports from the range 49152 - 65535 (the Dynamic and/or Private Port range [PORT-NUMBERS]), unless the TURN server application knows, through some means not specified here, that other applications running on the same host as the TURN server application will not be impacted by allocating ports outside this range. This condition can often be satisfied by running the TURN server application on a dedicated machine and/or by arranging that any other applications on the machine allocate ports before the TURN server application starts. In any case, the TURN server SHOULD NOT allocate ports in the range 0 - 1023 (the Well- Known Port range) to discourage clients from using TURN to run standard services.

NOTE: The use of randomized port assignments to avoid certain types of attacks is described in [RFC6056]. It is RECOMMENDED that a TURN server implement a randomized port assignment algorithm from [RFC6056]. This is especially applicable to servers that choose to pre-allocate a number of ports from the underlying OS and then later assign them to allocations; for example, a server may choose this technique to implement the EVEN-PORT attribute.

The server determines the initial value of the time-to-expiry field as follows. If the request contains a LIFETIME attribute, then the server computes the minimum of the client’s proposed lifetime and the server’s maximum allowed lifetime. If this computed value is greater than the default lifetime, then the server uses the computed lifetime as the initial value of the time-to-expiry field. Otherwise, the server uses the default lifetime. It is RECOMMENDED that the server use a maximum allowed lifetime value of no more than 3600 seconds (1 hour). Servers that implement allocation quotas or charge users for allocations in some way may wish to use a smaller maximum allowed lifetime (perhaps as small as the default lifetime) to more quickly remove orphaned allocations (that is, allocations where the corresponding client has crashed or terminated, or the client connection has been lost for some reason). Also, note that the time- to-expiry is recomputed with each successful Refresh request, and thus, the value computed here applies only until the first refresh.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);
assert!(router.alloc_port(&addr).is_some());
Source

pub fn bind_port(&self, addr: &SocketAddr, port: u16) -> Option<()>

bind port for State.

A server need not do anything special to implement idempotency of CreatePermission requests over UDP using the “stateless stack approach”. Retransmitted CreatePermission requests will simply refresh the permissions.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let port = router.alloc_port(&addr).unwrap();
assert!(router.bind_port(&addr, port).is_some());
Source

pub fn bind_channel( &self, addr: &SocketAddr, port: u16, channel: u16, ) -> Option<()>

bind channel number for State.

A server need not do anything special to implement idempotency of ChannelBind requests over UDP using the “stateless stack approach”. Retransmitted ChannelBind requests will simply refresh the channel binding and the corresponding permission. Furthermore, the client must wait 5 minutes before binding a previously bound channel number or peer address to a different channel, eliminating the possibility that the transaction would initially fail but succeed on a retransmission.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);

let port = router.alloc_port(&addr).unwrap();
assert!(router.bind_channel(&addr, port, 0x4000).is_some());
Source

pub fn refresh(&self, addr: &SocketAddr, delay: u32)

refresh node lifetime.

The server computes a value called the “desired lifetime” as follows: if the request contains a LIFETIME attribute and the attribute value is zero, then the “desired lifetime” is zero. Otherwise, if the request contains a LIFETIME attribute, then the server computes the minimum of the client’s requested lifetime and the server’s maximum allowed lifetime. If this computed value is greater than the default lifetime, then the “desired lifetime” is the computed value. Otherwise, the “desired lifetime” is the default lifetime.

Subsequent processing depends on the “desired lifetime” value:

  • If the “desired lifetime” is zero, then the request succeeds and the allocation is deleted.

  • If the “desired lifetime” is non-zero, then the request succeeds and the allocation’s time-to-expiry is set to the “desired lifetime”.

If the request succeeds, then the server sends a success response containing:

  • A LIFETIME attribute containing the current value of the time-to- expiry timer.

NOTE: A server need not do anything special to implement idempotency of Refresh requests over UDP using the “stateless stack approach”. Retransmitted Refresh requests with a non- zero “desired lifetime” will simply refresh the allocation. A retransmitted Refresh request with a zero “desired lifetime” will cause a 437 (Allocation Mismatch) response if the allocation has already been deleted, but the client will treat this as equivalent to a success response (see below).

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);
router.refresh(&addr, 0);

assert!(router.get_node(&addr).is_none());
Source

pub fn remove(&self, addr: &SocketAddr) -> Option<()>

remove a node.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);
assert!(router.remove(&addr).is_some());
assert!(router.get_node(&addr).is_none());
Source

pub fn remove_from_user(&self, u: &str)

remove a node from username.

§Examples
use std::net::SocketAddr;
use std::sync::Arc;
use turn_rs::router::*;
use turn_rs::*;

struct ObserverTest;

impl Observer for ObserverTest {
    fn get_password_blocking(
        &self,
        _: &SocketAddr,
        _: &str,
    ) -> Option<String> {
        Some("test".to_string())
    }
}

let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let secret = [
    174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    239,
];

let router = Router::new("test".to_string(), Arc::new(ObserverTest));
let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();

assert_eq!(key.as_slice(), &secret);
router.remove_from_user("test");

assert!(router.get_node(&addr).is_none());

Auto Trait Implementations§

§

impl !Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V