-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
reasons: | ||
- it disrespects open source licenses and may expose you to liability | ||
- it may turn into a crutch that you'll depend heavily on and may have to pay for | ||
- your private code is transmitted to github | ||
- it can and will produce bad and/or unsafe code | ||
|
||
sources: | ||
- https://copilot.github.com/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
replace ip addresses as appropriate | ||
# replace ip addresses as appropriate | ||
|
||
run on machine 1 | ||
echo 'punch hole' | nc -u -p 50001 143.198.238.218 50002 | ||
run on machine 1 (147.182.184.196) | ||
echo 'punch hole' | nc -u -p 50001 143.198.173.180 50002 | ||
nc -u -l 50001 | ||
|
||
run on machine 2 | ||
echo 'punch hole' | nc -u -p 50001 143.198.185.20 50002 | ||
run on machine 2 (143.198.173.180) | ||
echo 'punch hole' | nc -u -p 50001 147.182.184.196 50002 | ||
nc -u -l 50001 | ||
|
||
run on machine 1 | ||
echo 'hello' | nc -u -p 50002 143.198.238.218 50001 | ||
run on machine 1 (147.182.184.196) | ||
echo 'hello' | nc -u -p 50002 143.198.173.180 50001 | ||
|
||
run on machine 2 | ||
echo 'hello' | nc -u -p 50002 143.198.185.20 50001 | ||
run on machine 2 (143.198.173.180) | ||
echo 'hello' | nc -u -p 50002 147.182.184.196 50001 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import socket | ||
import sys | ||
import threading | ||
|
||
rendezvous = ('147.182.184.215', 55555) | ||
|
||
# connect to rendezvous | ||
print('connecting to rendezvous server') | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.bind(('0.0.0.0', 50001)) | ||
sock.sendto(b'0', rendezvous) | ||
|
||
while True: | ||
data = sock.recv(1024).decode() | ||
|
||
if data.strip() == 'ready': | ||
print('checked in with server, waiting') | ||
break | ||
|
||
data = sock.recv(1024).decode() | ||
ip, sport, dport = data.split(' ') | ||
sport = int(sport) | ||
dport = int(dport) | ||
|
||
print('\ngot peer') | ||
print(' ip: {}'.format(ip)) | ||
print(' source port: {}'.format(sport)) | ||
print(' dest port: {}\n'.format(dport)) | ||
|
||
# punch hole | ||
# equiv: echo 'punch hole' | nc -u -p 50001 x.x.x.x 50002 | ||
print('punching hole') | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.bind(('0.0.0.0', sport)) | ||
sock.sendto(b'0', (ip, dport)) | ||
|
||
print('ready to exchange messages\n') | ||
|
||
# listen for | ||
# equiv: nc -u -l 50001 | ||
def listen(): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.bind(('0.0.0.0', sport)) | ||
|
||
while True: | ||
data = sock.recv(1024) | ||
print('\rpeer: {}\n> '.format(data.decode()), end='') | ||
|
||
listener = threading.Thread(target=listen, daemon=True); | ||
listener.start() | ||
|
||
# send messages | ||
# equiv: echo 'xxx' | nc -u -p 50002 x.x.x.x 50001 | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.bind(('0.0.0.0', dport)) | ||
|
||
while True: | ||
msg = input('> ') | ||
sock.sendto(msg.encode(), (ip, sport)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import socket | ||
|
||
known_port = 50002 | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.bind(('0.0.0.0', 55555)) | ||
|
||
while True: | ||
clients = [] | ||
|
||
while True: | ||
data, address = sock.recvfrom(128) | ||
|
||
print('connection from: {}'.format(address)) | ||
clients.append(address) | ||
|
||
sock.sendto(b'ready', address) | ||
|
||
if len(clients) == 2: | ||
print('got 2 clients, sending details to each') | ||
break | ||
|
||
c1 = clients.pop() | ||
c1_addr, c1_port = c1 | ||
c2 = clients.pop() | ||
c2_addr, c2_port = c2 | ||
|
||
sock.sendto('{} {} {}'.format(c1_addr, c1_port, known_port).encode(), c2) | ||
sock.sendto('{} {} {}'.format(c2_addr, c2_port, known_port).encode(), c1) |