2017-11-08 02:35:29 +00:00
|
|
|
import os
|
2017-09-13 07:37:29 +00:00
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.python import usage
|
2017-11-08 01:29:26 +00:00
|
|
|
from twisted.application.service import MultiService
|
2017-11-04 19:54:49 +00:00
|
|
|
from twisted.application.internet import (TimerService,
|
|
|
|
StreamServerEndpointService)
|
2017-09-13 07:37:29 +00:00
|
|
|
from twisted.internet import endpoints
|
2018-02-19 20:28:17 +00:00
|
|
|
from . import transit_server
|
|
|
|
from .increase_rlimits import increase_rlimits
|
2017-09-13 07:37:29 +00:00
|
|
|
|
|
|
|
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=]"
|
2017-09-13 07:37:29 +00:00
|
|
|
longdesc = LONGDESC
|
|
|
|
|
|
|
|
optParameters = [
|
2019-09-11 07:23:50 +00:00
|
|
|
("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)"),
|
2017-09-13 07:37:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def opt_blur_usage(self, arg):
|
2017-11-08 02:14:54 +00:00
|
|
|
self["blur-usage"] = int(arg)
|
2017-09-13 07:37:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def makeService(config, reactor=reactor):
|
2018-02-19 20:28:17 +00:00
|
|
|
increase_rlimits()
|
2017-09-13 07:37:29 +00:00
|
|
|
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"],
|
2017-11-08 01:29:26 +00:00
|
|
|
log_file=log_file,
|
2017-11-04 19:54:49 +00:00
|
|
|
usage_db=config["usage-db"])
|
2017-11-08 01:29:26 +00:00
|
|
|
parent = MultiService()
|
2017-11-04 19:54:49 +00:00
|
|
|
StreamServerEndpointService(ep, f).setServiceParent(parent)
|
2017-11-08 01:29:26 +00:00
|
|
|
TimerService(5*60.0, f.timerUpdateStats).setServiceParent(parent)
|
2017-11-04 19:54:49 +00:00
|
|
|
return parent
|