add CLI args to override the relay hosts (rendezvous and transit)

This commit is contained in:
Brian Warner 2015-03-20 17:45:03 -07:00
parent 84aa7ff248
commit cc37d2dc2f
7 changed files with 24 additions and 17 deletions

View File

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

View File

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

View File

@ -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: ")

View File

@ -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: ")

View File

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

View File

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

View File

@ -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 <command>"
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"),