more transit-relay work
This commit is contained in:
parent
cd54eff994
commit
dc8d6e979f
|
@ -45,6 +45,9 @@ def build_sender_handshake(key):
|
||||||
hexid = HKDF(key, 32, CTXinfo=b"transit_sender")
|
hexid = HKDF(key, 32, CTXinfo=b"transit_sender")
|
||||||
return "transit sender %s ready\n\n" % hexlify(hexid)
|
return "transit sender %s ready\n\n" % hexlify(hexid)
|
||||||
|
|
||||||
|
def build_relay_token(key):
|
||||||
|
return "PLEASE RELAY\n"
|
||||||
|
|
||||||
TIMEOUT=15
|
TIMEOUT=15
|
||||||
|
|
||||||
# 1: sender only transmits, receiver only accepts, both wait forever
|
# 1: sender only transmits, receiver only accepts, both wait forever
|
||||||
|
@ -62,7 +65,16 @@ def force_ascii(s):
|
||||||
return s.encode("ascii")
|
return s.encode("ascii")
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def connector(owner, hint, send_handshake, expected_handshake):
|
def wait_for(skt, expected, hint):
|
||||||
|
got = b""
|
||||||
|
while len(got) < len(expected):
|
||||||
|
got += skt.recv(1)
|
||||||
|
if expected[:len(got)] != got:
|
||||||
|
raise BadHandshake("got '%r' want '%r' on %s" %
|
||||||
|
(got, expected, hint))
|
||||||
|
|
||||||
|
def connector(owner, hint, send_handshake, expected_handshake,
|
||||||
|
relay_token=None):
|
||||||
addr,port = hint.split(",")
|
addr,port = hint.split(",")
|
||||||
skt = None
|
skt = None
|
||||||
try:
|
try:
|
||||||
|
@ -70,13 +82,11 @@ def connector(owner, hint, send_handshake, expected_handshake):
|
||||||
TIMEOUT) # timeout or ECONNREFUSED
|
TIMEOUT) # timeout or ECONNREFUSED
|
||||||
skt.settimeout(TIMEOUT)
|
skt.settimeout(TIMEOUT)
|
||||||
#print("socket(%s) connected" % (hint,))
|
#print("socket(%s) connected" % (hint,))
|
||||||
|
if relay_token:
|
||||||
|
skt.send(relay_token)
|
||||||
|
wait_for(skt, "ok\n", hint)
|
||||||
skt.send(send_handshake)
|
skt.send(send_handshake)
|
||||||
got = b""
|
wait_for(skt, expected_handshake, hint)
|
||||||
while len(got) < len(expected_handshake):
|
|
||||||
got += skt.recv(1)
|
|
||||||
if expected_handshake[:len(got)] != got:
|
|
||||||
raise BadHandshake("got '%r' want '%r' on %s" %
|
|
||||||
(got, expected_handshake, hint))
|
|
||||||
#print("connector ready %r" % (hint,))
|
#print("connector ready %r" % (hint,))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
try:
|
try:
|
||||||
|
@ -208,17 +218,17 @@ class Common:
|
||||||
if not self._their_direct_hints:
|
if not self._their_direct_hints:
|
||||||
self._start_relay_connectors()
|
self._start_relay_connectors()
|
||||||
|
|
||||||
def _start_connector(self, hint):
|
def _start_connector(self, hint, is_relay=False):
|
||||||
t = threading.Thread(target=connector,
|
args = (self, hint, self._send_this(), self._expect_this())
|
||||||
args=(self, hint,
|
if is_relay:
|
||||||
self._send_this(),
|
args = args + (build_relay_token(self._transit_key),)
|
||||||
self._expect_this()))
|
t = threading.Thread(target=connector, args=args)
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
def _start_relay_connectors(self):
|
def _start_relay_connectors(self):
|
||||||
for hint in self._their_relay_hints:
|
for hint in self._their_relay_hints:
|
||||||
self._start_connector(hint)
|
self._start_connector(hint, is_relay=True)
|
||||||
|
|
||||||
def establish_connection(self):
|
def establish_connection(self):
|
||||||
self.winning_skt = None
|
self.winning_skt = None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user