2016-06-04 06:07:50 +00:00
|
|
|
from __future__ import print_function, absolute_import, unicode_literals
|
2017-02-24 01:29:56 +00:00
|
|
|
import os, sys
|
2017-02-23 23:57:24 +00:00
|
|
|
from attr import attrs, attrib
|
2017-02-24 01:29:56 +00:00
|
|
|
from zope.interface import implementer
|
|
|
|
from twisted.internet import defer
|
|
|
|
from ._interfaces import IWormhole
|
|
|
|
from .util import bytes_to_hexstr
|
2017-02-23 01:02:01 +00:00
|
|
|
from .timing import DebugTiming
|
2017-02-22 20:51:53 +00:00
|
|
|
from .journal import ImmediateJournal
|
2017-02-25 02:30:00 +00:00
|
|
|
from ._boss import Boss, WormholeError
|
2016-05-21 01:49:20 +00:00
|
|
|
|
2017-02-23 23:57:24 +00:00
|
|
|
# We can provide different APIs to different apps:
|
|
|
|
# * Deferreds
|
|
|
|
# w.when_got_code().addCallback(print_code)
|
|
|
|
# w.send(data)
|
|
|
|
# w.receive().addCallback(got_data)
|
|
|
|
# w.close().addCallback(closed)
|
|
|
|
|
|
|
|
# * delegate callbacks (better for journaled environments)
|
|
|
|
# w = wormhole(delegate=app)
|
|
|
|
# w.send(data)
|
|
|
|
# app.wormhole_got_code(code)
|
2017-02-24 01:29:56 +00:00
|
|
|
# app.wormhole_got_verifier(verifier)
|
2017-02-23 23:57:24 +00:00
|
|
|
# app.wormhole_receive(data)
|
|
|
|
# w.close()
|
|
|
|
# app.wormhole_closed()
|
|
|
|
#
|
|
|
|
# * potential delegate options
|
|
|
|
# wormhole(delegate=app, delegate_prefix="wormhole_",
|
|
|
|
# delegate_args=(args, kwargs))
|
|
|
|
|
2017-03-02 03:55:13 +00:00
|
|
|
def _log(client_name, machine_name, old_state, input, new_state):
|
|
|
|
print("%s.%s[%s].%s -> [%s]" % (client_name, machine_name,
|
|
|
|
old_state, input, new_state))
|
|
|
|
|
2017-02-23 23:57:24 +00:00
|
|
|
@attrs
|
2017-02-24 01:29:56 +00:00
|
|
|
@implementer(IWormhole)
|
2017-02-23 23:57:24 +00:00
|
|
|
class _DelegatedWormhole(object):
|
|
|
|
_delegate = attrib()
|
|
|
|
|
|
|
|
def _set_boss(self, boss):
|
|
|
|
self._boss = boss
|
|
|
|
|
|
|
|
# from above
|
2017-02-24 02:23:55 +00:00
|
|
|
|
|
|
|
def allocate_code(self, code_length=2):
|
|
|
|
self._boss.allocate_code(code_length)
|
|
|
|
def input_code(self, stdio):
|
|
|
|
self._boss.input_code(stdio)
|
|
|
|
def set_code(self, code):
|
|
|
|
self._boss.set_code(code)
|
|
|
|
|
2017-02-24 01:29:56 +00:00
|
|
|
def send(self, plaintext):
|
|
|
|
self._boss.send(plaintext)
|
2017-02-23 23:57:24 +00:00
|
|
|
def close(self):
|
|
|
|
self._boss.close()
|
|
|
|
|
2017-03-02 03:55:13 +00:00
|
|
|
def debug_set_trace(self, client_name, which="B N M S O K R RC NL C T",
|
|
|
|
logger=_log):
|
|
|
|
self._boss.set_trace(client_name, which, logger)
|
|
|
|
|
2017-02-23 23:57:24 +00:00
|
|
|
# from below
|
|
|
|
def got_code(self, code):
|
|
|
|
self._delegate.wormhole_got_code(code)
|
|
|
|
def got_verifier(self, verifier):
|
|
|
|
self._delegate.wormhole_got_verifier(verifier)
|
2017-02-24 02:23:55 +00:00
|
|
|
def received(self, plaintext):
|
|
|
|
self._delegate.wormhole_received(plaintext)
|
2017-02-23 23:57:24 +00:00
|
|
|
def closed(self, result):
|
|
|
|
self._delegate.wormhole_closed(result)
|
|
|
|
|
2017-02-25 02:30:00 +00:00
|
|
|
class WormholeClosed(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-02-24 01:29:56 +00:00
|
|
|
@implementer(IWormhole)
|
2017-02-23 23:57:24 +00:00
|
|
|
class _DeferredWormhole(object):
|
2017-02-23 02:21:47 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._code = None
|
|
|
|
self._code_observers = []
|
|
|
|
self._verifier = None
|
|
|
|
self._verifier_observers = []
|
2017-02-24 02:23:55 +00:00
|
|
|
self._received_data = []
|
|
|
|
self._received_observers = []
|
2017-02-25 02:30:00 +00:00
|
|
|
self._closed_observers = []
|
2017-02-23 02:21:47 +00:00
|
|
|
|
|
|
|
def _set_boss(self, boss):
|
|
|
|
self._boss = boss
|
|
|
|
|
|
|
|
# from above
|
|
|
|
def when_code(self):
|
|
|
|
if self._code:
|
|
|
|
return defer.succeed(self._code)
|
|
|
|
d = defer.Deferred()
|
|
|
|
self._code_observers.append(d)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def when_verifier(self):
|
|
|
|
if self._verifier:
|
|
|
|
return defer.succeed(self._verifier)
|
|
|
|
d = defer.Deferred()
|
|
|
|
self._verifier_observers.append(d)
|
|
|
|
return d
|
|
|
|
|
2017-02-24 02:23:55 +00:00
|
|
|
def when_received(self):
|
|
|
|
if self._received_data:
|
|
|
|
return defer.succeed(self._received_data.pop(0))
|
|
|
|
d = defer.Deferred()
|
|
|
|
self._received_observers.append(d)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def allocate_code(self, code_length=2):
|
|
|
|
self._boss.allocate_code(code_length)
|
|
|
|
def input_code(self, stdio):
|
|
|
|
self._boss.input_code(stdio)
|
|
|
|
def set_code(self, code):
|
|
|
|
self._boss.set_code(code)
|
|
|
|
|
2017-02-24 01:29:56 +00:00
|
|
|
def send(self, plaintext):
|
|
|
|
self._boss.send(plaintext)
|
2017-02-23 02:21:47 +00:00
|
|
|
def close(self):
|
|
|
|
self._boss.close()
|
2017-02-25 02:30:00 +00:00
|
|
|
d = defer.Deferred()
|
|
|
|
self._closed_observers.append(d)
|
|
|
|
return d
|
2017-02-23 02:21:47 +00:00
|
|
|
|
2017-03-02 03:55:13 +00:00
|
|
|
def debug_set_trace(self, client_name, which="B N M S O K R RC NL C T",
|
|
|
|
logger=_log):
|
|
|
|
self._boss._set_trace(client_name, which, logger)
|
|
|
|
|
2017-02-23 02:21:47 +00:00
|
|
|
# from below
|
|
|
|
def got_code(self, code):
|
|
|
|
self._code = code
|
|
|
|
for d in self._code_observers:
|
|
|
|
d.callback(code)
|
|
|
|
self._code_observers[:] = []
|
|
|
|
def got_verifier(self, verifier):
|
|
|
|
self._verifier = verifier
|
|
|
|
for d in self._verifier_observers:
|
|
|
|
d.callback(verifier)
|
|
|
|
self._verifier_observers[:] = []
|
|
|
|
|
2017-02-24 02:23:55 +00:00
|
|
|
def received(self, plaintext):
|
|
|
|
if self._received_observers:
|
|
|
|
self._received_observers.pop(0).callback(plaintext)
|
|
|
|
return
|
|
|
|
self._received_data.append(plaintext)
|
2017-02-23 02:21:47 +00:00
|
|
|
|
|
|
|
def closed(self, result):
|
2017-02-25 02:30:00 +00:00
|
|
|
print("closed", result, type(result))
|
|
|
|
if isinstance(result, WormholeError):
|
|
|
|
e = result
|
|
|
|
else:
|
|
|
|
e = WormholeClosed(result)
|
|
|
|
for d in self._verifier_observers:
|
|
|
|
d.errback(e)
|
|
|
|
for d in self._received_observers:
|
|
|
|
d.errback(e)
|
|
|
|
for d in self._closed_observers:
|
|
|
|
d.callback(result)
|
2017-02-23 02:21:47 +00:00
|
|
|
|
2017-02-23 23:57:24 +00:00
|
|
|
def _wormhole(appid, relay_url, reactor, delegate=None,
|
|
|
|
tor_manager=None, timing=None,
|
|
|
|
journal=None,
|
|
|
|
stderr=sys.stderr,
|
|
|
|
):
|
2016-05-23 01:40:44 +00:00
|
|
|
timing = timing or DebugTiming()
|
2017-02-23 02:21:47 +00:00
|
|
|
side = bytes_to_hexstr(os.urandom(5))
|
|
|
|
journal = journal or ImmediateJournal()
|
2017-02-23 23:57:24 +00:00
|
|
|
if delegate:
|
|
|
|
w = _DelegatedWormhole(delegate)
|
|
|
|
else:
|
|
|
|
w = _DeferredWormhole()
|
2017-02-23 02:21:47 +00:00
|
|
|
b = Boss(w, side, relay_url, appid, reactor, journal, timing)
|
|
|
|
w._set_boss(b)
|
|
|
|
# force allocate for now
|
|
|
|
b.start()
|
2016-05-21 01:49:20 +00:00
|
|
|
return w
|
|
|
|
|
2017-02-23 23:57:24 +00:00
|
|
|
def delegated_wormhole(appid, relay_url, reactor, delegate,
|
|
|
|
tor_manager=None, timing=None,
|
|
|
|
journal=None,
|
|
|
|
stderr=sys.stderr,
|
|
|
|
):
|
|
|
|
assert delegate
|
|
|
|
return _wormhole(appid, relay_url, reactor, delegate,
|
|
|
|
tor_manager, timing, journal, stderr)
|
|
|
|
|
|
|
|
def deferred_wormhole(appid, relay_url, reactor,
|
|
|
|
tor_manager=None, timing=None,
|
|
|
|
journal=None,
|
|
|
|
stderr=sys.stderr,
|
|
|
|
):
|
2017-02-24 01:29:56 +00:00
|
|
|
return _wormhole(appid, relay_url, reactor, None,
|
2017-02-23 23:57:24 +00:00
|
|
|
tor_manager, timing, journal, stderr)
|