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