networking
This commit is contained in:
parent
27ccbc0141
commit
657eea8fc1
@ -7,6 +7,14 @@
|
|||||||
|
|
||||||
Aby wyjść ze środowiska uruchom komendę `deactivate`.
|
Aby wyjść ze środowiska uruchom komendę `deactivate`.
|
||||||
|
|
||||||
|
## Testowanie działania
|
||||||
|
|
||||||
|
1. Na komputerze A uruchom program `alice.py` podając jako argumenty dwa pliki, z których Bob może wybrać jeden
|
||||||
|
2. Na komputerze B uruchom program `bob.py` podając jako argument który z plików ma uruchomić (`0` lub `1`) oraz adres IP komputera A przez flagę `--ip`.
|
||||||
|
|
||||||
|
W razie wątpliwości zapoznaj się z pomocą programów `alice.py` i `bob.py` lub zobacz przykładowy test w `test.sh`.
|
||||||
|
|
||||||
|
|
||||||
## Dodawanie paczek
|
## Dodawanie paczek
|
||||||
|
|
||||||
W środowisku wirtualnym:
|
W środowisku wirtualnym:
|
||||||
|
30
alice.py
Normal file
30
alice.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import argparse
|
||||||
|
import socket
|
||||||
|
import contextlib
|
||||||
|
from otp import alice
|
||||||
|
from net import send, send_point, recv, recv_point
|
||||||
|
|
||||||
|
def main():
|
||||||
|
p = argparse.ArgumentParser()
|
||||||
|
p.add_argument('msg0', help='First file which Bob can receive')
|
||||||
|
p.add_argument('msg1', help='Second file which Bob can receive')
|
||||||
|
p.add_argument('-p', '--port', type=int, default=8080)
|
||||||
|
args = p.parse_args()
|
||||||
|
|
||||||
|
with open(args.msg0, 'rb') as f0, open(args.msg1, 'rb') as f1:
|
||||||
|
m0 = f0.read()
|
||||||
|
m1 = f1.read()
|
||||||
|
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
s.bind(('0.0.0.0', args.port))
|
||||||
|
s.listen(1)
|
||||||
|
bob, address = s.accept()
|
||||||
|
ip, _ = address
|
||||||
|
a = alice(m0, m1)
|
||||||
|
send_point(bob, a.send(None))
|
||||||
|
send(bob, a.send(recv_point(bob)))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
27
bob.py
Normal file
27
bob.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import argparse
|
||||||
|
import socket
|
||||||
|
from otp import bob
|
||||||
|
from net import send, send_point, recv, recv_point
|
||||||
|
import contextlib
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
p = argparse.ArgumentParser()
|
||||||
|
p.add_argument('c', type=int, help="Which message to choose? [0, 1]")
|
||||||
|
p.add_argument('--ip', type=str, default='127.0.0.1')
|
||||||
|
p.add_argument('-p', '--port', type=int, default=8080)
|
||||||
|
args = p.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as alice:
|
||||||
|
alice.connect((args.ip, args.port))
|
||||||
|
with contextlib.suppress(StopIteration):
|
||||||
|
b = bob(args.c > 0)
|
||||||
|
b.send(None)
|
||||||
|
send_point(alice, b.send(recv_point(alice)))
|
||||||
|
response = b.send(recv(alice))
|
||||||
|
sys.stdout.buffer.write(response)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
29
net.py
Normal file
29
net.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import socket
|
||||||
|
import pickle
|
||||||
|
import struct
|
||||||
|
import ecpy.curves
|
||||||
|
from otp import curve
|
||||||
|
|
||||||
|
def send_point(s: socket.socket, point: ecpy.curves.Point):
|
||||||
|
send(s, (point.x, point.y))
|
||||||
|
|
||||||
|
def recv_point(s: socket.socket) -> ecpy.curves.Point:
|
||||||
|
point = recv(s)
|
||||||
|
return ecpy.curves.Point(point[0], point[1], curve)
|
||||||
|
|
||||||
|
def send(s: socket.socket, data):
|
||||||
|
data = pickle.dumps(data, protocol=4)
|
||||||
|
data = struct.pack('<I', len(data)) + data
|
||||||
|
s.sendall(data)
|
||||||
|
|
||||||
|
def recv(s: socket.socket):
|
||||||
|
length = s.recv(4)
|
||||||
|
length, *_ = struct.unpack('<I', length)
|
||||||
|
|
||||||
|
pickled = b""
|
||||||
|
toread = length
|
||||||
|
while toread:
|
||||||
|
read = s.recv(toread)
|
||||||
|
pickled += read
|
||||||
|
toread -= len(pickled)
|
||||||
|
return pickle.loads(pickled)
|
@ -57,7 +57,7 @@ def bob(c: bool):
|
|||||||
|
|
||||||
e0, e1, salt = yield B
|
e0, e1, salt = yield B
|
||||||
kc = H(curve.mul_point(b, A), salt)
|
kc = H(curve.mul_point(b, A), salt)
|
||||||
print(D(kc, e1 if c else e0))
|
yield D(kc, e1 if c else e0)
|
||||||
|
|
||||||
|
|
||||||
########################################################
|
########################################################
|
||||||
@ -73,7 +73,8 @@ def main():
|
|||||||
b.send(None)
|
b.send(None)
|
||||||
B = b.send(A)
|
B = b.send(A)
|
||||||
encrypted = a.send(B)
|
encrypted = a.send(B)
|
||||||
b.send(encrypted)
|
result = b.send(encrypted)
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
16
test.sh
Executable file
16
test.sh
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
readonly MSG=("to jest pierwsza wiadomość" "to jest druga wiadomość")
|
||||||
|
|
||||||
|
function do_test() {
|
||||||
|
python alice.py <(echo "${MSG[0]}") <(echo "${MSG[1]}") &
|
||||||
|
alice=$!
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
if [ "${MSG[$1]}" != "$(python bob.py $1)" ]; then
|
||||||
|
echo "Failed in test $1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
wait $alice
|
||||||
|
}
|
||||||
|
|
||||||
|
do_test 0
|
||||||
|
do_test 1
|
Loading…
Reference in New Issue
Block a user