Skip to content

Commit

Permalink
feat: implement autonat server behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 20, 2024
1 parent 02a2f56 commit d50b49b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "examples/dcutr", "examples/chat"]

[package]
name = "boot-node"
version = "0.4.0"
version = "0.5.0"
authors = ["Calimero Limited <[email protected]>"]
edition = "2021"
repository = "https://github.com/calimero-network/boot-node"
Expand All @@ -15,6 +15,7 @@ clap = { version = "4.5.4", features = ["derive", "env"] }
eyre = "0.6.12"
futures-util = "0.3.30"
libp2p = { version = "0.53.2", features = [
"autonat",
"identify",
"kad",
"macros",
Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::net::Ipv4Addr;
use clap::Parser;
use libp2p::futures::prelude::*;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{identify, identity, kad, ping, relay, rendezvous, Multiaddr, StreamProtocol, Swarm};
use libp2p::{
autonat, identify, identity, kad, ping, relay, rendezvous, Multiaddr, StreamProtocol, Swarm,
};
use tracing::info;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;
Expand All @@ -13,11 +15,12 @@ const CALIMERO_KAD_PROTO_NAME: StreamProtocol = StreamProtocol::new("/calimero/k

#[derive(NetworkBehaviour)]
struct Behaviour {
auto_nat: autonat::Behaviour,
identify: identify::Behaviour,
kad: kad::Behaviour<kad::store::MemoryStore>,
ping: ping::Behaviour,
rendezvous: rendezvous::server::Behaviour,
relay: relay::Behaviour,
rendezvous: rendezvous::server::Behaviour,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -61,6 +64,7 @@ async fn main() -> eyre::Result<()> {
)?
.with_quic()
.with_behaviour(|keypair| Behaviour {
auto_nat: autonat::Behaviour::new(peer_id.clone(), Default::default()),
identify: identify::Behaviour::new(identify::Config::new(
PROTOCOL_VERSION.to_owned(),
keypair.public(),
Expand Down Expand Up @@ -127,6 +131,9 @@ async fn handle_swarm_event(swarm: &mut Swarm<Behaviour>, event: SwarmEvent<Beha

async fn handle_swarm_behaviour_event(swarm: &mut Swarm<Behaviour>, event: BehaviourEvent) {
match event {
BehaviourEvent::AutoNat(event) => {
info!("AutoNat event: {event:?}");
}
BehaviourEvent::Identify(event) => {
info!("Identify event: {event:?}");
match event {
Expand All @@ -143,12 +150,12 @@ async fn handle_swarm_behaviour_event(swarm: &mut Swarm<Behaviour>, event: Behav
BehaviourEvent::Kad(event) => {
info!("Kad event: {event:?}");
}
BehaviourEvent::Rendezvous(event) => {
info!("Rendezvous event: {event:?}");
}
BehaviourEvent::Relay(event) => {
info!("Relay event: {event:?}");
}
BehaviourEvent::Rendezvous(event) => {
info!("Rendezvous event: {event:?}");
}
_ => {}
}
}

0 comments on commit d50b49b

Please sign in to comment.