Пример #1
0
def start_tornado(config, config_filename):
    if not options.cookie_secret:
        logging.warn(
            "\n\n\t\t!!! WARNNING !!!\n\n"
            "You must specify the cookie_secret option. It should be a long "
            "random sequence of bytes to be used as the HMAC secret for the "
            "signature.\n\n"
            "To keep the Shadowsocks Web Interface runable as always it be, "
            "it's signed by a random cookie_secret option. Yes, this chould "
            "keep the service runable, but also effects the users have to "
            "re-login every time the system administrator restart the "
            "service or reboot the system. YOU ARE NOTICED.\n\n"
            "You can create this HMAC string by typing following on server:\n"
            "\t%s --hmac" % sys.argv[0])
        options.cookie_secret = common.hmacstr(
            key=common.randomstr(1000),
            msg=common.randomstr(1000),
            hashtype='sha512')

    handlers = [
        (options.base_url + r"/", RootHandler),
        (options.base_url + r"/robots.txt", RobotsHandler),
        (options.base_url + r"/dashboard", DashboardHandler),
        (options.base_url + r"/login", LoginHandler),
        (options.base_url + r"/logout", LogoutHandler),
        (options.base_url + r"/config", ConfigHandler),
        (options.base_url + r"/control", ServiceControlHandler),
        (options.base_url + r"/control/start", ServiceControlHandler,
            dict(action="start")),
        (options.base_url + r"/control/stop", ServiceControlHandler,
            dict(action="stop")),
        (options.base_url + r"/control/restart", ServiceControlHandler,
            dict(action="restart")),
        (options.base_url + r"/hideme", PlaneConfigHandler),
    ]
    user = userlib.User("ssweb.db")
    user.db_init()
    settings = dict(
        template_path=os.path.join(os.path.dirname(__file__),
                                   "templates/" + options.theme),
        static_path=os.path.join(os.path.dirname(__file__),
                                 "templates/" + options.theme + "/assets"),
        static_url_prefix=options.base_url + "/static/",
        cookie_secret=options.cookie_secret,
        login_url="/login",
        xsrf_cookies=True,
        debug=options.debug,
        user=user,
        svservname=options.service_name)

    application = tornado.web.Application(handlers, **settings)
    application.config = config
    application.config_filename = config_filename
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port, options.host)
    info_("shadowsocks-web start at %s:%s" % (options.host, options.port))
    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        info_("shadowsocksweb is stoped.")
Пример #2
0
def start_tornado(config, config_filename):
    if not options.cookie_secret:
        logging.warn(
            "\n\n\t\t!!! WARNNING !!!\n\n"
            "You must specify the cookie_secret option. It should be a long "
            "random sequence of bytes to be used as the HMAC secret for the "
            "signature.\n\n"
            "To keep the Shadowsocks Web Interface runable as always it be, "
            "it's signed by a random cookie_secret option. Yes, this chould "
            "keep the service runable, but also effects the users have to "
            "re-login every time the system administrator restart the "
            "service or reboot the system. YOU ARE NOTICED.\n\n"
            "You can create this HMAC string by typing following on server:\n"
            "\t%s --hmac" % sys.argv[0])
        options.cookie_secret = common.hmacstr(key=common.randomstr(1000),
                                               msg=common.randomstr(1000),
                                               hashtype='sha512')

    handlers = [
        (options.base_url + r"/", RootHandler),
        (options.base_url + r"/robots.txt", RobotsHandler),
        (options.base_url + r"/dashboard", DashboardHandler),
        (options.base_url + r"/login", LoginHandler),
        (options.base_url + r"/logout", LogoutHandler),
        (options.base_url + r"/config", ConfigHandler),
        (options.base_url + r"/control", ServiceControlHandler),
        (options.base_url + r"/control/start", ServiceControlHandler,
         dict(action="start")),
        (options.base_url + r"/control/stop", ServiceControlHandler,
         dict(action="stop")),
        (options.base_url + r"/control/restart", ServiceControlHandler,
         dict(action="restart")),
        (options.base_url + r"/hideme", PlaneConfigHandler),
    ]
    user = userlib.User("ssweb.db")
    user.db_init()
    settings = dict(template_path=os.path.join(os.path.dirname(__file__),
                                               "templates/" + options.theme),
                    static_path=os.path.join(
                        os.path.dirname(__file__),
                        "templates/" + options.theme + "/assets"),
                    static_url_prefix=options.base_url + "/static/",
                    cookie_secret=options.cookie_secret,
                    login_url="/login",
                    xsrf_cookies=True,
                    debug=options.debug,
                    user=user,
                    svservname=options.service_name)

    application = tornado.web.Application(handlers, **settings)
    application.config = config
    application.config_filename = config_filename
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port, options.host)
    info_("shadowsocks-web start at %s:%s" % (options.host, options.port))
    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        info_("shadowsocksweb is stoped.")
Пример #3
0
def start(demo=False):
    if demo:
        # load demo options. it will escape config file.
        cookie_secret = common.hmacstr(common.randomstr(), common.randomstr())
        args = [sys.argv[0], "--debug", "--host=0.0.0.0", "--port=8080",
                "--base_url=/ssweb", "--service_name=shadowsocks",
                "--cookie_secret=" + cookie_secret, "--logging=debug"]
        options.parse_command_line(args)
    else:
        # pre-parse the command line options. it will be over write by 'load
        # options from config file'. by then, it yet loaded.
        options.parse_command_line()

        if options.config is not None:
            # load options from specified config file
            if not os.path.isfile(options.config):
                err_("Can't find config file '%s'." % options.config)
                exit(1)
            else:
                config = common.load_config(options.config)
                if config is not None:
                    info_("Load config from file '%s'." % options.config)
                    args = [sys.argv[0]]
                    for item in config:
                        args += ["--%s=%s" % (item, config[item])]
                    try:
                        options.parse_command_line(args)
                    except tornado.options.Error:
                        err_("Error on config file option.")
                        sys.exit(1)
        else:
            # load options from config file, if the file exists.
            config_file = common.find_config_file()
            if config_file is not None:
                config = common.load_config(config_file)
                if config is not None:
                    info_("Load config from file '%s'." % config_file)
                    args = [sys.argv[0]]
                    for item in config:
                        args += ["--%s=%s" % (item, config[item])]
                    try:
                        options.parse_command_line(args)
                    except tornado.options.Error:
                        err_("Error on config file option.")
                        sys.exit(1)

        # load options from command line
        try:
            options.parse_command_line()
        except tornado.options.Error:
            err_("Error on command line option.")
            sys.exit(1)
    debug_("options: %s" % json.dumps(options.as_dict(), sort_keys=True))
    logging.debug("options: %s" %
                  json.dumps(options.as_dict(), sort_keys=True))

    # load shadowsocks configuration
    ss_config_filename = common.find_shadowsocks_config_file()
    if ss_config_filename is None:
        err_("Can't find any shadowsocks config file. Are you sure there "
             "installed shadowsocks already?")
        exit(1)
    config = common.load_shadowsocks_config(ss_config_filename)
    info_("Loading shadowsocks config from file '%s'." % ss_config_filename)
    start_tornado(config, ss_config_filename)
Пример #4
0
def start(demo=False):
    if demo:
        # load demo options. it will escape config file.
        cookie_secret = common.hmacstr(common.randomstr(), common.randomstr())
        args = [
            sys.argv[0], "--debug", "--host=0.0.0.0", "--port=8080",
            "--base_url=/ssweb", "--service_name=shadowsocks",
            "--cookie_secret=" + cookie_secret, "--logging=debug"
        ]
        options.parse_command_line(args)
    else:
        # pre-parse the command line options. it will be over write by 'load
        # options from config file'. by then, it yet loaded.
        options.parse_command_line()

        if options.config is not None:
            # load options from specified config file
            if not os.path.isfile(options.config):
                err_("Can't find config file '%s'." % options.config)
                exit(1)
            else:
                config = common.load_config(options.config)
                if config is not None:
                    info_("Load config from file '%s'." % options.config)
                    args = [sys.argv[0]]
                    for item in config:
                        args += ["--%s=%s" % (item, config[item])]
                    try:
                        options.parse_command_line(args)
                    except tornado.options.Error:
                        err_("Error on config file option.")
                        sys.exit(1)
        else:
            # load options from config file, if the file exists.
            config_file = common.find_config_file()
            if config_file is not None:
                config = common.load_config(config_file)
                if config is not None:
                    info_("Load config from file '%s'." % config_file)
                    args = [sys.argv[0]]
                    for item in config:
                        args += ["--%s=%s" % (item, config[item])]
                    try:
                        options.parse_command_line(args)
                    except tornado.options.Error:
                        err_("Error on config file option.")
                        sys.exit(1)

        # load options from command line
        try:
            options.parse_command_line()
        except tornado.options.Error:
            err_("Error on command line option.")
            sys.exit(1)
    debug_("options: %s" % json.dumps(options.as_dict(), sort_keys=True))
    logging.debug("options: %s" %
                  json.dumps(options.as_dict(), sort_keys=True))

    # load shadowsocks configuration
    ss_config_filename = common.find_shadowsocks_config_file()
    if ss_config_filename is None:
        err_("Can't find any shadowsocks config file. Are you sure there "
             "installed shadowsocks already?")
        exit(1)
    config = common.load_shadowsocks_config(ss_config_filename)
    info_("Loading shadowsocks config from file '%s'." % ss_config_filename)
    start_tornado(config, ss_config_filename)