2015-04-09 01:39:33 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
import os, time
|
|
|
|
|
|
|
|
class MyPlugin:
|
|
|
|
tapname = "xyznode"
|
|
|
|
def __init__(self, args):
|
|
|
|
self.args = args
|
|
|
|
def makeService(self, so):
|
|
|
|
# delay this import as late as possible, to allow twistd's code to
|
|
|
|
# accept --reactor= selection
|
2015-10-04 19:40:12 +00:00
|
|
|
from .server import RelayServer
|
2015-04-21 01:34:13 +00:00
|
|
|
return RelayServer(self.args.rendezvous, self.args.transit,
|
2015-09-25 19:47:15 +00:00
|
|
|
self.args.advertise_version,
|
2015-12-04 05:15:19 +00:00
|
|
|
"relay.sqlite", self.args.blur_usage)
|
2015-04-09 01:39:33 +00:00
|
|
|
|
|
|
|
def start_server(args):
|
|
|
|
from twisted.python import usage
|
|
|
|
from twisted.scripts import twistd
|
|
|
|
|
|
|
|
class MyTwistdConfig(twistd.ServerOptions):
|
|
|
|
subCommands = [("XYZ", None, usage.Options, "node")]
|
|
|
|
|
|
|
|
c = MyTwistdConfig()
|
|
|
|
#twistd_args = tuple(args.twistd_args) + ("XYZ",)
|
2015-09-27 21:24:03 +00:00
|
|
|
base_args = []
|
|
|
|
if args.no_daemon:
|
|
|
|
base_args.append("--nodaemon")
|
|
|
|
twistd_args = base_args + ["XYZ"]
|
|
|
|
c.parseOptions(tuple(twistd_args))
|
2015-04-09 01:39:33 +00:00
|
|
|
c.loadedPlugins = {"XYZ": MyPlugin(args)}
|
|
|
|
|
|
|
|
print("starting wormhole relay server")
|
|
|
|
# this forks and never comes back. The parent calls os._exit(0)
|
|
|
|
twistd.runApp(c)
|
|
|
|
|
|
|
|
def kill_server():
|
|
|
|
try:
|
|
|
|
f = open("twistd.pid", "r")
|
|
|
|
except EnvironmentError:
|
|
|
|
print("Unable to find twistd.pid . Is this really a server directory?")
|
|
|
|
return 1
|
|
|
|
pid = int(f.read().strip())
|
|
|
|
f.close()
|
|
|
|
os.kill(pid, 15)
|
|
|
|
print("server process %d sent SIGTERM" % pid)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def stop_server(args):
|
|
|
|
return kill_server()
|
|
|
|
|
|
|
|
def restart_server(args):
|
|
|
|
kill_server()
|
|
|
|
time.sleep(0.1)
|
|
|
|
timeout = 0
|
|
|
|
while os.path.exists("twistd.pid") and timeout < 10:
|
|
|
|
if timeout == 0:
|
|
|
|
print(" waiting for shutdown..")
|
|
|
|
timeout += 1
|
|
|
|
time.sleep(1)
|
|
|
|
if os.path.exists("twistd.pid"):
|
|
|
|
print("error: unable to shut down old server")
|
|
|
|
return 1
|
|
|
|
print(" old server shut down")
|
|
|
|
start_server(args)
|