From 38b9c075665b6ff449d71ed127dcce019c22c44c Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sun, 15 Feb 2015 09:53:59 -0800 Subject: [PATCH] sketch out transit-client API --- bin/receive_file.py | 23 ++++++++++++++++------- bin/send_file.py | 29 ++++++++++++++++++----------- src/wormhole/blocking/transit.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 src/wormhole/blocking/transit.py diff --git a/bin/receive_file.py b/bin/receive_file.py index 0dc53a1..2ef9695 100644 --- a/bin/receive_file.py +++ b/bin/receive_file.py @@ -2,25 +2,34 @@ from __future__ import print_function import os, sys, json from binascii import unhexlify from nacl.secret import SecretBox -from .transcribe import Receiver +from wormhole.blocking.transcribe import Receiver +from wormhole.codes import input_code_with_completion +from wormhole.blocking.transit import TransitReceiver APPID = "lothar.com/wormhole/file-xfer" -RELAY = "example.com" # we're receiving -code = sys.argv[1] -blob = b"" -r = Receiver(APPID, blob, code) +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) +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,)) + xfer_key = unhexlify(them_d["xfer_key"].encode("ascii")) filename = os.path.basename(them_d["filename"]) # unicode filesize = them_d["filesize"] -relay = them_d["relay"].encode("ascii") # now receive the rest of the owl -encrypted = RECEIVE(relay) +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) +encrypted = transit_receiver.receive() decrypted = SecretBox(xfer_key).decrypt(encrypted) diff --git a/bin/send_file.py b/bin/send_file.py index 1c111ca..dc6a93f 100644 --- a/bin/send_file.py +++ b/bin/send_file.py @@ -3,27 +3,31 @@ import os, sys, json from binascii import hexlify from nacl.secret import SecretBox from nacl import utils -from .transcribe import Initiator +from wormhole.blocking.transcribe import Initiator +from wormhole.blocking.transit import TransitSender APPID = "lothar.com/wormhole/file-xfer" -RELAY = "example.com" # we're sending filename = sys.argv[1] assert os.path.isfile(filename) xfer_key = utils.random(SecretBox.KEY_SIZE) -blob = json.dumps({"xfer_key": hexlify(xfer_key), +transit_sender = TransitSender() +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, - "relay": RELAY, + "direct_connection_hints": direct_hints, + "relay_connection_hints": relay_hints, }).encode("utf-8") -i = Initiator(APPID, blob) + +i = Initiator(APPID, data) code = i.get_code() +print("On the other computer, please run: receive_file") print("Wormhole code is '%s'" % code) -print("On the other computer, please run:") -print() -print(" wormhole-receive-file %s" % code) -print() +print("") them_bytes = i.get_data() them_d = json.loads(them_bytes.decode("utf-8")) print("them: %r" % (them_d,)) @@ -34,6 +38,9 @@ with open(filename, "rb") as f: nonce = utils.random(SecretBox.NONCE_SIZE) encrypted = box.encrypt(plaintext, nonce) -# now draw the rest of the owl -SEND(RELAY, encrypted) +transit_sender.add_receiver_hints(them_d["direct_connection_hints"]) +transit_sender.establish_connection(IDS) +transit_sender.write(encrypted) +transit_sender.close() + print("file sent") diff --git a/src/wormhole/blocking/transit.py b/src/wormhole/blocking/transit.py new file mode 100644 index 0000000..84877d2 --- /dev/null +++ b/src/wormhole/blocking/transit.py @@ -0,0 +1,30 @@ + +class TransitSender: + def __init__(self, IDS): + pass + def get_direct_hints(self): + pass + def get_relay_hints(self): + return [] + def add_receiver_hints(self, hints): + self.receiver_hints = hints + def establish_connection(self): + pass + def write(self, data): + pass + def close(self): + pass + +class TransitReceiver: + def __init__(self, IDS): + pass + def get_direct_hints(self): + pass + def add_sender_direct_hints(self, hints): + self.sender_direct_hints = hints + def add_sender_relay_hints(self, hints): + self.sender_relay_hints = hints + def establish_connection(self): + pass + def receive(self): + pass