by apankrat · 4y ago · 0 comments
A good chunk of NAT devices will pick external port based on [src ip/port, dst ip/port] combo, not just [src ip/port], so "WAN IP/port" you get from STUN will get you nothing useful. Not by itself.
STUNs should be used for discovering the pattern in NAT port overloading logic and then using it to predict which port your peer will use towards you if you were to try and connect now.
That is, you need to know the overloading pattern and then also time stuff correctly.
For that reason you will need a rendezvous server and it's also the best to let the server drive the whole process (as opposed to what STUN-based setups do, which is to let clients do it).
PS. In my past life I made a P2P VPN called Hamachi, which used all this stuff very extensively.
Unfortunately, there hasn't been much progress on that funtionality, as the ipfs team seems to focus more on "crypto" lately. But it mostly works: http://docs.ipfs.io.ipns.localhost:8080/reference/cli/#ipfs-... and I guess this gives more context on relays: https://github.com/ipfs/go-ipfs/issues/7433#issuecomment-640...
Otherwise, there is a useful library of firewall traversal libraries in any webrtc implementation.
https://syncthing.net/ https://github.com/hyperswarm/hyperswarm (whole family hypercore, hyperdrive, hyperswarm, ...)
All peers simply lookup their external port via STUN and publish this information.
What do you suggest as a learning resource for things like this? The STUN RFC's? The Stevens' networking book?
Stevens' book is a must read, yes, but it has nothing even on NAT (iirc), leave alone on working around it. Look at how NAT works, what types of it exist, etc. Then look at "hole punching". For bonus points skim through p2p-hackers mailing list archives from 2003 and thereabouts.
Very little has changed in this area since mid-00's, people just rediscover and re-implement the same thing over and over again. Few get it 100% right (IMO) due to sticking to prediction being done client-side. That's inferior to the server doing it, because it gets you more precise timing and better prediction rate.