Created
April 25, 2016 05:18
-
-
Save Highstaker/3f7131977cdddbfbcbf92e7544ba5a34 to your computer and use it in GitHub Desktop.
A simple hole-punching system with sender, receiver and introductor. Launch introductor first, then sender, then receiver. Presumably, may work with full-cone NAT, but not the other type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from socket import AF_INET, SOCK_DGRAM | |
import socket | |
PORT = 9091 | |
sock = socket.socket(AF_INET, SOCK_DGRAM) | |
# assign socket to host (empty for any) and port (as tuple, not as two params!) | |
sock.bind(('', PORT)) | |
print("binded to port", PORT) | |
adreses = [] | |
while True: | |
data, addr = sock.recvfrom(1024) | |
print(addr, "Data:",data) | |
if data.decode("utf-8") == "ping": | |
adreses += [addr] | |
sock.sendto(b"pong", addr) | |
if len(adreses) >= 2: | |
# Once there are two addresses, send address of one peer to another and vice versa | |
adr1 = adreses.pop(0) | |
adr2 = adreses.pop(0) | |
send_to1 = adr2 | |
send_to2 = adr1 | |
print("Sending data about receiver to sender") | |
sock.sendto(" ".join(map(str,send_to1)).encode("utf-8"),adr1) | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import socket | |
from socket import AF_INET, SOCK_DGRAM | |
from time import sleep | |
HOST = 'localhost' | |
NAT_PORT = 9091 | |
PORT = 44568 | |
NAT_ADDR = (HOST,NAT_PORT) | |
sock = socket.socket(AF_INET, SOCK_DGRAM) | |
sock.bind(('', PORT)) | |
sock.sendto(b"ping", NAT_ADDR) | |
# collect the data about other peer | |
data = sock.recv(1024) | |
data_text = data.decode("utf-8") | |
print("Reply is:", data) | |
######################## | |
#Establish P2P | |
######################## | |
data = sock.recv(1024) | |
print("Reply is:", data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from socket import AF_INET, SOCK_DGRAM | |
import socket | |
from time import sleep | |
HOST = 'localhost' | |
NAT_PORT = 9091 | |
PORT = 44567 | |
NAT_ADDR = (HOST,NAT_PORT) | |
sock = socket.socket(AF_INET, SOCK_DGRAM) | |
sock.bind(('', PORT)) | |
sock.sendto(b"ping", NAT_ADDR) | |
data = sock.recv(1024) | |
print("Reply is:" + data.decode("utf-8")) | |
# collect the data about other peer | |
data = sock.recv(1024) | |
print("Reply is:" + data.decode("utf-8")) | |
data = data.decode("utf-8").split(" ") | |
PEER_ADDR = data[0] | |
PEER_PORT = int(data[1]) | |
PEER_FULL_ADDR = (PEER_ADDR, PEER_PORT) | |
##################### | |
#Establish P2P | |
##################### | |
sleep(1) | |
sock.sendto(b"Hello, receiver!", PEER_FULL_ADDR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment