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
use super::*;
use crate::errors::*;
use std::net::IpAddr;
use tokio::net::UdpSocket;
use async_trait::async_trait;
pub struct RelayAddressGeneratorStatic {
pub relay_address: IpAddr,
pub address: String,
}
#[async_trait]
impl RelayAddressGenerator for RelayAddressGeneratorStatic {
fn validate(&self) -> Result<(), Error> {
if self.address.is_empty() {
Err(ERR_LISTENING_ADDRESS_INVALID.to_owned())
} else {
Ok(())
}
}
async fn allocate_conn(
&self,
_network: &str,
requested_port: u16,
) -> Result<(Arc<dyn Conn + Send + Sync>, SocketAddr), Error> {
let conn = UdpSocket::bind(format!("{}:{}", self.address, requested_port)).await?;
let mut relay_addr = conn.local_addr()?;
relay_addr.set_ip(self.relay_address);
return Ok((Arc::new(conn), relay_addr));
}
}