holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.07k stars 86 forks source link

[Newbie Question] How does peer punchhole happen? #113

Closed TheYellowArchitect closed 2 years ago

TheYellowArchitect commented 2 years ago

I was looking into IPFS and Hypercore/Hyperswarm. Haven't used either. My use-case is to be able to connect peers directly, without a master server. I was surprised Hyperswarm has this

My use-case question: Assume I have one Hyperswarm, and there are 3 peers in total, all online, all connected. If I turn offline 1 peer, then reboot that peer, can it connect to the other 2 peers without connecting to a "master server" (pre-known static server)?

KrishnaPG commented 2 years ago

If you know the other servers then you can use them as bootstrap servers to discover them directly.

mafintosh commented 2 years ago

Hyperswarm is heavily focused on holepunching, ie connecting peers directly, so yes that servers. It does this through the DHT for discovery and distributed holepunching (so never relying on one single node etc).

mafintosh commented 2 years ago

How the holepunching works, by itself, is pretty complex. I recently did a talk describing some of the techniques, and will share once that's online.

smoyer64 commented 2 years ago

But IIRC, at least one of those three peers has to have a public IP address. Hole-punching is a pretty well known technique (perhaps with a few implementation twists) - there's even a Wikipedia page for it.

IPFS has a similar hole-punching process encompassed in one of the libp2p libraries. There's a reasonably digestible article on the IPFS blog describing how this - https://blog.ipfs.io/2022-01-20-libp2p-hole-punching/.

mafintosh commented 2 years ago

in practice it's pretty complex due to different nats. a lot more to it these days (unfortunately) than in the past. you need a third party yes, but you also want to be able to survive that third party lying about nat data, in a peer to peer system, so in pracitce you want tons of peers to help you, each only providing partial information.

TheYellowArchitect commented 2 years ago

I apologize for not having noticed these posts earlier, I am surprised at the many answers, expected just a "yes" or "nope" after so much time had passed

If you know the other servers then you can use them as bootstrap servers to discover them directly.

But IIRC, at least one of those three peers has to have a public IP address.

So, the servers must have a static IP? To translate this question in my use-case above (the 3 peers example):

For the re-activating peer who has cached the IP (or whatever identification token) of the other 2 peers it had last connected, the cached IP must be a static IP?

But IIRC, at least one of those three peers has to have a public IP address. at least one

Could you explain the above? What does it depend on, in order to have a succesful re-connection?

I apologize for these gimmick questions. The answer/conclusion of the above use-case depends if I will spend at least a month fulltime working on a project using Hyperswarm (which obviously means this feature will be coded by me), and I don't want to start something complicated which has a feature which I misunderstood

mafintosh commented 2 years ago

When you holepunch you just need a peer between you and the one you want to connect to that can relay info between you. This node does NOT need a static ip, you just need to be able to talk to them. It could for example be a node both of you holepunched to in the past. Now to bootstrap this process, it’s obvs easier if the first node you use to help you holepunch, does indeed have a static ip. Like I said above, in practice it’s more complex as you want to use many nodes instead of just one as well for security reasons (trust as few nodes as possible)

TheYellowArchitect commented 2 years ago

Perfect. I thank you for the clear answer.

I didn't expect hyperswarm to really be this powerful, it sounds like magic - that it's able to accomplish this, I can't imagine the complexity of the code in order to have achieved this, all exposed neatly for me on an API to learn/use :+1: