fixed public ip binding

This commit is contained in:
17ms 2023-05-29 02:42:38 +03:00
parent e461aa962f
commit b695d2fa1c
4 changed files with 20 additions and 15 deletions

View File

@ -79,16 +79,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
let paths = filepaths(source, files)?; let paths = filepaths(source, files)?;
let (metadata, index) = metadata(&paths).await?; let (metadata, index) = metadata(&paths).await?;
let addr = match (local, ipv6) { let (display_addr, bind_addr) = match (local, ipv6) {
(true, _) => Ip::Local.fetch(port)?, (true, _) => Ip::Local.fetch(port)?,
(false, true) => Ip::V6.fetch(port)?, (false, true) => Ip::V6.fetch(port)?,
(false, false) => Ip::V4.fetch(port)?, (false, false) => Ip::V4.fetch(port)?,
}; };
let server = Server::new(addr, key, chunksize, metadata, index); let server = Server::new(display_addr, key, chunksize, metadata, index);
tokio::spawn(async move { tokio::spawn(async move {
match server.start(rx).await { match server.start(rx, &bind_addr).await {
Ok(_) => {} Ok(_) => {}
Err(e) => error!("Error during server execution: {}", e), Err(e) => error!("Error during server execution: {}", e),
}; };

View File

@ -39,15 +39,19 @@ impl Server {
pub async fn start( pub async fn start(
self: Arc<Self>, self: Arc<Self>,
mut kill: mpsc::Receiver<()>, mut kill: mpsc::Receiver<()>,
bind_addr: &SocketAddr,
) -> Result<(), Box<dyn Error + Send + Sync>> { ) -> Result<(), Box<dyn Error + Send + Sync>> {
tokio::select! { tokio::select! {
_ = self.listen() => Ok(()), _ = self.listen(bind_addr) => Ok(()),
_ = kill.recv() => Ok(()), _ = kill.recv() => Ok(()),
} }
} }
async fn listen(self: Arc<Self>) -> Result<(), Box<dyn Error + Send + Sync>> { async fn listen(
let listener = TcpListener::bind(self.addr).await?; self: Arc<Self>,
bind_addr: &SocketAddr,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let listener = TcpListener::bind(bind_addr).await?;
info!("Listening on {} - Access key: {}", self.addr, self.key); info!("Listening on {} - Access key: {}", self.addr, self.key);

View File

@ -16,24 +16,26 @@ pub enum Ip {
} }
impl Ip { impl Ip {
pub fn fetch(self, port: u16) -> Result<SocketAddr, Box<dyn Error>> { pub fn fetch(self, port: u16) -> Result<(SocketAddr, SocketAddr), Box<dyn Error>> {
let addr = match self { let addr = match self {
Ip::V4 => PUBLIC_IPV4, Ip::V4 => PUBLIC_IPV4,
Ip::V6 => PUBLIC_IPV6, Ip::V6 => PUBLIC_IPV6,
Ip::Local => { Ip::Local => {
let addr_str = format!("127.0.0.1:{}", port); let addr_str = format!("127.0.0.1:{}", port);
return Ok(addr_str.parse::<SocketAddr>()?); let addr = addr_str.parse::<SocketAddr>()?;
return Ok((addr, addr));
} }
}; };
info!("Fetching IP information from {}", addr); info!("Fetching IP information from {}", addr);
let res = format!("{}:{}", ureq::get(addr).call()?.into_string()?.trim(), port); let res = format!("{}:{}", ureq::get(addr).call()?.into_string()?.trim(), port);
let addr = res.parse::<SocketAddr>()?; let display_addr = res.parse::<SocketAddr>()?;
let bind_addr = format!("0.0.0.0:{}", port).parse::<SocketAddr>()?;
debug!("IP: {}", res); debug!("IP: {}", res);
Ok(addr) Ok((display_addr, bind_addr))
} }
} }

View File

@ -24,14 +24,13 @@ async fn sockets_integration() {
.is_test(true) .is_test(true)
.try_init() .try_init()
.unwrap(); .unwrap();
//env_logger::builder().is_test(true).try_init().unwrap();
debug!("Initializing and starting the test"); debug!("Initializing and starting the test");
let (testdata, paths) = testdata(); let (testdata, paths) = testdata();
let (metadata, index) = metadata(&paths).await.unwrap(); let (metadata, index) = metadata(&paths).await.unwrap();
let addr = Ip::Local.fetch(8080).unwrap(); let (display_addr, bind_addr) = Ip::Local.fetch(8080).unwrap();
let outdir = PathBuf::from("./tests/output/"); let outdir = PathBuf::from("./tests/output/");
let key = String::from("testkey"); let key = String::from("testkey");
let c_key = key.clone(); let c_key = key.clone();
@ -40,14 +39,14 @@ async fn sockets_integration() {
let server_handle = tokio::spawn(async move { let server_handle = tokio::spawn(async move {
debug!("Initializing the asynchronous server task"); debug!("Initializing the asynchronous server task");
let server = Server::new(addr, key, 8192, metadata, index); let server = Server::new(display_addr, key, 8192, metadata, index);
debug!("Starting to listen to incoming connections"); debug!("Starting to listen to incoming connections");
server.start(rx).await.unwrap(); server.start(rx, &bind_addr).await.unwrap();
}); });
let client_handle = tokio::spawn(async move { let client_handle = tokio::spawn(async move {
debug!("Initializing the asynchronous client task"); debug!("Initializing the asynchronous client task");
let client = Client::new(addr, c_key, outdir); let client = Client::new(display_addr, c_key, outdir);
debug!("Connecting to the server"); debug!("Connecting to the server");
client.connection().await.unwrap(); client.connection().await.unwrap();
}); });