Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/dcutr/examples: Wait to tell relay its public addr #2659

Merged
merged 5 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 32 additions & 40 deletions protocols/dcutr/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,69 +186,55 @@ fn main() -> Result<(), Box<dyn Error>> {
}
});

match opts.mode {
Mode::Dial => {
swarm.dial(opts.relay_address.clone()).unwrap();
}
Mode::Listen => {
swarm
.listen_on(opts.relay_address.clone().with(Protocol::P2pCircuit))
.unwrap();
}
}

// Wait till connected to relay to learn external address. In case we are in listening mode,
// wait for the relay to accept our reservation request.
// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
// our local public address and (b) enable a freshly started relay to learn its public address.
swarm.dial(opts.relay_address.clone()).unwrap();
block_on(async {
let mut learned_observed_addr = false;
let mut relay_accepted_reservation = false;
let mut told_relay_observed_addr = false;

loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { .. } => {}
SwarmEvent::Dialing { .. } => {}
SwarmEvent::ConnectionEstablished { .. } => {}
SwarmEvent::Behaviour(Event::Ping(_)) => {}
SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted {
..
})) => {
info!("Relay accepted our reservation request.");
relay_accepted_reservation = true
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {
info!("Told relay its public address.");
told_relay_observed_addr = true;
}
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {}
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received {
info: IdentifyInfo { observed_addr, .. },
..
})) => {
info!("Relay observes us under the address: {:?}", observed_addr);
info!("Relay told us our public address: {:?}", observed_addr);
learned_observed_addr = true;
}
event => panic!("{:?}", event),
}

// Check whether we are done.

if !learned_observed_addr {
continue;
}

if opts.mode == Mode::Listen && !relay_accepted_reservation {
continue;
if learned_observed_addr && told_relay_observed_addr {
break;
}

break;
}
});

if opts.mode == Mode::Dial {
swarm
.dial(
opts.relay_address
.clone()
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(opts.remote_peer_id.unwrap().into())),
)
.unwrap();
match opts.mode {
Mode::Dial => {
swarm
.dial(
opts.relay_address
.clone()
.with(Protocol::P2pCircuit)
mxinden marked this conversation as resolved.
Show resolved Hide resolved
.with(Protocol::P2p(opts.remote_peer_id.unwrap().into())),
)
.unwrap();
}
Mode::Listen => {
swarm
.listen_on(opts.relay_address.with(Protocol::P2pCircuit))
.unwrap();
}
}

block_on(async {
Expand All @@ -257,6 +243,12 @@ fn main() -> Result<(), Box<dyn Error>> {
SwarmEvent::NewListenAddr { address, .. } => {
info!("Listening on {:?}", address);
}
SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted {
..
})) => {
assert!(opts.mode == Mode::Listen);
info!("Relay accepted our reservation request.");
}
SwarmEvent::Behaviour(Event::Relay(event)) => {
info!("{:?}", event)
}
Expand Down
4 changes: 3 additions & 1 deletion src/tutorials/hole_punching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@
//! [2022-05-11T10:38:52Z INFO client] Local peer id: PeerId("XXX")
//! [2022-05-11T10:38:52Z INFO client] Listening on "/ip4/127.0.0.1/tcp/44703"
//! [2022-05-11T10:38:52Z INFO client] Listening on "/ip4/XXX/tcp/44703"
//! [2022-05-11T10:38:54Z INFO client] Relay told us our public address: "/ip4/XXX/tcp/53160"
//! [2022-05-11T10:38:54Z INFO client] Told relay its public address.
//! [2022-05-11T10:38:54Z INFO client] Relay accepted our reservation request.
//! [2022-05-11T10:38:54Z INFO client] Relay observes us under the address: "/ip4/XXX/tcp/53160"
//! [2022-05-11T10:38:54Z INFO client] Listening on "/ip4/$RELAY_SERVER_IP/tcp/4001/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN/p2p-circuit/p2p/XXX"
//! ```
//!
//! Now let's make sure that the listening client is not public, in other words let's make sure one
Expand Down