rearrange twisted+blocking to look roughly the same
This commit is contained in:
parent
cebfa71563
commit
5951015f79
|
@ -94,37 +94,6 @@ class Wormhole:
|
|||
other_msgs = r.json()["messages"]
|
||||
return other_msgs
|
||||
|
||||
def _get_messages(self, old_msgs, verb, msgnum):
|
||||
# For now, server errors cause the client to fail. TODO: don't. This
|
||||
# will require changing the client to re-post messages when the
|
||||
# server comes back up.
|
||||
|
||||
# note: while this passes around msgs (plural), our callers really
|
||||
# only care about the first one. we use "WHICH" and "SIDE" so that we
|
||||
# only expect to see a single message (not our own, where "SIDE" is
|
||||
# our own, and not messages for earlier stages, where "WHICH" is
|
||||
# different)
|
||||
msgs = old_msgs
|
||||
while not msgs:
|
||||
remaining = self.started + self.timeout - time.time()
|
||||
if remaining < 0:
|
||||
raise Timeout
|
||||
#time.sleep(self.wait)
|
||||
f = EventSourceFollower(self._url(verb, msgnum), remaining)
|
||||
for (eventtype, data) in f.iter_events():
|
||||
if eventtype == "welcome":
|
||||
self.handle_welcome(json.loads(data))
|
||||
if eventtype == "message":
|
||||
msgs = [json.loads(data)["message"]]
|
||||
break
|
||||
f.close()
|
||||
return msgs
|
||||
|
||||
def _get_data(self, other_msgs):
|
||||
msgs = self._get_messages(other_msgs, "poll", "data")
|
||||
data = unhexlify(msgs[0].encode("ascii"))
|
||||
return data
|
||||
|
||||
def _allocate_channel(self):
|
||||
r = requests.post(self.relay + "allocate/%s" % self.side)
|
||||
r.raise_for_status()
|
||||
|
@ -134,11 +103,6 @@ class Wormhole:
|
|||
channel_id = data["channel-id"]
|
||||
return channel_id
|
||||
|
||||
def derive_key(self, purpose, length=SecretBox.KEY_SIZE):
|
||||
if not isinstance(purpose, type(b"")): raise UsageError
|
||||
return HKDF(self.key, length, CTXinfo=purpose)
|
||||
|
||||
|
||||
def get_code(self, code_length=2):
|
||||
if self.code is not None: raise UsageError
|
||||
self.side = hexlify(os.urandom(5))
|
||||
|
@ -181,6 +145,54 @@ class Wormhole:
|
|||
idB=self.appid+":SymmetricB")
|
||||
self.msg1 = self.sp.start()
|
||||
|
||||
def _get_messages(self, old_msgs, verb, msgnum):
|
||||
# For now, server errors cause the client to fail. TODO: don't. This
|
||||
# will require changing the client to re-post messages when the
|
||||
# server comes back up.
|
||||
|
||||
# note: while this passes around msgs (plural), our callers really
|
||||
# only care about the first one. we use "WHICH" and "SIDE" so that we
|
||||
# only expect to see a single message (not our own, where "SIDE" is
|
||||
# our own, and not messages for earlier stages, where "WHICH" is
|
||||
# different)
|
||||
msgs = old_msgs
|
||||
while not msgs:
|
||||
remaining = self.started + self.timeout - time.time()
|
||||
if remaining < 0:
|
||||
raise Timeout
|
||||
#time.sleep(self.wait)
|
||||
f = EventSourceFollower(self._url(verb, msgnum), remaining)
|
||||
for (eventtype, data) in f.iter_events():
|
||||
if eventtype == "welcome":
|
||||
self.handle_welcome(json.loads(data))
|
||||
if eventtype == "message":
|
||||
msgs = [json.loads(data)["message"]]
|
||||
break
|
||||
f.close()
|
||||
return msgs
|
||||
|
||||
def _get_data(self, other_msgs):
|
||||
msgs = self._get_messages(other_msgs, "poll", "data")
|
||||
data = unhexlify(msgs[0].encode("ascii"))
|
||||
return data
|
||||
|
||||
def derive_key(self, purpose, length=SecretBox.KEY_SIZE):
|
||||
if not isinstance(purpose, type(b"")): raise UsageError
|
||||
return HKDF(self.key, length, CTXinfo=purpose)
|
||||
|
||||
def _encrypt_data(self, key, data):
|
||||
if len(key) != SecretBox.KEY_SIZE: raise UsageError
|
||||
box = SecretBox(key)
|
||||
nonce = utils.random(SecretBox.NONCE_SIZE)
|
||||
return box.encrypt(data, nonce)
|
||||
|
||||
def _decrypt_data(self, key, encrypted):
|
||||
if len(key) != SecretBox.KEY_SIZE: raise UsageError
|
||||
box = SecretBox(key)
|
||||
data = box.decrypt(encrypted)
|
||||
return data
|
||||
|
||||
|
||||
def _get_key(self):
|
||||
if not self.key:
|
||||
post_data = {"message": hexlify(self.msg1).decode("ascii")}
|
||||
|
@ -222,18 +234,6 @@ class Wormhole:
|
|||
self._deallocate()
|
||||
return inbound_data
|
||||
|
||||
def _encrypt_data(self, key, data):
|
||||
if len(key) != SecretBox.KEY_SIZE: raise UsageError
|
||||
box = SecretBox(key)
|
||||
nonce = utils.random(SecretBox.NONCE_SIZE)
|
||||
return box.encrypt(data, nonce)
|
||||
|
||||
def _decrypt_data(self, key, encrypted):
|
||||
if len(key) != SecretBox.KEY_SIZE: raise UsageError
|
||||
box = SecretBox(key)
|
||||
data = box.decrypt(encrypted)
|
||||
return data
|
||||
|
||||
def _deallocate(self):
|
||||
r = requests.post(self._url("deallocate"))
|
||||
r.raise_for_status()
|
||||
|
|
|
@ -84,20 +84,6 @@ class SymmetricWormhole:
|
|||
if "error" in welcome:
|
||||
raise ServerError(welcome["error"], self.relay)
|
||||
|
||||
def get_code(self, code_length=2):
|
||||
if self.code is not None: raise UsageError
|
||||
if self._started_get_code: raise UsageError
|
||||
self._started_get_code = True
|
||||
self.side = hexlify(os.urandom(5))
|
||||
d = self._allocate_channel()
|
||||
def _got_channel_id(channel_id):
|
||||
code = codes.make_code(channel_id, code_length)
|
||||
self._set_code_and_channel_id(code)
|
||||
self._start()
|
||||
return code
|
||||
d.addCallback(_got_channel_id)
|
||||
return d
|
||||
|
||||
def _post_json(self, url, post_json=None):
|
||||
# POST to a URL, parsing the response as JSON. Optionally include a
|
||||
# JSON request body.
|
||||
|
@ -125,6 +111,20 @@ class SymmetricWormhole:
|
|||
d.addCallback(_got_channel)
|
||||
return d
|
||||
|
||||
def get_code(self, code_length=2):
|
||||
if self.code is not None: raise UsageError
|
||||
if self._started_get_code: raise UsageError
|
||||
self._started_get_code = True
|
||||
self.side = hexlify(os.urandom(5))
|
||||
d = self._allocate_channel()
|
||||
def _got_channel_id(channel_id):
|
||||
code = codes.make_code(channel_id, code_length)
|
||||
self._set_code_and_channel_id(code)
|
||||
self._start()
|
||||
return code
|
||||
d.addCallback(_got_channel_id)
|
||||
return d
|
||||
|
||||
def set_code(self, code):
|
||||
if self.code is not None: raise UsageError
|
||||
if self.side is not None: raise UsageError
|
||||
|
|
Loading…
Reference in New Issue
Block a user