relay-url is now unicode

This commit is contained in:
Brian Warner 2015-10-06 16:52:33 -07:00
parent 13dd359f90
commit 9ba7de6e1e
10 changed files with 24 additions and 11 deletions

View File

@ -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. 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 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. src/wormhole/servers/relay.py) and configure their clients to use it instead.
This URL is passed as a unicode string.
## Polling and Shutdown ## Polling and Shutdown
@ -253,9 +254,13 @@ Some human-readable parameters are passed as strings: "str" in python2, "str"
(i.e. unicode) in python3: (i.e. unicode) in python3:
* wormhole code * wormhole code
* relay/transit URLs * transit URLs
* transit connection hints (e.g. "host:port") * transit connection hints (e.g. "host:port")
And some are always unicode, in both python2 and python3:
* relay URL
## Detailed Example ## Detailed Example
```python ```python

View File

@ -27,7 +27,7 @@ MINUTE = 60*SECOND
class Channel: class Channel:
def __init__(self, relay_url, channelid, side, handle_welcome): 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._side = side
self._handle_welcome = handle_welcome self._handle_welcome = handle_welcome
self._messages = set() # (phase,body) , body is bytes self._messages = set() # (phase,body) , body is bytes
@ -131,9 +131,10 @@ class Wormhole:
def __init__(self, appid, relay_url): def __init__(self, appid, relay_url):
if not isinstance(appid, type(b"")): raise UsageError 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._appid = appid
self._relay_url = relay_url self._relay_url = relay_url
if not self._relay_url.endswith("/"): raise UsageError
side = hexlify(os.urandom(5)).decode("ascii") side = hexlify(os.urandom(5)).decode("ascii")
self._channel_manager = ChannelManager(relay_url, side, self._channel_manager = ChannelManager(relay_url, side,
self.handle_welcome) self.handle_welcome)

View File

@ -1,5 +1,5 @@
# This is a relay I run on a personal server. If it gets too expensive to # This is a relay I run on a personal server. If it gets too expensive to
# run, I'll shut it down. # 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" TRANSIT_RELAY = "tcp:wormhole-transit-relay.petmail.org:3001"

View File

@ -10,6 +10,7 @@ def receive(args):
from ..blocking.transcribe import Wormhole, WrongPasswordError from ..blocking.transcribe import Wormhole, WrongPasswordError
from ..blocking.transit import TransitReceiver, TransitError from ..blocking.transit import TransitReceiver, TransitError
from .progress import start_progress, update_progress, finish_progress from .progress import start_progress, update_progress, finish_progress
assert isinstance(args.relay_url, type(u""))
w = Wormhole(APPID, args.relay_url) w = Wormhole(APPID, args.relay_url)
if args.zeromode: if args.zeromode:

View File

@ -10,6 +10,7 @@ def send(args):
from ..blocking.transcribe import Wormhole, WrongPasswordError from ..blocking.transcribe import Wormhole, WrongPasswordError
from ..blocking.transit import TransitSender from ..blocking.transit import TransitSender
from .progress import start_progress, update_progress, finish_progress from .progress import start_progress, update_progress, finish_progress
assert isinstance(args.relay_url, type(u""))
text = args.text text = args.text
if not text and not args.what: if not text and not args.what:

View File

@ -18,7 +18,7 @@ parser.add_argument("--version", action="version",
version="magic-wormhole "+ __version__) version="magic-wormhole "+ __version__)
g = parser.add_argument_group("wormhole configuration options") g = parser.add_argument_group("wormhole configuration options")
g.add_argument("--relay-url", default=public_relay.RENDEZVOUS_RELAY, 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, g.add_argument("--transit-helper", default=public_relay.TRANSIT_RELAY,
metavar="tcp:HOST:PORT", help="transit relay to use") metavar="tcp:HOST:PORT", help="transit relay to use")
g.add_argument("-c", "--code-length", type=int, default=2, g.add_argument("-c", "--code-length", type=int, default=2,

View File

@ -14,7 +14,7 @@ class ServerBase:
"tcp:%s:interface=127.0.0.1" % transitport, "tcp:%s:interface=127.0.0.1" % transitport,
__version__) __version__)
s.setServiceParent(self.sp) 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 self.transit = "tcp:127.0.0.1:%d" % transitport
d.addCallback(_got_ports) d.addCallback(_got_ports)
return d return d

