diff --git a/bin/receive_file.py b/bin/receive_file.py index 9a0e50f..2ee976f 100644 --- a/bin/receive_file.py +++ b/bin/receive_file.py @@ -20,7 +20,7 @@ r = Receiver(APPID, mydata) r.set_code(r.input_code("Enter receive-file wormhole code: ")) data = json.loads(r.get_data().decode("utf-8")) -print("their data: %r" % (data,)) +#print("their data: %r" % (data,)) file_data = data["file"] 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 tdata = data["transit"] -print("calling tr.set_transit_key()") 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"]) skt = transit_receiver.establish_connection() +print("Receiving %d bytes.." % filesize) encrypted = skt.recv(encrypted_filesize) if len(encrypted) != encrypted_filesize: print("Connection dropped before file received") @@ -49,4 +49,5 @@ assert os.path.dirname(target) == here assert not os.path.exists(target) with open(target, "wb") as f: f.write(decrypted) -print("%s written" % filename) +print("Received file written to %s" % filename) +skt.send("ok\n") diff --git a/bin/send_file.py b/bin/send_file.py index 4fdb430..11ce780 100644 --- a/bin/send_file.py +++ b/bin/send_file.py @@ -16,11 +16,12 @@ transit_key = transit_sender.get_transit_key() direct_hints = transit_sender.get_direct_hints() relay_hints = transit_sender.get_relay_hints() +filesize = os.stat(filename).st_size data = json.dumps({ "file": { "key": hexlify(xfer_key), "filename": os.path.basename(filename), - "filesize": os.stat(filename).st_size, + "filesize": filesize, }, "transit": { "key": hexlify(transit_key), @@ -36,7 +37,7 @@ print("Wormhole code is '%s'" % code) print("") them_bytes = i.get_data() them_d = json.loads(them_bytes.decode("utf-8")) -print("them: %r" % (them_d,)) +#print("them: %r" % (them_d,)) box = SecretBox(xfer_key) with open(filename, "rb") as f: @@ -47,7 +48,14 @@ encrypted = box.encrypt(plaintext, nonce) tdata = them_d["transit"] transit_sender.add_receiver_hints(tdata["direct_connection_hints"]) 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() diff --git a/src/wormhole/blocking/transit.py b/src/wormhole/blocking/transit.py index 3eb6d65..b636c58 100644 --- a/src/wormhole/blocking/transit.py +++ b/src/wormhole/blocking/transit.py @@ -1,3 +1,4 @@ +from __future__ import print_function import os, threading, socket, SocketServer from binascii import hexlify from ..util import ipaddrs @@ -121,7 +122,7 @@ def connector(owner, hint, send_handshake, expected_handshake): addr,port = hint.split(",") skt = socket.create_connection((addr,port)) # timeout here skt.settimeout(TIMEOUT) - print "socket(%s) connected" % hint + #print("socket(%s) connected" % (hint,)) try: skt.send(send_handshake) got = b"" @@ -130,7 +131,7 @@ def connector(owner, hint, send_handshake, expected_handshake): if expected_handshake[:len(got)] != got: raise BadHandshake("got '%r' want '%r' on %s" % (got, expected_handshake, hint)) - print "connector ready", hint + #print("connector ready %r" % (hint,)) except: try: 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): try: - print "handle", skt + #print("handle %r" % (skt,)) skt.settimeout(TIMEOUT) skt.send(send_handshake) got = b"" @@ -158,9 +159,9 @@ def handle(skt, client_address, owner, send_handshake, expected_handshake): if expected_handshake[:len(got)] != got: raise BadHandshake("got '%r' want '%r'" % (got, expected_handshake)) - print "handler negotiation finished", client_address + #print("handler negotiation finished %r" % (client_address,)) except Exception as e: - print "handler failed", client_address + #print("handler failed %r" % (client_address,)) try: # this raises socket.err(EBADF) if the socket was already closed skt.shutdown(socket.SHUT_WR) @@ -260,5 +261,5 @@ class TransitReceiver: self.winning.set() else: winner.close() - raise BadHandshake("weird, receiver was given duplicate winner") + print("weird, receiver was given duplicate winner")