diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index 5e358aa..bd3255d 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -5,9 +5,8 @@ from spake2 import SPAKE2_A, SPAKE2_B from nacl.secret import SecretBox from nacl.exceptions import CryptoError from nacl import utils -from .. import codes +from .. import codes, const from ..util.hkdf import HKDF -from ..const import RENDEZVOUS_RELAY SECOND = 1 MINUTE = 60*SECOND @@ -164,11 +163,11 @@ class Common: return HKDF(self.key, length, CTXinfo=purpose) class Initiator(Common): - def __init__(self, appid, data, relay=RENDEZVOUS_RELAY): + def __init__(self, appid, data, relay=None): self.appid = appid self.data = data - assert relay.endswith("/") - self.relay = relay + self.relay = relay or const.RENDEZVOUS_RELAY + assert self.relay.endswith("/") self.started = time.time() self.wait = 0.5*SECOND self.timeout = 3*MINUTE @@ -204,11 +203,11 @@ class Initiator(Common): class Receiver(Common): - def __init__(self, appid, data, relay=RENDEZVOUS_RELAY): + def __init__(self, appid, data, relay=None): self.appid = appid self.data = data - self.relay = relay - assert relay.endswith("/") + self.relay = relay or const.RENDEZVOUS_RELAY + assert self.relay.endswith("/") self.started = time.time() self.wait = 0.5*SECOND self.timeout = 3*MINUTE diff --git a/src/wormhole/blocking/transit.py b/src/wormhole/blocking/transit.py index c2ed3ae..fad7122 100644 --- a/src/wormhole/blocking/transit.py +++ b/src/wormhole/blocking/transit.py @@ -2,9 +2,9 @@ from __future__ import print_function import re, time, threading, socket, SocketServer from binascii import hexlify, unhexlify from nacl.secret import SecretBox +from .. import const from ..util import ipaddrs from ..util.hkdf import HKDF -from ..const import TRANSIT_RELAY class TransitError(Exception): pass @@ -270,7 +270,8 @@ class RecordPipe: self.skt.close() class Common: - def __init__(self): + def __init__(self, transit_relay=None): + self._transit_relay = transit_relay or const.TRANSIT_RELAY self.winning = threading.Event() self._negotiation_check_lock = threading.Lock() self._have_transit_key = threading.Condition() @@ -291,7 +292,7 @@ class Common: def get_direct_hints(self): return self.my_direct_hints def get_relay_hints(self): - return [TRANSIT_RELAY] + return [self._transit_relay] def add_their_direct_hints(self, hints): self._their_direct_hints = [force_ascii(h) for h in hints] diff --git a/src/wormhole/scripts/cmd_receive_file.py b/src/wormhole/scripts/cmd_receive_file.py index 97d0cc7..dcd677b 100644 --- a/src/wormhole/scripts/cmd_receive_file.py +++ b/src/wormhole/scripts/cmd_receive_file.py @@ -8,7 +8,7 @@ APPID = "lothar.com/wormhole/file-xfer" def receive_file(so): # we're receiving - transit_receiver = TransitReceiver() + transit_receiver = TransitReceiver(transit_relay=so.parent["transit-helper"]) mydata = json.dumps({ "transit": { @@ -16,7 +16,7 @@ def receive_file(so): "relay_connection_hints": transit_receiver.get_relay_hints(), }, }).encode("utf-8") - r = Receiver(APPID, mydata) + r = Receiver(APPID, mydata, so.parent["relay-url"]) code = so["code"] if not code: code = r.input_code("Enter receive-file wormhole code: ") diff --git a/src/wormhole/scripts/cmd_receive_text.py b/src/wormhole/scripts/cmd_receive_text.py index a1bb814..a8215d4 100644 --- a/src/wormhole/scripts/cmd_receive_text.py +++ b/src/wormhole/scripts/cmd_receive_text.py @@ -7,7 +7,7 @@ APPID = "lothar.com/wormhole/text-xfer" def receive_text(so): # we're receiving data = json.dumps({"message": "ok"}).encode("utf-8") - r = Receiver(APPID, data) + r = Receiver(APPID, data, so.parent["relay-url"]) code = so["code"] if not code: code = r.input_code("Enter receive-text wormhole code: ") diff --git a/src/wormhole/scripts/cmd_send_file.py b/src/wormhole/scripts/cmd_send_file.py index 3d2a2b7..12d8027 100644 --- a/src/wormhole/scripts/cmd_send_file.py +++ b/src/wormhole/scripts/cmd_send_file.py @@ -10,7 +10,7 @@ def send_file(so): # we're sending filename = so["filename"] assert os.path.isfile(filename) - transit_sender = TransitSender() + transit_sender = TransitSender(transit_relay=so.parent["transit-helper"]) filesize = os.stat(filename).st_size data = json.dumps({ @@ -24,7 +24,7 @@ def send_file(so): }, }).encode("utf-8") - i = Initiator(APPID, data) + i = Initiator(APPID, data, so.parent["relay-url"]) code = i.get_code() print("On the other computer, please run: wormhole receive-file") print("Wormhole code is '%s'" % code) diff --git a/src/wormhole/scripts/cmd_send_text.py b/src/wormhole/scripts/cmd_send_text.py index 714ab85..8a8b45c 100644 --- a/src/wormhole/scripts/cmd_send_text.py +++ b/src/wormhole/scripts/cmd_send_text.py @@ -9,7 +9,7 @@ def send_text(so): message = so["text"] data = json.dumps({"message": message, }).encode("utf-8") - i = Initiator(APPID, data) + i = Initiator(APPID, data, so.parent["relay-url"]) code = i.get_code() print("On the other computer, please run: wormhole receive-text") print("Wormhole code is: %s" % code) diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index 3b39cc7..ffa0bf3 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -1,5 +1,6 @@ import sys from twisted.python import usage +from .. import const class SendTextOptions(usage.Options): def parseArgs(self, text): @@ -26,6 +27,12 @@ class ReceiveFileOptions(usage.Options): class Options(usage.Options): synopsis = "\nUsage: wormhole " + optParameters = [ + ["relay-url", None, const.RENDEZVOUS_RELAY, + "rendezvous relay to use (URL)"], + ["transit-helper", None, const.TRANSIT_RELAY, + "transit relay to use (tcp:HOST:PORT)"], + ] subCommands = [("send-text", None, SendTextOptions, "Send a text message"), ("send-file", None, SendFileOptions, "Send a file"), ("receive-text", None, ReceiveTextOptions, "Receive a text message"),