View File

@ -212,7 +212,7 @@ class API(ServerBase, unittest.TestCase):
d = self.post("allocate", {"side": "abc"}) d = self.post("allocate", {"side": "abc"})
def _allocated(data): def _allocated(data):
self.cid = data["channelid"] 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) self.o = OneEventAtATime(url, parser=json.loads)
return self.o.wait_for_connection() return self.o.wait_for_connection()
d.addCallback(_allocated) d.addCallback(_allocated)

View File

@ -80,6 +80,7 @@ class EventSourceError(Exception):
class EventSource: # TODO: service.Service class EventSource: # TODO: service.Service
def __init__(self, url, handler, when_connected=None, agent=None): def __init__(self, url, handler, when_connected=None, agent=None):
assert isinstance(url, type(u""))
self.url = url self.url = url
self.handler = handler self.handler = handler
self.when_connected = when_connected self.when_connected = when_connected
@ -94,7 +95,7 @@ class EventSource: # TODO: service.Service
assert not self.started, "single-use" assert not self.started, "single-use"
self.started = True self.started = True
assert self.url 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"]})) Headers({"accept": ["text/event-stream"]}))
d.addCallback(self._connected) d.addCallback(self._connected)
return d return d

View File

@ -36,7 +36,8 @@ class DataProducer:
def post_json(agent, url, request_body): def post_json(agent, url, request_body):
# POST a JSON body to a URL, parsing the response as JSON # POST a JSON body to a URL, parsing the response as JSON
data = json.dumps(request_body).encode("utf-8") 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): def _check_error(resp):
if resp.code != 200: if resp.code != 200:
raise web_error.Error(resp.code, resp.phrase) raise web_error.Error(resp.code, resp.phrase)
@ -49,7 +50,7 @@ def post_json(agent, url, request_body):
class Channel: class Channel:
def __init__(self, relay_url, channelid, side, handle_welcome, def __init__(self, relay_url, channelid, side, handle_welcome,
agent): agent):
self._channel_url = "%s%d" % (relay_url, channelid) self._channel_url = u"%s%d" % (relay_url, channelid)
self._side = side self._side = side
self._handle_welcome = handle_welcome self._handle_welcome = handle_welcome
self._agent = agent self._agent = agent
@ -117,6 +118,7 @@ class Channel:
class ChannelManager: class ChannelManager:
def __init__(self, relay_url, side, handle_welcome): def __init__(self, relay_url, side, handle_welcome):
assert isinstance(relay_url, type(u""))
self._relay_url = relay_url self._relay_url = relay_url
self._side = side self._side = side
self._handle_welcome = handle_welcome self._handle_welcome = handle_welcome
@ -145,6 +147,8 @@ class Wormhole:
def __init__(self, appid, relay_url): def __init__(self, appid, relay_url):
if not isinstance(appid, type(b"")): raise UsageError 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._appid = appid
self._relay_url = relay_url self._relay_url = relay_url
self._set_side(hexlify(os.urandom(5)).decode("ascii")) self._set_side(hexlify(os.urandom(5)).decode("ascii"))
@ -238,7 +242,7 @@ class Wormhole:
@classmethod @classmethod
def from_serialized(klass, data): def from_serialized(klass, data):
d = json.loads(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_side(d["side"].encode("ascii"))
self._set_code_and_channelid(d["code"].encode("ascii")) self._set_code_and_channelid(d["code"].encode("ascii"))
self.sp = SPAKE2_Symmetric.from_serialized(json.dumps(d["spake2"])) self.sp = SPAKE2_Symmetric.from_serialized(json.dumps(d["spake2"]))