johanhelsing / matchbox

Painless peer-to-peer WebRTC networking for rust wasm (and native!)
Apache License 2.0
918 stars 76 forks source link

Add handshake logic for negotiating closest network IP to peer #460

Open ConnorBP opened 2 months ago

ConnorBP commented 2 months ago

Currently matchbox always connects peers via public IP Address. I propose an extra step is added to the handshake process to detect peers located on the same LAN or WAN networks, perhaps via MAC address of the router / modem, then the process can proceed to connect the peers via the local address with a fallback to the external on fail.

Reasons:

  1. Some college campuses and business networks block incoming connections on their firewalls, thereby breaking peer to peer connections over external IP's
  2. When two people play on the same network, connecting over the internet may add unnecessary latency and points of failure.
  3. I'm sure there are other reasons I am forgetting at the moment

Other potential solutions to number 1 would involve using a relay server of some kind. This kind of defeats the purpose of peer to peer, but could be a good fallback option. In general more fallback options is good to have.

Side note: when the peers are set together and the traffic between them never starts, matchbox never fails (or perhaps fails silently). Some kind of error handling or timeout on new peer connection would also be a welcome addition to this handshaking process.