diff --git a/docs/api.md b/docs/api.md index 3548c7c..f1421a5 100644 --- a/docs/api.md +++ b/docs/api.md @@ -185,6 +185,7 @@ to the application source code or default config. This library includes the URL of a public relay run by the author. Application developers can use this one, or they can run their own (see src/wormhole/servers/relay.py) and configure their clients to use it instead. +This URL is passed as a unicode string. ## Polling and Shutdown @@ -253,9 +254,13 @@ Some human-readable parameters are passed as strings: "str" in python2, "str" (i.e. unicode) in python3: * wormhole code -* relay/transit URLs +* transit URLs * transit connection hints (e.g. "host:port") +And some are always unicode, in both python2 and python3: + +* relay URL + ## Detailed Example ```python diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index b2e9bc4..e84ca10 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -27,7 +27,7 @@ MINUTE = 60*SECOND class Channel: def __init__(self, relay_url, channelid, side, handle_welcome): - self._channel_url = "%s%d" % (relay_url, channelid) + self._channel_url = u"%s%d" % (relay_url, channelid) self._side = side self._handle_welcome = handle_welcome self._messages = set() # (phase,body) , body is bytes @@ -131,9 +131,10 @@ class Wormhole: def __init__(self, appid, relay_url): if not isinstance(appid, type(b"")): raise UsageError + if not isinstance(relay_url, type(u"")): raise UsageError + if not relay_url.endswith(u"/"): raise UsageError self._appid = appid self._relay_url = relay_url - if not self._relay_url.endswith("/"): raise UsageError side = hexlify(os.urandom(5)).decode("ascii") self._channel_manager = ChannelManager(relay_url, side, self.handle_welcome) diff --git a/src/wormhole/public_relay.py b/src/wormhole/public_relay.py index 1f7a617..f486662 100644 --- a/src/wormhole/public_relay.py +++ b/src/wormhole/public_relay.py @@ -1,5 +1,5 @@ # This is a relay I run on a personal server. If it gets too expensive to # run, I'll shut it down. -RENDEZVOUS_RELAY = "http://wormhole-relay.petmail.org:3000/wormhole-relay/" +RENDEZVOUS_RELAY = u"http://wormhole-relay.petmail.org:3000/wormhole-relay/" TRANSIT_RELAY = "tcp:wormhole-transit-relay.petmail.org:3001" diff --git a/src/wormhole/scripts/cmd_receive.py b/src/wormhole/scripts/cmd_receive.py index 3cb3dd5..c62ea4e 100644 --- a/src/wormhole/scripts/cmd_receive.py +++ b/src/wormhole/scripts/cmd_receive.py @@ -10,6 +10,7 @@ def receive(args): from ..blocking.transcribe import Wormhole, WrongPasswordError from ..blocking.transit import TransitReceiver, TransitError from .progress import start_progress, update_progress, finish_progress + assert isinstance(args.relay_url, type(u"")) w = Wormhole(APPID, args.relay_url) if args.zeromode: diff --git a/src/wormhole/scripts/cmd_send.py b/src/wormhole/scripts/cmd_send.py index a35aa9e..35def33 100644 --- a/src/wormhole/scripts/cmd_send.py +++ b/src/wormhole/scripts/cmd_send.py @@ -10,6 +10,7 @@ def send(args): from ..blocking.transcribe import Wormhole, WrongPasswordError from ..blocking.transit import TransitSender from .progress import start_progress, update_progress, finish_progress + assert isinstance(args.relay_url, type(u"")) text = args.text if not text and not args.what: diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index 6619248..620c7c8 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -18,7 +18,7 @@ parser.add_argument("--version", action="version", version="magic-wormhole "+ __version__) g = parser.add_argument_group("wormhole configuration options") g.add_argument("--relay-url", default=public_relay.RENDEZVOUS_RELAY, - metavar="URL", help="rendezvous relay to use") + metavar="URL", help="rendezvous relay to use", type=type(u"")) g.add_argument("--transit-helper", default=public_relay.TRANSIT_RELAY, metavar="tcp:HOST:PORT", help="transit relay to use") g.add_argument("-c", "--code-length", type=int, default=2, diff --git a/src/wormhole/test/common.py b/src/wormhole/test/common.py index ef5a924..068249f 100644 --- a/src/wormhole/test/common.py +++ b/src/wormhole/test/common.py @@ -14,7 +14,7 @@ class ServerBase: "tcp:%s:interface=127.0.0.1" % transitport, __version__) s.setServiceParent(self.sp) - self.relayurl = "http://127.0.0.1:%d/wormhole-relay/" % relayport + self.relayurl = u"http://127.0.0.1:%d/wormhole-relay/" % relayport self.transit = "tcp:127.0.0.1:%d" % transitport d.addCallback(_got_ports) return d diff --git a/src/wormhole/test/test_server.py b/src/wormhole/test/test_server.py index d84eff1..32a2f2c 100644 --- a/src/wormhole/test/test_server.py +++ b/src/wormhole/test/test_server.py @@ -212,7 +212,7 @@ class API(ServerBase, unittest.TestCase): d = self.post("allocate", {"side": "abc"}) def _allocated(data): self.cid = data["channelid"] - url = (self.relayurl+str(self.cid)).encode("utf-8") + url = self.relayurl+str(self.cid) self.o = OneEventAtATime(url, parser=json.loads) return self.o.wait_for_connection() d.addCallback(_allocated) diff --git a/src/wormhole/twisted/eventsource_twisted.py b/src/wormhole/twisted/eventsource_twisted.py index 2d508a3..b2f37fc 100644 --- a/src/wormhole/twisted/eventsource_twisted.py +++ b/src/wormhole/twisted/eventsource_twisted.py @@ -80,6 +80,7 @@ class EventSourceError(Exception): class EventSource: # TODO: service.Service def __init__(self, url, handler, when_connected=None, agent=None): + assert isinstance(url, type(u"")) self.url = url self.handler = handler self.when_connected = when_connected @@ -94,7 +95,7 @@ class EventSource: # TODO: service.Service assert not self.started, "single-use" self.started = True assert self.url - d = self.agent.request("GET", self.url, + d = self.agent.request("GET", self.url.encode("utf-8"), Headers({"accept": ["text/event-stream"]})) d.addCallback(self._connected) return d diff --git a/src/wormhole/twisted/transcribe.py b/src/wormhole/twisted/transcribe.py index b00a606..66db95a 100644 --- a/src/wormhole/twisted/transcribe.py +++ b/src/wormhole/twisted/transcribe.py @@ -36,7 +36,8 @@ class DataProducer: def post_json(agent, url, request_body): # POST a JSON body to a URL, parsing the response as JSON data = json.dumps(request_body).encode("utf-8") - d = agent.request("POST", url, bodyProducer=DataProducer(data)) + d = agent.request("POST", url.encode("utf-8"), + bodyProducer=DataProducer(data)) def _check_error(resp): if resp.code != 200: raise web_error.Error(resp.code, resp.phrase) @@ -49,7 +50,7 @@ def post_json(agent, url, request_body): class Channel: def __init__(self, relay_url, channelid, side, handle_welcome, agent): - self._channel_url = "%s%d" % (relay_url, channelid) + self._channel_url = u"%s%d" % (relay_url, channelid) self._side = side self._handle_welcome = handle_welcome self._agent = agent @@ -117,6 +118,7 @@ class Channel: class ChannelManager: def __init__(self, relay_url, side, handle_welcome): + assert isinstance(relay_url, type(u"")) self._relay_url = relay_url self._side = side self._handle_welcome = handle_welcome @@ -145,6 +147,8 @@ class Wormhole: def __init__(self, appid, relay_url): if not isinstance(appid, type(b"")): raise UsageError + if not isinstance(relay_url, type(u"")): raise UsageError + if not relay_url.endswith(u"/"): raise UsageError self._appid = appid self._relay_url = relay_url self._set_side(hexlify(os.urandom(5)).decode("ascii")) @@ -238,7 +242,7 @@ class Wormhole: @classmethod def from_serialized(klass, data): d = json.loads(data) - self = klass(d["appid"].encode("ascii"), d["relay_url"].encode("ascii")) + self = klass(d["appid"].encode("ascii"), d["relay_url"]) self._set_side(d["side"].encode("ascii")) self._set_code_and_channelid(d["code"].encode("ascii")) self.sp = SPAKE2_Symmetric.from_serialized(json.dumps(d["spake2"]))