server: add --advertise-version option
This commit is contained in:
parent
549b348aee
commit
c393e09e8a
|
@ -36,6 +36,8 @@ sp_start.add_argument("--rendezvous", default="tcp:3000", metavar="tcp:PORT",
|
||||||
help="endpoint specification for the rendezvous port")
|
help="endpoint specification for the rendezvous port")
|
||||||
sp_start.add_argument("--transit", default="tcp:3001", metavar="tcp:PORT",
|
sp_start.add_argument("--transit", default="tcp:3001", metavar="tcp:PORT",
|
||||||
help="endpoint specification for the transit-relay port")
|
help="endpoint specification for the transit-relay port")
|
||||||
|
sp_start.add_argument("--advertise-version", metavar="VERSION",
|
||||||
|
help="version to recommend to clients")
|
||||||
#sp_start.add_argument("twistd_args", nargs="*", default=None,
|
#sp_start.add_argument("twistd_args", nargs="*", default=None,
|
||||||
# metavar="[TWISTD-ARGS..]",
|
# metavar="[TWISTD-ARGS..]",
|
||||||
# help=dedent("""\
|
# help=dedent("""\
|
||||||
|
@ -51,6 +53,8 @@ sp_restart.add_argument("--rendezvous", default="tcp:3000", metavar="tcp:PORT",
|
||||||
help="endpoint specification for the rendezvous port")
|
help="endpoint specification for the rendezvous port")
|
||||||
sp_restart.add_argument("--transit", default="tcp:3001", metavar="tcp:PORT",
|
sp_restart.add_argument("--transit", default="tcp:3001", metavar="tcp:PORT",
|
||||||
help="endpoint specification for the transit-relay port")
|
help="endpoint specification for the transit-relay port")
|
||||||
|
sp_restart.add_argument("--advertise-version", metavar="VERSION",
|
||||||
|
help="version to recommend to clients")
|
||||||
sp_restart.set_defaults(func=cmd_server.restart_server)
|
sp_restart.set_defaults(func=cmd_server.restart_server)
|
||||||
|
|
||||||
# CLI: send-text
|
# CLI: send-text
|
||||||
|
|
|
@ -9,7 +9,8 @@ class MyPlugin:
|
||||||
# delay this import as late as possible, to allow twistd's code to
|
# delay this import as late as possible, to allow twistd's code to
|
||||||
# accept --reactor= selection
|
# accept --reactor= selection
|
||||||
from .relay import RelayServer
|
from .relay import RelayServer
|
||||||
return RelayServer(self.args.rendezvous, self.args.transit)
|
return RelayServer(self.args.rendezvous, self.args.transit,
|
||||||
|
self.args.advertise_version)
|
||||||
|
|
||||||
def start_server(args):
|
def start_server(args):
|
||||||
from twisted.python import usage
|
from twisted.python import usage
|
||||||
|
|
|
@ -46,15 +46,6 @@ class EventsProtocol:
|
||||||
|
|
||||||
# note: no versions of IE (including the current IE11) support EventSource
|
# note: no versions of IE (including the current IE11) support EventSource
|
||||||
|
|
||||||
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.",
|
|
||||||
}
|
|
||||||
|
|
||||||
# relay URLs are:
|
# relay URLs are:
|
||||||
# GET /list -> {channel-ids: [INT..]}
|
# GET /list -> {channel-ids: [INT..]}
|
||||||
# POST /allocate/SIDE -> {channel-id: INT}
|
# POST /allocate/SIDE -> {channel-id: INT}
|
||||||
|
@ -67,10 +58,11 @@ WELCOME = {
|
||||||
class Channel(resource.Resource):
|
class Channel(resource.Resource):
|
||||||
isLeaf = True # I handle /CHANNEL-ID/*
|
isLeaf = True # I handle /CHANNEL-ID/*
|
||||||
|
|
||||||
def __init__(self, channel_id, relay):
|
def __init__(self, channel_id, relay, welcome):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
self.channel_id = channel_id
|
self.channel_id = channel_id
|
||||||
self.relay = relay
|
self.relay = relay
|
||||||
|
self.welcome = welcome
|
||||||
self.expire_at = time.time() + CHANNEL_EXPIRATION_TIME
|
self.expire_at = time.time() + CHANNEL_EXPIRATION_TIME
|
||||||
self.sides = set()
|
self.sides = set()
|
||||||
self.messages = [] # (side, msgnum, str)
|
self.messages = [] # (side, msgnum, str)
|
||||||
|
@ -89,7 +81,7 @@ class Channel(resource.Resource):
|
||||||
return "Must use EventSource (Content-Type: text/event-stream)"
|
return "Must use EventSource (Content-Type: text/event-stream)"
|
||||||
request.setHeader("content-type", "text/event-stream")
|
request.setHeader("content-type", "text/event-stream")
|
||||||
ep = EventsProtocol(request)
|
ep = EventsProtocol(request)
|
||||||
ep.sendEvent(json.dumps(WELCOME), name="welcome")
|
ep.sendEvent(json.dumps(self.welcome), name="welcome")
|
||||||
handle = (their_side, their_msgnum, ep)
|
handle = (their_side, their_msgnum, ep)
|
||||||
self.event_channels.add(handle)
|
self.event_channels.add(handle)
|
||||||
request.notifyFinish().addErrback(self._shutdown, handle)
|
request.notifyFinish().addErrback(self._shutdown, handle)
|
||||||
|
@ -140,36 +132,40 @@ class Channel(resource.Resource):
|
||||||
self.message_added(side, msgnum, data["message"])
|
self.message_added(side, msgnum, data["message"])
|
||||||
|
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader("content-type", "application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": WELCOME,
|
return json.dumps({"welcome": self.welcome,
|
||||||
"messages": other_messages})+"\n"
|
"messages": other_messages})+"\n"
|
||||||
|
|
||||||
class Allocator(resource.Resource):
|
class Allocator(resource.Resource):
|
||||||
isLeaf = True
|
isLeaf = True
|
||||||
def __init__(self, relay):
|
def __init__(self, relay, welcome):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
self.relay = relay
|
self.relay = relay
|
||||||
|
self.welcome = welcome
|
||||||
def render_POST(self, request):
|
def render_POST(self, request):
|
||||||
side = request.postpath[0]
|
side = request.postpath[0]
|
||||||
channel_id = self.relay.allocate_channel_id()
|
channel_id = self.relay.allocate_channel_id()
|
||||||
self.relay.channels[channel_id] = Channel(channel_id, self.relay)
|
self.relay.channels[channel_id] = Channel(channel_id, self.relay,
|
||||||
|
self.welcome)
|
||||||
log.msg("allocated #%d, now have %d channels" %
|
log.msg("allocated #%d, now have %d channels" %
|
||||||
(channel_id, len(self.relay.channels)))
|
(channel_id, len(self.relay.channels)))
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader("content-type", "application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": WELCOME,
|
return json.dumps({"welcome": self.welcome,
|
||||||
"channel-id": channel_id})+"\n"
|
"channel-id": channel_id})+"\n"
|
||||||
|
|
||||||
class ChannelList(resource.Resource):
|
class ChannelList(resource.Resource):
|
||||||
def __init__(self, channel_ids):
|
def __init__(self, channel_ids, welcome):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
self.channel_ids = channel_ids
|
self.channel_ids = channel_ids
|
||||||
|
self.welcome = welcome
|
||||||
def render_GET(self, request):
|
def render_GET(self, request):
|
||||||
request.setHeader("content-type", "application/json; charset=utf-8")
|
request.setHeader("content-type", "application/json; charset=utf-8")
|
||||||
return json.dumps({"welcome": WELCOME,
|
return json.dumps({"welcome": self.welcome,
|
||||||
"channel-ids": self.channel_ids})+"\n"
|
"channel-ids": self.channel_ids})+"\n"
|
||||||
|
|
||||||
class Relay(resource.Resource):
|
class Relay(resource.Resource):
|
||||||
def __init__(self):
|
def __init__(self, welcome):
|
||||||
resource.Resource.__init__(self)
|
resource.Resource.__init__(self)
|
||||||
|
self.welcome = welcome
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
|
|
||||||
def prune_old_channels(self):
|
def prune_old_channels(self):
|
||||||
|
@ -197,10 +193,10 @@ class Relay(resource.Resource):
|
||||||
|
|
||||||
def getChild(self, path, request):
|
def getChild(self, path, request):
|
||||||
if path == "allocate":
|
if path == "allocate":
|
||||||
return Allocator(self)
|
return Allocator(self, self.welcome)
|
||||||
if path == "list":
|
if path == "list":
|
||||||
channel_ids = sorted(self.channels.keys())
|
channel_ids = sorted(self.channels.keys())
|
||||||
return ChannelList(channel_ids)
|
return ChannelList(channel_ids, self.welcome)
|
||||||
if not re.search(r'^\d+$', path):
|
if not re.search(r'^\d+$', path):
|
||||||
return resource.ErrorPage(http.BAD_REQUEST,
|
return resource.ErrorPage(http.BAD_REQUEST,
|
||||||
"invalid channel id",
|
"invalid channel id",
|
||||||
|
@ -209,7 +205,7 @@ class Relay(resource.Resource):
|
||||||
if not channel_id in self.channels:
|
if not channel_id in self.channels:
|
||||||
log.msg("claimed #%d, now have %d channels" %
|
log.msg("claimed #%d, now have %d channels" %
|
||||||
(channel_id, len(self.channels)))
|
(channel_id, len(self.channels)))
|
||||||
self.channels[channel_id] = Channel(channel_id, self)
|
self.channels[channel_id] = Channel(channel_id, self, self.welcome)
|
||||||
return self.channels[channel_id]
|
return self.channels[channel_id]
|
||||||
|
|
||||||
def free_child(self, channel_id):
|
def free_child(self, channel_id):
|
||||||
|
@ -344,13 +340,25 @@ class Root(resource.Resource):
|
||||||
self.putChild("", static.Data("Wormhole Relay\n", "text/plain"))
|
self.putChild("", static.Data("Wormhole Relay\n", "text/plain"))
|
||||||
|
|
||||||
class RelayServer(service.MultiService):
|
class RelayServer(service.MultiService):
|
||||||
def __init__(self, relayport, transitport):
|
def __init__(self, relayport, transitport, advertise_version):
|
||||||
service.MultiService.__init__(self)
|
service.MultiService.__init__(self)
|
||||||
|
|
||||||
|
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()
|
self.root = Root()
|
||||||
site = server.Site(self.root)
|
site = server.Site(self.root)
|
||||||
self.relayport_service = strports.service(relayport, site)
|
self.relayport_service = strports.service(relayport, site)
|
||||||
self.relayport_service.setServiceParent(self)
|
self.relayport_service.setServiceParent(self)
|
||||||
self.relay = Relay() # accessible from tests
|
self.relay = Relay(welcome) # accessible from tests
|
||||||
self.root.putChild("wormhole-relay", self.relay)
|
self.root.putChild("wormhole-relay", self.relay)
|
||||||
t = internet.TimerService(5*MINUTE, self.relay.prune_old_channels)
|
t = internet.TimerService(5*MINUTE, self.relay.prune_old_channels)
|
||||||
t.setServiceParent(self)
|
t.setServiceParent(self)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user