1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::auth::*;
use crate::errors::*;
use crate::relay::*;

use util::{Conn, Error};

use tokio::time::Duration;

use std::sync::Arc;

// ConnConfig is used for UDP listeners
pub struct ConnConfig {
    pub conn: Arc<dyn Conn + Send + Sync>,

    // When an allocation is generated the RelayAddressGenerator
    // creates the net.PacketConn and returns the IP/Port it is available at
    pub relay_addr_generator: Box<dyn RelayAddressGenerator + Send + Sync>,
}

impl ConnConfig {
    pub fn validate(&self) -> Result<(), Error> {
        self.relay_addr_generator.validate()
    }
}

// ServerConfig configures the Pion TURN Server
pub struct ServerConfig {
    // conn_configs are a list of all the turn listeners
    // Each listener can have custom behavior around the creation of Relays
    pub conn_configs: Vec<ConnConfig>,

    // realm sets the realm for this server
    pub realm: String,

    // auth_handler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
    pub auth_handler: Arc<Box<dyn AuthHandler + Send + Sync>>,

    // channel_bind_timeout sets the lifetime of channel binding. Defaults to 10 minutes.
    pub channel_bind_timeout: Duration,
}

impl ServerConfig {
    pub fn validate(&self) -> Result<(), Error> {
        if self.conn_configs.is_empty() {
            return Err(ERR_NO_AVAILABLE_CONNS.to_owned());
        }

        for cc in &self.conn_configs {
            cc.validate()?;
        }
        Ok(())
    }
}