fixed public ip binding
This commit is contained in:
parent
e461aa962f
commit
b695d2fa1c
@ -79,16 +79,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let paths = filepaths(source, files)?;
|
||||
let (metadata, index) = metadata(&paths).await?;
|
||||
let addr = match (local, ipv6) {
|
||||
let (display_addr, bind_addr) = match (local, ipv6) {
|
||||
(true, _) => Ip::Local.fetch(port)?,
|
||||
(false, true) => Ip::V6.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 {
|
||||
match server.start(rx).await {
|
||||
match server.start(rx, &bind_addr).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => error!("Error during server execution: {}", e),
|
||||
};
|
||||
|
@ -39,15 +39,19 @@ impl Server {
|
||||
pub async fn start(
|
||||
self: Arc<Self>,
|
||||
mut kill: mpsc::Receiver<()>,
|
||||
bind_addr: &SocketAddr,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
tokio::select! {
|
||||
_ = self.listen() => Ok(()),
|
||||
_ = self.listen(bind_addr) => Ok(()),
|
||||
_ = kill.recv() => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn listen(self: Arc<Self>) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let listener = TcpListener::bind(self.addr).await?;
|
||||
async fn listen(
|
||||
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);
|
||||
|
||||
|
10
src/util.rs
10
src/util.rs
@ -16,24 +16,26 @@ pub enum 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 {
|
||||
Ip::V4 => PUBLIC_IPV4,
|
||||
Ip::V6 => PUBLIC_IPV6,
|
||||
Ip::Local => {
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
Ok(addr)
|
||||
Ok((display_addr, bind_addr))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,14 +24,13 @@ async fn sockets_integration() {
|
||||
.is_test(true)
|
||||
.try_init()
|
||||
.unwrap();
|
||||
//env_logger::builder().is_test(true).try_init().unwrap();
|
||||
|
||||
debug!("Initializing and starting the test");
|
||||
|
||||
let (testdata, paths) = testdata();
|
||||
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 key = String::from("testkey");
|
||||
let c_key = key.clone();
|
||||
@ -40,14 +39,14 @@ async fn sockets_integration() {
|
||||
|
||||
let server_handle = tokio::spawn(async move {
|
||||
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");
|
||||
server.start(rx).await.unwrap();
|
||||
server.start(rx, &bind_addr).await.unwrap();
|
||||
});
|
||||
|
||||
let client_handle = tokio::spawn(async move {
|
||||
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");
|
||||
client.connection().await.unwrap();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user