Skip to main content

clia_turn/server/
config.rs

1use std::sync::Arc;
2
3use tokio::sync::mpsc;
4use tokio::time::Duration;
5use util::Conn;
6
7use crate::allocation::*;
8use crate::auth::*;
9use crate::error::*;
10use crate::relay::*;
11
12/// ConnConfig is used for UDP listeners
13pub struct ConnConfig {
14    pub conn: Arc<dyn Conn + Send + Sync>,
15
16    // When an allocation is generated the RelayAddressGenerator
17    // creates the net.PacketConn and returns the IP/Port it is available at
18    pub relay_addr_generator: Box<dyn RelayAddressGenerator + Send + Sync>,
19}
20
21impl ConnConfig {
22    pub fn validate(&self) -> Result<()> {
23        self.relay_addr_generator.validate()
24    }
25}
26
27/// ServerConfig configures the TURN Server
28pub struct ServerConfig {
29    /// `conn_configs` are a list of all the turn listeners.
30    /// Each listener can have custom behavior around the creation of Relays.
31    pub conn_configs: Vec<ConnConfig>,
32
33    /// `realm` sets the realm for this server
34    pub realm: String,
35
36    /// `auth_handler` is a callback used to handle incoming auth requests,
37    /// allowing users to customize Pion TURN with custom behavior.
38    pub auth_handler: Arc<dyn AuthHandler + Send + Sync>,
39
40    /// `channel_bind_timeout` sets the lifetime of channel binding. Defaults to 10 minutes.
41    pub channel_bind_timeout: Duration,
42
43    /// To receive notify on allocation close event, with metrics data.
44    pub alloc_close_notify: Option<mpsc::Sender<AllocationInfo>>,
45}
46
47impl ServerConfig {
48    pub fn validate(&self) -> Result<()> {
49        if self.conn_configs.is_empty() {
50            return Err(Error::ErrNoAvailableConns);
51        }
52
53        for cc in &self.conn_configs {
54            cc.validate()?;
55        }
56        Ok(())
57    }
58}