xref: /webrtc/turn/src/server/config.rs (revision 9ea7b2ac)
1 use crate::allocation::*;
2 use crate::auth::*;
3 use crate::error::*;
4 use crate::relay::*;
5 
6 use util::Conn;
7 
8 use std::sync::Arc;
9 use tokio::sync::mpsc;
10 use tokio::time::Duration;
11 
12 // ConnConfig is used for UDP listeners
13 pub 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 
21 impl ConnConfig {
validate(&self) -> Result<()>22     pub fn validate(&self) -> Result<()> {
23         self.relay_addr_generator.validate()
24     }
25 }
26 
27 // ServerConfig configures the Pion TURN Server
28 pub 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, allowing users to customize Pion TURN with custom behavior
37     pub auth_handler: Arc<dyn AuthHandler + Send + Sync>,
38 
39     // channel_bind_timeout sets the lifetime of channel binding. Defaults to 10 minutes.
40     pub channel_bind_timeout: Duration,
41 
42     // to receive notify on allocation close event, with metrics data.
43     pub alloc_close_notify: Option<mpsc::Sender<AllocationInfo>>,
44 }
45 
46 impl ServerConfig {
validate(&self) -> Result<()>47     pub fn validate(&self) -> Result<()> {
48         if self.conn_configs.is_empty() {
49             return Err(Error::ErrNoAvailableConns);
50         }
51 
52         for cc in &self.conn_configs {
53             cc.validate()?;
54         }
55         Ok(())
56     }
57 }
58