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

46 lines
2.0 KiB
Python
Raw Normal View History

2015-10-04 19:40:12 +00:00
from __future__ import print_function
from twisted.internet import reactor, endpoints
from twisted.application import service
2015-10-04 19:40:12 +00:00
from twisted.web import server, static, resource
from ..util.endpoint_service import ServerEndpointService
from .. import __version__
from ..database import get_db
2015-10-04 19:46:09 +00:00
from .relay_server import Relay
2015-10-04 19:40:12 +00:00
from .transit_server import Transit
class Root(resource.Resource):
# child_FOO is a nevow thing, not a twisted.web.resource thing
def __init__(self):
resource.Resource.__init__(self)
self.putChild(b"", static.Data(b"Wormhole Relay\n", "text/plain"))
class RelayServer(service.MultiService):
def __init__(self, relayport, transitport, advertise_version,
db_url=":memory:"):
service.MultiService.__init__(self)
self.db = get_db(db_url)
welcome = {
"current_version": __version__,
# adding .motd will cause all clients to display the message,
# then keep running normally
#"motd": "Welcome to the public relay.\nPlease enjoy this service.",
#
# adding .error will cause all clients to fail, with this message
#"error": "This server has been disabled, see URL for details.",
}
if advertise_version:
welcome["current_version"] = advertise_version
self.root = Root()
site = server.Site(self.root)
r = endpoints.serverFromString(reactor, relayport)
self.relayport_service = ServerEndpointService(r, site)
self.relayport_service.setServiceParent(self)
self.relay = Relay(self.db, welcome) # accessible from tests
self.root.putChild(b"wormhole-relay", self.relay)
if transitport:
2015-12-04 03:45:34 +00:00
self.transit = Transit(self.db)
2015-10-04 19:40:12 +00:00
self.transit.setServiceParent(self) # for the timer
t = endpoints.serverFromString(reactor, transitport)
self.transport_service = ServerEndpointService(t, self.transit)
self.transport_service.setServiceParent(self)