rename some methods to make them more private
This commit is contained in:
parent
effbd27047
commit
e5fcc6a8c8
|
@ -59,7 +59,7 @@ class Wormhole:
|
||||||
self.key = None
|
self.key = None
|
||||||
self.verifier = None
|
self.verifier = None
|
||||||
|
|
||||||
def url(self, verb, msgnum=None):
|
def _url(self, verb, msgnum=None):
|
||||||
url = "%s%d/%s/%s" % (self.relay, self.channel_id, self.side, verb)
|
url = "%s%d/%s/%s" % (self.relay, self.channel_id, self.side, verb)
|
||||||
if msgnum is not None:
|
if msgnum is not None:
|
||||||
url += "/" + msgnum
|
url += "/" + msgnum
|
||||||
|
@ -87,7 +87,7 @@ class Wormhole:
|
||||||
if "error" in welcome:
|
if "error" in welcome:
|
||||||
raise ServerError(welcome["error"], self.relay)
|
raise ServerError(welcome["error"], self.relay)
|
||||||
|
|
||||||
def get(self, old_msgs, verb, msgnum):
|
def _get_messages(self, old_msgs, verb, msgnum):
|
||||||
# For now, server errors cause the client to fail. TODO: don't. This
|
# For now, server errors cause the client to fail. TODO: don't. This
|
||||||
# will require changing the client to re-post messages when the
|
# will require changing the client to re-post messages when the
|
||||||
# server comes back up.
|
# server comes back up.
|
||||||
|
@ -103,7 +103,7 @@ class Wormhole:
|
||||||
if remaining < 0:
|
if remaining < 0:
|
||||||
raise Timeout
|
raise Timeout
|
||||||
#time.sleep(self.wait)
|
#time.sleep(self.wait)
|
||||||
f = EventSourceFollower(self.url(verb, msgnum), remaining)
|
f = EventSourceFollower(self._url(verb, msgnum), remaining)
|
||||||
for (eventtype, data) in f.iter_events():
|
for (eventtype, data) in f.iter_events():
|
||||||
if eventtype == "welcome":
|
if eventtype == "welcome":
|
||||||
self.handle_welcome(json.loads(data))
|
self.handle_welcome(json.loads(data))
|
||||||
|
@ -172,11 +172,11 @@ class Wormhole:
|
||||||
def _get_key(self):
|
def _get_key(self):
|
||||||
if not self.key:
|
if not self.key:
|
||||||
post_data = {"message": hexlify(self.msg1).decode("ascii")}
|
post_data = {"message": hexlify(self.msg1).decode("ascii")}
|
||||||
r = requests.post(self.url("post", "pake"),
|
r = requests.post(self._url("post", "pake"),
|
||||||
data=json.dumps(post_data))
|
data=json.dumps(post_data))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
other_msgs = r.json()["messages"]
|
other_msgs = r.json()["messages"]
|
||||||
msgs = self.get(other_msgs, "poll", "pake")
|
msgs = self._get_messages(other_msgs, "poll", "pake")
|
||||||
pake_msg = unhexlify(msgs[0].encode("ascii"))
|
pake_msg = unhexlify(msgs[0].encode("ascii"))
|
||||||
self.key = self.sp.finish(pake_msg)
|
self.key = self.sp.finish(pake_msg)
|
||||||
self.verifier = self.derive_key(self.appid+b":Verifier")
|
self.verifier = self.derive_key(self.appid+b":Verifier")
|
||||||
|
@ -224,16 +224,16 @@ class Wormhole:
|
||||||
|
|
||||||
def _post_data(self, data):
|
def _post_data(self, data):
|
||||||
post_data = json.dumps({"message": hexlify(data).decode("ascii")})
|
post_data = json.dumps({"message": hexlify(data).decode("ascii")})
|
||||||
r = requests.post(self.url("post", "data"), data=post_data)
|
r = requests.post(self._url("post", "data"), data=post_data)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
other_msgs = r.json()["messages"]
|
other_msgs = r.json()["messages"]
|
||||||
return other_msgs
|
return other_msgs
|
||||||
|
|
||||||
def _get_data(self, other_msgs):
|
def _get_data(self, other_msgs):
|
||||||
msgs = self.get(other_msgs, "poll", "data")
|
msgs = self._get_messages(other_msgs, "poll", "data")
|
||||||
data = unhexlify(msgs[0].encode("ascii"))
|
data = unhexlify(msgs[0].encode("ascii"))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def _deallocate(self):
|
def _deallocate(self):
|
||||||
r = requests.post(self.url("deallocate"))
|
r = requests.post(self._url("deallocate"))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
|
@ -69,7 +69,7 @@ class SymmetricWormhole:
|
||||||
|
|
||||||
def _allocate_channel(self):
|
def _allocate_channel(self):
|
||||||
url = self.relay + "allocate/%s" % self.side
|
url = self.relay + "allocate/%s" % self.side
|
||||||
d = self.post(url)
|
d = self._post_json(url)
|
||||||
def _got_channel(data):
|
def _got_channel(data):
|
||||||
if "welcome" in data:
|
if "welcome" in data:
|
||||||
self.handle_welcome(data["welcome"])
|
self.handle_welcome(data["welcome"])
|
||||||
|
@ -149,13 +149,13 @@ class SymmetricWormhole:
|
||||||
if "error" in welcome:
|
if "error" in welcome:
|
||||||
raise ServerError(welcome["error"], self.relay)
|
raise ServerError(welcome["error"], self.relay)
|
||||||
|
|
||||||
def url(self, verb, msgnum=None):
|
def _url(self, verb, msgnum=None):
|
||||||
url = "%s%d/%s/%s" % (self.relay, self.channel_id, self.side, verb)
|
url = "%s%d/%s/%s" % (self.relay, self.channel_id, self.side, verb)
|
||||||
if msgnum is not None:
|
if msgnum is not None:
|
||||||
url += "/" + msgnum
|
url += "/" + msgnum
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def post(self, url, post_json=None):
|
def _post_json(self, url, post_json=None):
|
||||||
# TODO: retry on failure, with exponential backoff. We're guarding
|
# TODO: retry on failure, with exponential backoff. We're guarding
|
||||||
# against the rendezvous server being temporarily offline.
|
# against the rendezvous server being temporarily offline.
|
||||||
p = None
|
p = None
|
||||||
|
@ -172,7 +172,7 @@ class SymmetricWormhole:
|
||||||
d.addCallback(lambda data: json.loads(data))
|
d.addCallback(lambda data: json.loads(data))
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def _get_msgs(self, old_msgs, verb, msgnum):
|
def _get_messages(self, old_msgs, verb, msgnum):
|
||||||
# fire with a list of messages that match verb/msgnum, which either
|
# fire with a list of messages that match verb/msgnum, which either
|
||||||
# came from old_msgs, or from an EventSource that we attached to the
|
# came from old_msgs, or from an EventSource that we attached to the
|
||||||
# corresponding URL
|
# corresponding URL
|
||||||
|
@ -186,7 +186,7 @@ class SymmetricWormhole:
|
||||||
if name == "message":
|
if name == "message":
|
||||||
msgs.append(json.loads(data)["message"])
|
msgs.append(json.loads(data)["message"])
|
||||||
d.callback(None)
|
d.callback(None)
|
||||||
es = ReconnectingEventSource(None, lambda: self.url(verb, msgnum),
|
es = ReconnectingEventSource(None, lambda: self._url(verb, msgnum),
|
||||||
_handle)#, agent=self.agent)
|
_handle)#, agent=self.agent)
|
||||||
es.startService() # TODO: .setServiceParent(self)
|
es.startService() # TODO: .setServiceParent(self)
|
||||||
es.activate()
|
es.activate()
|
||||||
|
@ -220,8 +220,8 @@ class SymmetricWormhole:
|
||||||
if self.key:
|
if self.key:
|
||||||
return defer.succeed(self.key)
|
return defer.succeed(self.key)
|
||||||
data = {"message": hexlify(self.msg1).decode("ascii")}
|
data = {"message": hexlify(self.msg1).decode("ascii")}
|
||||||
d = self.post(self.url("post", "pake"), data)
|
d = self._post_json(self._url("post", "pake"), data)
|
||||||
d.addCallback(lambda j: self._get_msgs(j["messages"], "poll", "pake"))
|
d.addCallback(lambda j: self._get_messages(j["messages"], "poll", "pake"))
|
||||||
def _got_pake(msgs):
|
def _got_pake(msgs):
|
||||||
pake_msg = unhexlify(msgs[0].encode("ascii"))
|
pake_msg = unhexlify(msgs[0].encode("ascii"))
|
||||||
key = self.sp.finish(pake_msg)
|
key = self.sp.finish(pake_msg)
|
||||||
|
@ -251,8 +251,8 @@ class SymmetricWormhole:
|
||||||
data_key = self.derive_key(b"data-key")
|
data_key = self.derive_key(b"data-key")
|
||||||
outbound_encrypted = self._encrypt_data(data_key, outbound_data)
|
outbound_encrypted = self._encrypt_data(data_key, outbound_data)
|
||||||
data = {"message": hexlify(outbound_encrypted).decode("ascii")}
|
data = {"message": hexlify(outbound_encrypted).decode("ascii")}
|
||||||
d = self.post(self.url("post", "data"), data)
|
d = self._post_json(self._url("post", "data"), data)
|
||||||
d.addCallback(lambda j: self._get_msgs(j["messages"], "poll", "data"))
|
d.addCallback(lambda j: self._get_messages(j["messages"], "poll", "data"))
|
||||||
def _got_data(msgs):
|
def _got_data(msgs):
|
||||||
inbound_encrypted = unhexlify(msgs[0].encode("ascii"))
|
inbound_encrypted = unhexlify(msgs[0].encode("ascii"))
|
||||||
if inbound_encrypted == outbound_encrypted:
|
if inbound_encrypted == outbound_encrypted:
|
||||||
|
@ -268,6 +268,6 @@ class SymmetricWormhole:
|
||||||
|
|
||||||
def _deallocate(self, res):
|
def _deallocate(self, res):
|
||||||
# only try once, no retries
|
# only try once, no retries
|
||||||
d = self.agent.request("POST", self.url("deallocate"))
|
d = self.agent.request("POST", self._url("deallocate"))
|
||||||
d.addBoth(lambda _: res) # ignore POST failure, pass-through result
|
d.addBoth(lambda _: res) # ignore POST failure, pass-through result
|
||||||
return d
|
return d
|
||||||
|
|
Loading…
Reference in New Issue
Block a user