Skip to content

Commit

Permalink
listen_addr
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeguglielmo committed Nov 19, 2023
1 parent a69b4fd commit 21fdf95
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
13 changes: 7 additions & 6 deletions dht-cache/src/domocache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ impl DomoCache {

event = self.swarm.select_next_some() => {
match event {
SwarmEvent::ExpiredListenAddr { address, .. } => {
log::debug!("Address {address:?} expired");
SwarmEvent::ExpiredListenAddr { listener_id, address, .. } => {
println!("Address {address:?} expired");
self.swarm.remove_listener(listener_id);
}
SwarmEvent::ConnectionEstablished { peer_id, connection_id, endpoint, .. } => {
println!("Connection established {peer_id:?}, {connection_id:?}, {endpoint:?}");
Expand All @@ -422,10 +423,10 @@ impl DomoCache {
log::warn!("Listener Error {listener_id:?} -> {error:?}");
}
SwarmEvent::OutgoingConnectionError { connection_id, peer_id, error } => {
log::info!("Outgoing connection error {peer_id:?}, {connection_id:?} -> {error:?}");
println!("Outgoing connection error {peer_id:?}, {connection_id:?} -> {error:?}");
}
SwarmEvent::ListenerClosed { .. } => {
log::info!("Listener Closed");
println!("Listener Closed");
}
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening in {address:?}");
Expand Down Expand Up @@ -525,7 +526,7 @@ impl DomoCache {
let loopback_only = conf.loopback;
let shared_key = conf.shared_key.clone();
let private_key_file = conf.private_key.clone();

let listen_addr = conf.listen_addr.clone();
let storage = SqlxStorage::new(&conf).await;

// Create a random local key.
Expand All @@ -551,7 +552,7 @@ impl DomoCache {
let local_key_pair = Keypair::rsa_from_pkcs8(&mut pkcs8_der)
.map_err(|e| format!("Couldn't load key: {e:?}"))?;

let swarm = crate::domolibp2p::start(shared_key, local_key_pair, loopback_only)
let swarm = crate::domolibp2p::start(shared_key, local_key_pair, loopback_only, listen_addr)
.await
.unwrap();

Expand Down
4 changes: 3 additions & 1 deletion dht-cache/src/domolibp2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub async fn start(
shared_key: String,
local_key_pair: identity::Keypair,
loopback_only: bool,
listen_addr: String
) -> Result<Swarm<DomoBehaviour>, Box<dyn Error>> {
let local_peer_id = PeerId::from(local_key_pair.public());

Expand Down Expand Up @@ -144,9 +145,10 @@ pub async fn start(

// Listen on all interfaces and whatever port the OS assigns.
if !loopback_only {
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on(listen_addr.parse()?)?;
} else {
swarm.listen_on("/ip4/127.0.0.1/tcp/0".parse()?)?;

}

Ok(swarm)
Expand Down
5 changes: 5 additions & 0 deletions dht-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub struct Cache {
/// Use only loopback iface for libp2p
#[arg(long)]
pub loopback: bool,

/// Listen Address
#[arg(long, short, default_value = "/ip4/0.0.0.0/tcp/0")]
pub listen_addr: String
}

impl Default for Cache {
Expand All @@ -54,6 +58,7 @@ impl Default for Cache {
private_key: None,
shared_key: "".to_string(),
loopback: false,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string()
}
}
}
Expand Down

0 comments on commit 21fdf95

Please sign in to comment.