make transit mostly work, but the race condition kills it

This commit is contained in:
Brian Warner 2015-02-18 17:23:09 -08:00
parent f459d59b48
commit ae68dad441
4 changed files with 15 additions and 12 deletions

View File

@ -1,9 +1,8 @@
from __future__ import print_function from __future__ import print_function
import os, sys, json import os, json
from binascii import unhexlify from binascii import unhexlify
from nacl.secret import SecretBox from nacl.secret import SecretBox
from wormhole.blocking.transcribe import Receiver from wormhole.blocking.transcribe import Receiver
from wormhole.codes import input_code_with_completion
from wormhole.blocking.transit import TransitReceiver from wormhole.blocking.transit import TransitReceiver
APPID = "lothar.com/wormhole/file-xfer" APPID = "lothar.com/wormhole/file-xfer"
@ -18,7 +17,7 @@ mydata = json.dumps({
}, },
}).encode("utf-8") }).encode("utf-8")
r = Receiver(APPID, mydata) r = Receiver(APPID, mydata)
r.set_code(r.input_code("Enter receive-text 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,))
@ -30,11 +29,12 @@ filesize = file_data["filesize"]
# 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"])
transit_receiver.establish_connection() skt = transit_receiver.establish_connection()
encrypted = transit_receiver.receive() encrypted = skt.recv(filesize)
decrypted = SecretBox(xfer_key).decrypt(encrypted) decrypted = SecretBox(xfer_key).decrypt(encrypted)

View File

@ -1,7 +1,6 @@
from __future__ import print_function from __future__ import print_function
import time, json import time, json
from wormhole.blocking.transcribe import Receiver from wormhole.blocking.transcribe import Receiver
from wormhole.codes import input_code_with_completion
APPID = "lothar.com/wormhole/text-xfer" APPID = "lothar.com/wormhole/text-xfer"

View File

@ -41,12 +41,13 @@ 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:
plaintext = f.read() plaintext = f.read()
nonce = utils.random(SecretBox.NONCE_SIZE) nonce = os.urandom(SecretBox.NONCE_SIZE)
encrypted = box.encrypt(plaintext, nonce) encrypted = box.encrypt(plaintext, nonce)
transit_sender.add_receiver_hints(them_d["direct_connection_hints"]) tdata = them_d["transit"]
transit_sender.establish_connection() transit_sender.add_receiver_hints(tdata["direct_connection_hints"])
transit_sender.write(encrypted) skt = transit_sender.establish_connection()
transit_sender.close() skt.write(encrypted)
skt.close()
print("file sent") print("file sent")

View File

@ -116,6 +116,8 @@ class BadHandshake(Exception):
pass pass
def connector(owner, hint, send_handshake, expected_handshake): def connector(owner, hint, send_handshake, expected_handshake):
if isinstance(hint, type(u"")):
hint = hint.encode("ascii")
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)
@ -168,7 +170,7 @@ class MyTCPServer(SocketServer.TCPServer):
allow_reuse_address = True allow_reuse_address = True
def process_request(self, request, client_address): def process_request(self, request, client_address):
if not self.owner.key: if not self.owner.key:
raise BadHandshake("connection received before set_key()") raise BadHandshake("connection received before set_transit_key()")
t = threading.Thread(target=handle, t = threading.Thread(target=handle,
args=(request, client_address, args=(request, client_address,
self.owner, self.owner,
@ -181,6 +183,7 @@ class TransitReceiver:
def __init__(self): def __init__(self):
self.winning = threading.Event() self.winning = threading.Event()
self._negotiation_check_lock = threading.Lock() self._negotiation_check_lock = threading.Lock()
self.key = None
server = MyTCPServer(("",9999), None) server = MyTCPServer(("",9999), None)
_, port = server.server_address _, port = server.server_address
self.my_direct_hints = ["%s,%d" % (addr, port) self.my_direct_hints = ["%s,%d" % (addr, port)