shawwwn / Gole

A p2p hole punching/tunneling tool written in Go.
BSD 2-Clause "Simplified" License
103 stars 11 forks source link

how does gole work #2

Closed neel-bp closed 2 years ago

neel-bp commented 2 years ago

not really an issue but i am wondering how does gole work without an external server? I thought you absolutely need a server which can actually forward ports and is globally accessible. is it what server option is for? lets say i am trying to play a p2p game and both players need to know each other's ip and both are behind NAT and cannot forward ports. how would i do that?

shawwwn commented 2 years ago

Server/Client option does not concern the hole punching process, it is for the tunneling process AFTER a hole has been punched.

As for the tunneling process, 'server' means whatever machine that is at the receiving end of subsequent connections.

shawwwn commented 2 years ago

lets say i am trying to play a p2p game and both players need to know each other's ip and both are behind NAT and cannot forward ports. how would i do that?

LAN games usually have a host(server)/player(client) pair.

neel-bp commented 2 years ago

so does gole also handle the hole punching? i am having trouble understanding how if a pc which is behind several layers of NAT would be discoverable by another pc when some actual 3rd party server which actually is publicly accessible. kind of like n2n. i am actually trying to use a program that is peer to peer and none of the pcs are actually acting as server, its bidirectional. and when trying to establish a connection both need to know about each other ips. i understand if server mode is not for something like this. but i am wondering if it is possible. with maybe this holepunch option. I apologize for my ignorance, this is very interesting project, and this is also very clean implementation.

shawwwn commented 2 years ago

so does gole also handle the hole punching?

Yes.

i am having trouble understanding how if a pc which is behind several layers of NAT would be discoverable by another pc when some actual 3rd party server which actually is publicly accessible.

There is no discoverability feature in pure p2p hole punching. That feature you are looking for is called a STUN server. In Gole, both sides must know each other's public IPs and agree upon a pair of public ports prior to hole punch.

i am actually trying to use a program that is peer to peer and none of the pcs are actually acting as server, its bidirectional.

Sorry I can't offer more help with your game.

neel-bp commented 2 years ago

thanks for this information, i will read more on STUN and hole punching, i am closing the issue now.

AnimMouse commented 1 year ago

@shawwwn

In Gole, both sides must know each other's public IPs and agree upon a pair of public ports prior to hole punch.

How does this work exactly? In using a STUN server, computer A will contact the STUN server, making a NAT hole in the process due to outgoing connection to STUN server, and the STUN server will reply an IP address and a port as seen by the STUN server to computer A, and also with computer B. And then those 2 computers that got their public IP address and port from the STUN server will use that to connect with each other.

By connecting to a STUN server, a hole is punched as the NAT will add an entry to its table.

In Gole, both sides know each other's public IPs and agree a pair of public ports, but how can Gole make an entry to the NAT table? Does computer A, knowing computer B's IP and port, will make a NAT entry without connection since computer B is behind NAT and can't open ports?

shawwwn commented 1 year ago

By connecting to a STUN server, a hole is punched as the NAT will add an entry to its table.

STUN is not involved in the actual hole punching process. Its sole purpose is to relay information (such as each party's public IP address).

For example, If both A and B are behind NAT, they have no way to know each other's public IP addresses other than requesting information from a public third party server C, hence the STUN server.

Bottom line is, you don't necessarily need a STUN server to punch holes.

AnimMouse commented 1 year ago

So Gole works like the chownat that the 2 computers try to contact with each other using UDP packets, hopefully, computer B's NAT will assume that the packets from computer A are "responses" from computer B's attempt to connect to computer A, and then computer B's NAT will add an entry to its table. Am I right?

The problem with that method is most of NAT today will randomize the source port, without a STUN server, you can't know what source port does the other computer have.

I have tried using Gole in 2 instance of GitHub Codespaces trying to connect with each other

gole -v tcp 0.0.0.0:3333 4.194.117.141:4444 -op server -fwd=127.0.0.1:8080
gole -v tcp 0.0.0.0:4444 4.193.163.249:3333 -op client -fwd=127.0.0.1:8080

But I can't make it holepunch.

shawwwn commented 1 year ago

So Gole works like the chownat that the 2 computers try to contact with each other using UDP packets, hopefully, computer B's NAT will assume that the packets from computer A are "responses" from computer B's attempt to connect to computer A, and then computer B's NAT will add an entry to its table. Am I right?

Yes.

The problem with that method is most of NAT today will randomize the source port, without a STUN server, you can't know what source port does the other computer have.

If you have no control over the source port number from where your packets initiate (i.e., bind() does not work as it suppose to), then the whole thing will not work, even with STUN.

Most NATs today will honor local source port bind, given the chosen source port is not currently occupied -- meaning that no incoming packet has arrived before the port has any outgoing packet.