magic-wormhole-transit-relay/src/wormhole_transit_relay/server_tap.py

45 lines
1.7 KiB
Python
Raw Normal View History

2017-11-08 02:35:29 +00:00
import os
from twisted.internet import reactor
from twisted.python import usage
from twisted.application.service import MultiService
2017-11-04 19:54:49 +00:00
from twisted.application.internet import (TimerService,
StreamServerEndpointService)
from twisted.internet import endpoints
from . import transit_server
from .increase_rlimits import increase_rlimits
LONGDESC = """\
This plugin sets up a 'Transit Relay' server for magic-wormhole. This service
listens for TCP connections, finds pairs which present the same handshake, and
glues the two TCP sockets together.
"""
class Options(usage.Options):
2017-11-08 02:35:29 +00:00
synopsis = "[--port=] [--log-fd] [--blur-usage=] [--usage-db=]"
longdesc = LONGDESC
optParameters = [
("port", "p", "tcp:4001:interface=\:\:", "endpoint to listen on"),
2017-09-13 19:29:51 +00:00
("blur-usage", None, None, "blur timestamps and data sizes in logs"),
2017-11-08 02:35:29 +00:00
("log-fd", None, None, "write JSON usage logs to this file descriptor"),
2017-11-04 19:54:49 +00:00
("usage-db", None, None, "record usage data (SQLite)"),
]
def opt_blur_usage(self, arg):
2017-11-08 02:14:54 +00:00
self["blur-usage"] = int(arg)
def makeService(config, reactor=reactor):
increase_rlimits()
ep = endpoints.serverFromString(reactor, config["port"]) # to listen
2017-11-08 02:35:29 +00:00
log_file = (os.fdopen(int(config["log-fd"]), "w")
if config["log-fd"] is not None
else None)
2017-09-13 19:29:51 +00:00
f = transit_server.Transit(blur_usage=config["blur-usage"],
log_file=log_file,
2017-11-04 19:54:49 +00:00
usage_db=config["usage-db"])
parent = MultiService()
2017-11-04 19:54:49 +00:00
StreamServerEndpointService(ep, f).setServiceParent(parent)
TimerService(5*60.0, f.timerUpdateStats).setServiceParent(parent)
2017-11-04 19:54:49 +00:00
return parent