rust-libp2p icon indicating copy to clipboard operation
rust-libp2p copied to clipboard

webrtc: wrong peer id when connecting to peer via relay

Open dariusc93 opened this issue 1 year ago • 7 comments

Summary

When attempting to connect to a peer (from the browser) through a relay with webrtc-direct transport, it gives a WrongPeerId. This seems to happen because it is reporting/expecting the relay peer id instead the actual peer id whom we are connecting to via relay, likely coming from the outbound upgrade in webrtc-websys.

Expected behavior

Connect to peer over relay

Actual behavior

Errors with wrong peer id when attempting to connect to remote peer via relay

Relevant log output

/home/darius/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libp2p-swarm-0.44.2/src/lib.rs:843 Connection attempt to peer failed with WrongPeerId { obtained: PeerId("12D3KooWSsn13GxHchpG6dtr7o6ARqSkcMtsBuojgL9XU9t1M1uE"), endpoint: Dialer { address: "/ip4/XXX.XXX.XXX.XXX/udp/XXXX/webrtc-direct/certhash/uEiAX9LacN1_nm3t-qLg7Ap7g3GMiAoJgWTPwfueva3xdlg/p2p/12D3KooWSsn13GxHchpG6dtr7o6ARqSkcMtsBuojgL9XU9t1M1uE/p2p-circuit/p2p/12D3KooWR8YyQ3TxpkdS6QYNDjC18XtyqBxwaPGVMGraDmpmchqQ", role_override: Dialer } }. peer = 12D3KooWR8YyQ3TxpkdS6QYNDjC18XtyqBxwaPGVMGraDmpmchqQ;

Possible Solution

If intended, might be worth to document this until browser-to-browser is implemented (which may be the best solution), otherwise, we might need to report the correct peer id when connecting over relay (would need to evaluate other libp2p impl in how they handle relay v2 with webrtc-direct)

Version

0.53

Would you like to work on fixing this bug ?

Maybe

dariusc93 avatar Jun 07 '24 19:06 dariusc93

I have a similar problem, with a setup like this:

node1 (in browser) based on rust-libp2p as wasm with websocket_sys & webrtc_sys node2 as a relay with websocket transport + quic node3 connecting to relay with quic transport and webrtc

I want node1 to connect to relay (websocket) which then will share information with node3 so that I end up with direct webrtc connection between node1 & node3.

From node1 I'm trying to connect to /ip4/IP/tcp/PORT/ws/p2p/RELAY_PEER_ID/p2p-circuit/webrtc-direct/certhash/HASH/p2p/NODE3_PEER_ID. I see that it's connected to relay (devtools > ws), but unable to connect to node3: error: WrongPeerId.

It this setup even feasible? And if not, what would be correct way, so that I end up with direct connection between node1 & node3 (given that both are behind NAT)?

I'm using master branch for rust-libp2p since currently it's 0.53.0 on Cargo, but 0.54.0 on github

sqrtsanta avatar Jul 30 '24 12:07 sqrtsanta

Hey @sqrtsanta. If youre using another transport like websocket, tcp, or quic, you do not need to include webrtc-direct/certhash/HASH/ in apart of the relay. Instead you would use (in your case) /ip4/IP/tcp/PORT/ws/p2p/RELAY_PEER_ID/p2p-circuit/p2p/NODE3_PEER_ID.

dariusc93 avatar Jul 30 '24 12:07 dariusc93

@dariusc93 Thanks for answer, unfortunately it still doesn't work. Does node3 - relay connection have to be same transport as node1 - relay, e.g. webrtc or websocket? Or can it be arbitrary like quic?

sqrtsanta avatar Jul 30 '24 12:07 sqrtsanta

@dariusc93 Thanks for answer, unfortunately it still doesn't work. Does node3 - relay connection have to be same transport as node1 - relay, e.g. webrtc or websocket? Or can it be arbitrary like quic?

If youre doing it from the browser then unfortunately it would have to be websocket. In other words, your public relay would have to be listening on websocket (either ws or wss) with your browser connecting to that specific node at that address and establish a reservation.

For webrtc, I have already concluded that to use the relay it would require browser-to-browser (unless this was a bug in the design - hard to say for sure but based on js and go impl i assume its not a bug but could be wrong).

dariusc93 avatar Jul 30 '24 12:07 dariusc93

Hi, was anyone able to connect both nodes directly behind NATs, or only via a relay? If yes, can you share the solution? Did you do it 100% in libp2p/Rust, or did you use libp2p/js in the browser?

cr-RonZigelman avatar Mar 30 '25 10:03 cr-RonZigelman

Hi, was anyone able to connect both nodes directly behind NATs, or only via a relay? If yes, can you share the solution? Did you do it 100% in libp2p/Rust, or did you use libp2p/js in the browser?

Hey @cr-RonZigelman. webrtc is mostly meant for browser-to-node (via webrtc-direct). There is no support for relay here yet (which would enable browser-to-browser once there is support).

dariusc93 avatar Mar 30 '25 14:03 dariusc93

I was able to resolve the WrongPeerId issue by noting that I hadn't:

  1. Enabled a relay feature flag,
  2. Added a relay_client: relay::client::Behaviour, field to my Behaviour struct

ThouCheese avatar Nov 25 '25 00:11 ThouCheese