Skip to content

Commit

Permalink
new ep
Browse files Browse the repository at this point in the history
  • Loading branch information
realtux committed Aug 1, 2021
1 parent 5d85eda commit db2917c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
8 changes: 8 additions & 0 deletions 139/notes.yml
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/
18 changes: 9 additions & 9 deletions 140/commands.txt
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
61 changes: 61 additions & 0 deletions 141/client.py
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))
29 changes: 29 additions & 0 deletions 141/server.py
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)

0 comments on commit db2917c

Please sign in to comment.