How's it going with p2p? #2
Pinned
novemus
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello geek!
In this article, we will talk about the purpose and design of a small but useful network tool called plexus. This tool is written in C++. But it is likely that this will be interesting not only to C++ programmers, but also to anyone interested in network programming.
Motivation
Everyone can see how radically digital technologies have changed the world. Everybody migrated to the Internet, quite literally.
Scientists and programmers were the first, advanced youth followed them, then business came and everything started spinning.
Social networks arose, IT giants were born. The clumsy state also came with a delay. In the nineties, the Internet was the territory of true freedom or even anarchy. But now it's becoming more like the Matrix or some kind of it. The paradox is that the Internet architecture designed as decentralized, but the social infrastructure hosted on it is moving towards hyper-centralisation. The appearance of the Torrent, Tor or Bitcoin is like a riot on a ship that went off course. There are many reasons for such trend, but main one is that the Internet, although it helps to overcome borders, allows to collect and analyse a large amount of social information. As the result, this leads to illegal manipulations of data, attempts to direct a social life, and even invasions to the private spaces of citizens. The evolution of AI makes it all worse. At least, it's not very pleasant to feel like a fish in an aquarium. Although the virtual reality is a projection of the real life, it is very restricted in one important ability. This is the ability to make and maintain relationships without intermediaries. Secure and distributed social networks based on peer-to-peer solutions are still quite exotic. However, there is a evident societal demand for such things. One of the constraints is that the IPv4 networks and the NAT are still widespread. That's what we're going to talk about. Who is interested, can eat the red pill and go to the severe Zion. The blue pill will bring you back to the world of sweet dreams.
The NAT has solved the problem of the restricted 32-bits address space. However, NAT forces us to use third parties for private
communications, when our hosts are placed in different private networks. Most users are used to this and have no difficulties until they start thinking about safety and privacy of communications. But the programmers have been working on this for a long time and they have already come up with ways to protect your privacy. Overcoming NAT for P2P connections is used in some types of software, for example, IP-telephony, remote access systems, some messengers, distributed file systems, etc. But all these solutions are embedded in the relevant software and cannot be used separately. It would be great to have a universal tool of this kind. The
plexus
tool is the result of efforts to do this. To overcome NAT it implements the well-known UDP hole punching technique. We won't immerse into its details. Those who wish can find it on the Web. But further, it is assumed that the reader is familiar with it. Otherwise, skip the description of technical details and read about using of theplexus
tool.How it works
To put it simply, the
plexus
instances running on local and remote hosts synchronously punch holes in their NATs to each other direction and pass the control to specified applications. Also they pass to apps an endpoint of local interface from which punched a NAT, an endpoint issued by NAT on the public interface, a public endpoint of a peer and a session verification key. Theplexus
needs of an accessible STUN service to investigate NAT and access to email service to exchange investigated public endpoints with a peer. NATs have to use the independent mapping policy to map the client endpoint to endpoint of the public interface. Thus, for two outgoing packets from the same endpoint (address and port) to different destinations the NAT must assign the same public address and port. The filtering policy doesn't matter. The schema below shows the procedure in more detail.After start, the
plexus
investigates is there a NAT and is its mapping policy appropriate, with the help of STUN service. If there is no NAT or NAT policy is suitable theplexus
proceeds to the endpoint exchange procedure. One of the sides must run as an acceptor of peer connection requests. The other one has to initiate connection requests.NAT punching procedure
plexus
have seen any packet from the peer.Thus, the handshake is considered completed after the sides received and sent packets with the flag set to 1. Now the control can be passed to the specified programs.
Visit the repository to read the build manual or download prebuild binaries.
Usage
One of the cases you can use the
plexus
when you want to connect to a host behind the NAT and there is no mutually accessible VPN.Run
plexus
on remote host with accepting mode, set flag --accept. Optionally, you can set packet TTL with key --punch-hops, as we discussed above. By default it's set to 7. To determine actual TTL you can use some trace tools, for exampletraceroute
. This argument matters only for accepting side.plexus --accept --email-smtps=smtp.peermailer.com:xxx --email-imaps=imap.peermailer.com:xxx --email-login=peerlogin --email-passwd=peerpassword --email-from=peerhost@peermailer.com --email-to=yourhost@yourmailer.com --host-id=remote --peer-id=local --stun-server=stun.someserver.com --stun-client=xxx.xxx.xxx.xxx:xx --exec-command=~/plexus/exec.sh
When you need to connect, run
plexus
on the local host as initiator, without flag --accept.plexus --email-smtps=smtp.yourmailer.com:xxx --email-imaps=imap.yourmailer.com:xxx --email-login=yourlogin --email-passwd=yourpassword --email-from=yourhost@yourmailer.com --email-to=peerhost@peermailer.com --host-id=local --peer-id=remote --stun-server=stun.someserver.com --stun-client=xxx.xxx.xxx.xxx:xx --exec-command=~/plexus/exec.sh
If everything went well, the control will be passed to specified applications you set with key --exec-command. You can pass arguments to your application with key --exec-args and use wildcards:
plexus
, set by --stun-client or defaultplexus
, set by --stun-client or defaultIf you don't pass the --exec-args, the application will be invoked as follows:
/path/to/exec/command innerip innerport outerip outerport peerip peerport secret
For tests, you can use exec.sh and exec.bat scripts from the repository. The scripts are aimed for creating a VPN tunnel between machines and require
openvpn
installed, which needs administrative privileges. Of course, you can use your own scripts. To increase security level you can force to use S/MIME protocol for IMAP/SMTP communications. Pass --help to see all available arguments.Code
The following is a concise description of the code interfaces. The code is not complicated and structured and you'll figure it out without difficulty. The main entities are the
mediator
that's responsible for SMTP and IMAP communication to exchange endpoints and thepuncher
that communicates with STUN server, punches a UDP hole and performs handshake procedure.The names of the
mediator
methods speak for themselves. The reference type contains a peer endpoint and a puzzle which is a randomly generated number. Two numbers from each side form a verification key.Puncher methods
There's one important thing the
plexus
can't do well - bind TCP applications. This is because the NAT punching doesn't work well for TCP connections. But for such cases, there is the tunneling tool wormhole which can be used with theplexus
.Thank you for your time.
Beta Was this translation helpful? Give feedback.
All reactions