From 9dd4c6039f93ada1ab6b31f4811b718b1c202ad4 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sun, 15 Feb 2015 14:42:59 -0800 Subject: [PATCH] better sketch of transit-client API --- bin/receive_file.py | 29 +++++++++++++++++------------ bin/send_file.py | 24 +++++++++++++++--------- src/wormhole/blocking/transit.py | 12 +++++++++--- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/bin/receive_file.py b/bin/receive_file.py index 2ef9695..00c3b08 100644 --- a/bin/receive_file.py +++ b/bin/receive_file.py @@ -12,23 +12,28 @@ APPID = "lothar.com/wormhole/file-xfer" transit_receiver = TransitReceiver() direct_hints = transit_receiver.get_direct_hints() -data = json.dumps({"direct_connection_hints": direct_hints, - }).encode("utf-8") -r = Receiver(APPID, data) +mydata = json.dumps({ + "transit": { + "direct_connection_hints": direct_hints, + }, + }).encode("utf-8") +r = Receiver(APPID, mydata) r.set_code(r.input_code("Enter receive-text wormhole code: ")) -them_bytes = r.get_data() -them_d = json.loads(them_bytes.decode("utf-8")) -print("them: %r" % (them_d,)) +data = json.loads(r.get_data().decode("utf-8")) +print("their data: %r" % (data,)) -xfer_key = unhexlify(them_d["xfer_key"].encode("ascii")) -filename = os.path.basename(them_d["filename"]) # unicode -filesize = them_d["filesize"] +file_data = data["file"] +xfer_key = unhexlify(file_data["key"].encode("ascii")) +filename = os.path.basename(file_data["filename"]) # unicode +filesize = file_data["filesize"] # now receive the rest of the owl -transit_receiver.add_sender_direct_hints(them_d["direct_connection_hints"]) -transit_receiver.add_sender_relay_hints(them_d["relay_connection_hints"]) -transit_receiver.establish_connection(IDS) +tdata = data["transit"] +transit_receiver.set_transit_key(tdata["key"]) +transit_receiver.add_sender_direct_hints(tdata["direct_connection_hints"]) +transit_receiver.add_sender_relay_hints(tdata["relay_connection_hints"]) +transit_receiver.establish_connection() encrypted = transit_receiver.receive() decrypted = SecretBox(xfer_key).decrypt(encrypted) diff --git a/bin/send_file.py b/bin/send_file.py index dc6a93f..9f7537c 100644 --- a/bin/send_file.py +++ b/bin/send_file.py @@ -2,7 +2,6 @@ from __future__ import print_function import os, sys, json from binascii import hexlify from nacl.secret import SecretBox -from nacl import utils from wormhole.blocking.transcribe import Initiator from wormhole.blocking.transit import TransitSender @@ -11,17 +10,24 @@ APPID = "lothar.com/wormhole/file-xfer" # we're sending filename = sys.argv[1] assert os.path.isfile(filename) -xfer_key = utils.random(SecretBox.KEY_SIZE) +xfer_key = os.urandom(SecretBox.KEY_SIZE) transit_sender = TransitSender() +transit_key = transit_sender.get_transit_key() direct_hints = transit_sender.get_direct_hints() relay_hints = transit_sender.get_relay_hints() -data = json.dumps({"xfer_key": hexlify(xfer_key), - "filename": os.path.basename(filename), - "filesize": os.stat(filename).st_size, - "direct_connection_hints": direct_hints, - "relay_connection_hints": relay_hints, - }).encode("utf-8") +data = json.dumps({ + "file": { + "key": hexlify(xfer_key), + "filename": os.path.basename(filename), + "filesize": os.stat(filename).st_size, + }, + "transit": { + "key": hexlify(transit_key), + "direct_connection_hints": direct_hints, + "relay_connection_hints": relay_hints, + }, + }).encode("utf-8") i = Initiator(APPID, data) code = i.get_code() @@ -39,7 +45,7 @@ nonce = utils.random(SecretBox.NONCE_SIZE) encrypted = box.encrypt(plaintext, nonce) transit_sender.add_receiver_hints(them_d["direct_connection_hints"]) -transit_sender.establish_connection(IDS) +transit_sender.establish_connection() transit_sender.write(encrypted) transit_sender.close() diff --git a/src/wormhole/blocking/transit.py b/src/wormhole/blocking/transit.py index 84877d2..14bdb47 100644 --- a/src/wormhole/blocking/transit.py +++ b/src/wormhole/blocking/transit.py @@ -1,7 +1,11 @@ +import os +from ..util import ipaddrs class TransitSender: - def __init__(self, IDS): - pass + def __init__(self): + self.key = os.urandom(32) + def get_transit_key(self): + return self.key def get_direct_hints(self): pass def get_relay_hints(self): @@ -16,10 +20,12 @@ class TransitSender: pass class TransitReceiver: - def __init__(self, IDS): + def __init__(self): pass def get_direct_hints(self): pass + def set_transit_key(self, key): + self.key = key def add_sender_direct_hints(self, hints): self.sender_direct_hints = hints def add_sender_relay_hints(self, hints):