sketch out transit-client API

This commit is contained in:
Brian Warner 2015-02-15 09:53:59 -08:00
parent d6ef752152
commit 38b9c07566
3 changed files with 64 additions and 18 deletions

View File

@ -2,25 +2,34 @@ from __future__ import print_function
import os, sys, json import os, sys, json
from binascii import unhexlify from binascii import unhexlify
from nacl.secret import SecretBox 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" APPID = "lothar.com/wormhole/file-xfer"
RELAY = "example.com"
# we're receiving # we're receiving
code = sys.argv[1] transit_receiver = TransitReceiver()
blob = b"" direct_hints = transit_receiver.get_direct_hints()
r = Receiver(APPID, blob, code)
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_bytes = r.get_data()
them_d = json.loads(them_bytes.decode("utf-8")) them_d = json.loads(them_bytes.decode("utf-8"))
print("them: %r" % (them_d,)) print("them: %r" % (them_d,))
xfer_key = unhexlify(them_d["xfer_key"].encode("ascii")) xfer_key = unhexlify(them_d["xfer_key"].encode("ascii"))
filename = os.path.basename(them_d["filename"]) # unicode filename = os.path.basename(them_d["filename"]) # unicode
filesize = them_d["filesize"] filesize = them_d["filesize"]
relay = them_d["relay"].encode("ascii")
# now receive the rest of the owl # 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) decrypted = SecretBox(xfer_key).decrypt(encrypted)

View File

@ -3,27 +3,31 @@ import os, sys, json
from binascii import hexlify from binascii import hexlify
from nacl.secret import SecretBox from nacl.secret import SecretBox
from nacl import utils 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" APPID = "lothar.com/wormhole/file-xfer"
RELAY = "example.com"
# we're sending # we're sending
filename = sys.argv[1] filename = sys.argv[1]
assert os.path.isfile(filename) assert os.path.isfile(filename)
xfer_key = utils.random(SecretBox.KEY_SIZE) 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), "filename": os.path.basename(filename),
"filesize": os.stat(filename).st_size, "filesize": os.stat(filename).st_size,
"relay": RELAY, "direct_connection_hints": direct_hints,
"relay_connection_hints": relay_hints,
}).encode("utf-8") }).encode("utf-8")
i = Initiator(APPID, blob)
i = Initiator(APPID, data)
code = i.get_code() code = i.get_code()
print("On the other computer, please run: receive_file")
print("Wormhole code is '%s'" % code) print("Wormhole code is '%s'" % code)
print("On the other computer, please run:") print("")
print()
print(" wormhole-receive-file %s" % code)
print()
them_bytes = i.get_data() them_bytes = i.get_data()
them_d = json.loads(them_bytes.decode("utf-8")) them_d = json.loads(them_bytes.decode("utf-8"))
print("them: %r" % (them_d,)) print("them: %r" % (them_d,))
@ -34,6 +38,9 @@ with open(filename, "rb") as f:
nonce = utils.random(SecretBox.NONCE_SIZE) nonce = utils.random(SecretBox.NONCE_SIZE)
encrypted = box.encrypt(plaintext, nonce) encrypted = box.encrypt(plaintext, nonce)
# now draw the rest of the owl transit_sender.add_receiver_hints(them_d["direct_connection_hints"])
SEND(RELAY, encrypted) transit_sender.establish_connection(IDS)
transit_sender.write(encrypted)
transit_sender.close()
print("file sent") print("file sent")

View File

@ -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