Python实现NAT穿透+STUN+TURN+P2P聊天 | Python P2P chat
Switch branches/tags
Nothing to show
Clone or download
Latest commit 3b9fcf6 Jun 28, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Allow user to use Crtl+C to exit #4 Apr 4, 2017
LICENSE Update LICENSE Jun 28, 2017
README.md Update README.md Dec 21, 2016
client.py beautify Apr 4, 2017
server.py 有未识别或者错误的NAT类型则启用转发模式, 未测试 Jun 26, 2014
stun.py README Mar 23, 2014

README.md

PyPunchP2P

THIS PROJECT IS FOR STUDYING AND VERIFICATION, DON'T USE IT IN PRODUCTION.

Python p2p chat client/server with built-in NAT traversal (UDP hole punching).
I've written an article about the detailed implementation (in Chinese).

Based on
koenbollen's gist
pystun
Peer-to-Peer Communication Across Network Address Translators

Python edition: py2.6+ but no Python 3 support
Platform: Linux/Windows

Usage

Suppose you run server.py on a VPS with ip 1.2.3.4, listening on port 5678

$ server.py 5678

On client A and client B (run this on both clients):

$ client.py 1.2.3.4 5678 100  

The number 100 is used to match clients, you can choose any number you like but only clients with the same number will be linked by server. If two clients get linked, two people can chat by typing in terminal, and once you hit <ENTER> your partner will see your message in his terminal.
Encoding is a known issue since I didn't pay much effort on making this tool perfect, but as long as you type English it will be fine.

Test Mode

You could do simulation testing by specifying a fourth parameter of client.py, it will assume that your client is behind a specific type of NAT device.

Here are the corresponding NAT type and number:

FullCone         0  
RestrictNAT      1  
RestrictPortNAT  2  
SymmetricNAT     3   

So you might run

$ client.py 1.2.3.4 5678 100 1

pretending your client is behind RestrictNAT.
You can test the relay server functionality by making 3 as the forth parameter, since if one client is behind symmetric NAT, there will be no direct connection but server forwaring.

License

MIT