1 use super::*; 2 use crate::error::*; 3 4 use async_trait::async_trait; 5 use std::net::IpAddr; 6 use util::vnet::net::*; 7 8 // RelayAddressGeneratorStatic can be used to return static IP address each time a relay is created. 9 // This can be used when you have a single static IP address that you want to use 10 pub struct RelayAddressGeneratorStatic { 11 // RelayAddress is the IP returned to the user when the relay is created 12 pub relay_address: IpAddr, 13 14 // Address is passed to Listen/ListenPacket when creating the Relay 15 pub address: String, 16 17 pub net: Arc<Net>, 18 } 19 20 #[async_trait] 21 impl RelayAddressGenerator for RelayAddressGeneratorStatic { 22 // validate confirms that the RelayAddressGenerator is properly initialized validate(&self) -> Result<()>23 fn validate(&self) -> Result<()> { 24 if self.address.is_empty() { 25 Err(Error::ErrListeningAddressInvalid) 26 } else { 27 Ok(()) 28 } 29 } 30 31 // Allocate a PacketConn (UDP) RelayAddress allocate_conn( &self, use_ipv4: bool, requested_port: u16, ) -> Result<(Arc<dyn Conn + Send + Sync>, SocketAddr)>32 async fn allocate_conn( 33 &self, 34 use_ipv4: bool, 35 requested_port: u16, 36 ) -> Result<(Arc<dyn Conn + Send + Sync>, SocketAddr)> { 37 let addr = self 38 .net 39 .resolve_addr(use_ipv4, &format!("{}:{}", self.address, requested_port)) 40 .await?; 41 let conn = self.net.bind(addr).await?; 42 let mut relay_addr = conn.local_addr()?; 43 relay_addr.set_ip(self.relay_address); 44 return Ok((conn, relay_addr)); 45 } 46 } 47