magic-wormhole/src/wormhole/servers/transit_server.py

195 lines
7.9 KiB
Python
Raw Normal View History

2015-10-04 19:40:12 +00:00
from __future__ import print_function
2015-12-04 03:45:34 +00:00
import re, time
from twisted.python import log
2015-10-04 19:40:12 +00:00
from twisted.internet import protocol
from twisted.application import service
SECONDS = 1.0
MINUTE = 60*SECONDS
HOUR = 60*MINUTE
DAY = 24*HOUR
MB = 1000*1000
class TransitConnection(protocol.Protocol):
def __init__(self):
self._got_token = False
self._token_buffer = b""
self._sent_ok = False
self._buddy = None
2015-12-04 03:45:34 +00:00
self._had_buddy = False
self._total_sent = 0
2015-10-04 19:40:12 +00:00
def describeToken(self):
if self._got_token:
return self._got_token[:16].decode("ascii")
return "-"
2015-12-04 03:45:34 +00:00
def connectionMade(self):
self._started = time.time()
2015-10-04 19:40:12 +00:00
def dataReceived(self, data):
if self._sent_ok:
# We are an IPushProducer to our buddy's IConsumer, so they'll
# throttle us (by calling pauseProducing()) when their outbound
# buffer is full (e.g. when their downstream pipe is full). In
# practice, this buffers about 10MB per connection, after which
# point the sender will only transmit data as fast as the
# receiver can handle it.
self._total_sent += len(data)
self._buddy.transport.write(data)
2015-10-04 19:40:12 +00:00
return
2015-12-04 03:45:34 +00:00
if self._got_token: # but not yet sent_ok
self.transport.write(b"impatient\n")
log.msg("transit impatience failure")
2015-10-04 19:40:12 +00:00
return self.disconnect() # impatience yields failure
2015-12-04 03:45:34 +00:00
2015-10-04 19:40:12 +00:00
# else this should be (part of) the token
self._token_buffer += data
buf = self._token_buffer
2015-10-04 19:40:12 +00:00
wanted = len("please relay \n")+32*2
if len(buf) < wanted-1 and "\n" in buf:
self.transport.write(b"bad handshake\n")
log.msg("transit handshake early failure")
2015-10-04 19:40:12 +00:00
return self.disconnect()
if len(buf) < wanted:
return
if len(buf) > wanted:
self.transport.write(b"impatient\n")
log.msg("transit impatience failure")
2015-10-04 19:40:12 +00:00
return self.disconnect() # impatience yields failure
mo = re.search(br"^please relay (\w{64})\n", buf, re.M)
2015-10-04 19:40:12 +00:00
if not mo:
self.transport.write(b"bad handshake\n")
log.msg("transit handshake failure")
2015-10-04 19:40:12 +00:00
return self.disconnect() # incorrectness yields failure
token = mo.group(1)
self._got_token = token
2015-10-04 19:40:12 +00:00
self.factory.connection_got_token(token, self)
def buddy_connected(self, them):
self._buddy = them
2015-12-04 03:45:34 +00:00
self._had_buddy = True
2015-10-04 19:40:12 +00:00
self.transport.write(b"ok\n")
self._sent_ok = True
# Connect the two as a producer/consumer pair. We use streaming=True,
# so this expects the IPushProducer interface, and uses
# pauseProducing() to throttle, and resumeProducing() to unthrottle.
self._buddy.transport.registerProducer(self.transport, True)
# The Transit object calls buddy_connected() on both protocols, so
# there will be two producer/consumer pairs.
2015-10-04 19:40:12 +00:00
def buddy_disconnected(self):
log.msg("buddy_disconnected %s" % self.describeToken())
self._buddy = None
2015-10-04 19:40:12 +00:00
self.transport.loseConnection()
def connectionLost(self, reason):
if self._buddy:
self._buddy.buddy_disconnected()
self.factory.transitFinished(self, self._got_token,
self.describeToken())
2015-12-04 03:45:34 +00:00
# Record usage. There are four cases:
# * 1: we connected, never had a buddy
# * 2: we connected first, we disconnect before the buddy
# * 3: we connected first, buddy disconnects first
# * 4: buddy connected first, we disconnect before buddy
# * 5: buddy connected first, buddy disconnects first
# whoever disconnects first gets to write the usage record (1,2,4)
finished = time.time()
if not self._had_buddy: # 1
total_time = finished - self._started
self.factory.recordUsage(self._started, u"lonely", 0,
total_time, None)
if self._had_buddy and self._buddy: # 2,4
total_bytes = self._total_sent + self._buddy._total_sent
starts = [self._started, self._buddy._started]
total_time = finished - min(starts)
waiting_time = max(starts) - min(starts)
self.factory.recordUsage(self._started, u"happy", total_bytes,
total_time, waiting_time)
2015-10-04 19:40:12 +00:00
def disconnect(self):
self.transport.loseConnection()
self.factory.transitFailed(self)
2015-12-04 03:45:34 +00:00
finished = time.time()
total_time = finished - self._started
self.factory.recordUsage(self._started, u"errory", 0,
total_time, None)
2015-10-04 19:40:12 +00:00
class Transit(protocol.ServerFactory, service.MultiService):
# I manage pairs of simultaneous connections to a secondary TCP port,
# both forwarded to the other. Clients must begin each connection with
# "please relay TOKEN\n". I will send "ok\n" when the matching connection
# is established, or disconnect if no matching connection is made within
# MAX_WAIT_TIME seconds. I will disconnect if you send data before the
# "ok\n". All data you get after the "ok\n" will be from the other side.
# You will not receive "ok\n" until the other side has also connected and
# submitted a matching token. The token is the same for each side.
# In addition, the connections will be dropped after MAXLENGTH bytes have
# been sent by either side, or MAXTIME seconds have elapsed after the
# matching connections were established. A future API will reveal these
# limits to clients instead of causing mysterious spontaneous failures.
# These relay connections are not half-closeable (unlike full TCP
# connections, applications will not receive any data after half-closing
# their outgoing side). Applications must negotiate shutdown with their
# peer and not close the connection until all data has finished
# transferring in both directions. Applications which only need to send
# data in one direction can use close() as usual.
MAX_WAIT_TIME = 30*SECONDS
MAXLENGTH = 10*MB
MAXTIME = 60*SECONDS
protocol = TransitConnection
def __init__(self, db, blur_usage):
2015-10-04 19:40:12 +00:00
service.MultiService.__init__(self)
2015-12-04 03:45:34 +00:00
self._db = db
self._blur_usage = blur_usage
self._pending_requests = {} # token -> TransitConnection
self._active_connections = set() # TransitConnection
2015-10-04 19:40:12 +00:00
def connection_got_token(self, token, p):
if token in self._pending_requests:
log.msg("transit relay 2: %s" % p.describeToken())
buddy = self._pending_requests.pop(token)
self._active_connections.add(p)
self._active_connections.add(buddy)
2015-10-04 19:40:12 +00:00
p.buddy_connected(buddy)
buddy.buddy_connected(p)
else:
self._pending_requests[token] = p
log.msg("transit relay 1: %s" % p.describeToken())
2015-10-04 19:40:12 +00:00
# TODO: timer
2015-12-04 03:45:34 +00:00
def recordUsage(self, started, result, total_bytes,
total_time, waiting_time):
log.msg("Transit.recordUsage (%dB)" % total_bytes)
if self._blur_usage:
started = self._blur_usage * (started // self._blur_usage)
2015-12-04 03:45:34 +00:00
self._db.execute("INSERT INTO `usage`"
" (`type`, `started`, `result`, `total_bytes`,"
" `total_time`, `waiting_time`)"
" VALUES (?,?,?,?, ?,?)",
(u"transit", started, result, total_bytes,
total_time, waiting_time))
self._db.commit()
def transitFinished(self, p, token, description):
for token,tc in self._pending_requests.items():
2015-10-04 19:40:12 +00:00
if tc is p:
del self._pending_requests[token]
2015-10-04 19:40:12 +00:00
break
log.msg("transitFinished %s" % (description,))
self._active_connections.discard(p)
2015-10-04 19:40:12 +00:00
def transitFailed(self, p):
log.msg("transitFailed %r" % p)
2015-10-04 19:40:12 +00:00
pass