file-xfer: add ack, print encouraging messages, remove noise

This commit is contained in:
Brian Warner 2015-02-19 16:30:24 -08:00
parent 0ba01b2ce7
commit 3ccd7bd61e
3 changed files with 24 additions and 14 deletions

View File

@ -20,7 +20,7 @@ r = Receiver(APPID, mydata)
r.set_code(r.input_code("Enter receive-file wormhole code: ")) r.set_code(r.input_code("Enter receive-file wormhole code: "))
data = json.loads(r.get_data().decode("utf-8")) data = json.loads(r.get_data().decode("utf-8"))
print("their data: %r" % (data,)) #print("their data: %r" % (data,))
file_data = data["file"] file_data = data["file"]
xfer_key = unhexlify(file_data["key"].encode("ascii")) xfer_key = unhexlify(file_data["key"].encode("ascii"))
@ -30,11 +30,11 @@ encrypted_filesize = filesize + SecretBox.NONCE_SIZE+16
# now receive the rest of the owl # now receive the rest of the owl
tdata = data["transit"] tdata = data["transit"]
print("calling tr.set_transit_key()")
transit_receiver.set_transit_key(tdata["key"]) transit_receiver.set_transit_key(tdata["key"])
transit_receiver.add_sender_direct_hints(tdata["direct_connection_hints"]) transit_receiver.add_sender_direct_hints(tdata["direct_connection_hints"])
transit_receiver.add_sender_relay_hints(tdata["relay_connection_hints"]) transit_receiver.add_sender_relay_hints(tdata["relay_connection_hints"])
skt = transit_receiver.establish_connection() skt = transit_receiver.establish_connection()
print("Receiving %d bytes.." % filesize)
encrypted = skt.recv(encrypted_filesize) encrypted = skt.recv(encrypted_filesize)
if len(encrypted) != encrypted_filesize: if len(encrypted) != encrypted_filesize:
print("Connection dropped before file received") print("Connection dropped before file received")
@ -49,4 +49,5 @@ assert os.path.dirname(target) == here
assert not os.path.exists(target) assert not os.path.exists(target)
with open(target, "wb") as f: with open(target, "wb") as f:
f.write(decrypted) f.write(decrypted)
print("%s written" % filename) print("Received file written to %s" % filename)
skt.send("ok\n")

View File

@ -16,11 +16,12 @@ transit_key = transit_sender.get_transit_key()
direct_hints = transit_sender.get_direct_hints() direct_hints = transit_sender.get_direct_hints()
relay_hints = transit_sender.get_relay_hints() relay_hints = transit_sender.get_relay_hints()
filesize = os.stat(filename).st_size
data = json.dumps({ data = json.dumps({
"file": { "file": {
"key": hexlify(xfer_key), "key": hexlify(xfer_key),
"filename": os.path.basename(filename), "filename": os.path.basename(filename),
"filesize": os.stat(filename).st_size, "filesize": filesize,
}, },
"transit": { "transit": {
"key": hexlify(transit_key), "key": hexlify(transit_key),
@ -36,7 +37,7 @@ print("Wormhole code is '%s'" % code)
print("") 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,))
box = SecretBox(xfer_key) box = SecretBox(xfer_key)
with open(filename, "rb") as f: with open(filename, "rb") as f:
@ -47,7 +48,14 @@ encrypted = box.encrypt(plaintext, nonce)
tdata = them_d["transit"] tdata = them_d["transit"]
transit_sender.add_receiver_hints(tdata["direct_connection_hints"]) transit_sender.add_receiver_hints(tdata["direct_connection_hints"])
skt = transit_sender.establish_connection() skt = transit_sender.establish_connection()
skt.send(encrypted)
skt.close()
print("file sent") print("Sending %d bytes.." % filesize)
skt.send(encrypted)
print("File sent.. waiting for confirmation")
ack = skt.recv(3)
if ack == "ok\n":
print("Confirmation received. Transfer complete.")
else:
print("Transfer failed (remote says: '%r')" % ack)
skt.close()

View File

@ -1,3 +1,4 @@
from __future__ import print_function
import os, threading, socket, SocketServer import os, threading, socket, SocketServer
from binascii import hexlify from binascii import hexlify
from ..util import ipaddrs from ..util import ipaddrs
@ -121,7 +122,7 @@ def connector(owner, hint, send_handshake, expected_handshake):
addr,port = hint.split(",") addr,port = hint.split(",")
skt = socket.create_connection((addr,port)) # timeout here skt = socket.create_connection((addr,port)) # timeout here
skt.settimeout(TIMEOUT) skt.settimeout(TIMEOUT)
print "socket(%s) connected" % hint #print("socket(%s) connected" % (hint,))
try: try:
skt.send(send_handshake) skt.send(send_handshake)
got = b"" got = b""
@ -130,7 +131,7 @@ def connector(owner, hint, send_handshake, expected_handshake):
if expected_handshake[:len(got)] != got: if expected_handshake[:len(got)] != got:
raise BadHandshake("got '%r' want '%r' on %s" % raise BadHandshake("got '%r' want '%r' on %s" %
(got, expected_handshake, hint)) (got, expected_handshake, hint))
print "connector ready", hint #print("connector ready %r" % (hint,))
except: except:
try: try:
skt.shutdown(socket.SHUT_WR) skt.shutdown(socket.SHUT_WR)
@ -145,7 +146,7 @@ def connector(owner, hint, send_handshake, expected_handshake):
def handle(skt, client_address, owner, send_handshake, expected_handshake): def handle(skt, client_address, owner, send_handshake, expected_handshake):
try: try:
print "handle", skt #print("handle %r" % (skt,))
skt.settimeout(TIMEOUT) skt.settimeout(TIMEOUT)
skt.send(send_handshake) skt.send(send_handshake)
got = b"" got = b""
@ -158,9 +159,9 @@ def handle(skt, client_address, owner, send_handshake, expected_handshake):
if expected_handshake[:len(got)] != got: if expected_handshake[:len(got)] != got:
raise BadHandshake("got '%r' want '%r'" % raise BadHandshake("got '%r' want '%r'" %
(got, expected_handshake)) (got, expected_handshake))
print "handler negotiation finished", client_address #print("handler negotiation finished %r" % (client_address,))
except Exception as e: except Exception as e:
print "handler failed", client_address #print("handler failed %r" % (client_address,))
try: try:
# this raises socket.err(EBADF) if the socket was already closed # this raises socket.err(EBADF) if the socket was already closed
skt.shutdown(socket.SHUT_WR) skt.shutdown(socket.SHUT_WR)
@ -260,5 +261,5 @@ class TransitReceiver:
self.winning.set() self.winning.set()
else: else:
winner.close() winner.close()
raise BadHandshake("weird, receiver was given duplicate winner") print("weird, receiver was given duplicate winner")