Пример #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 post(self):
        common.error_log('start upload.....')
        self.set_header('Content-Type', 'text/html')
        rspd = {'status': 201, 'msg': 'ok'}
        callback = self.get_argument('callback', 'callback');
        common.error_log('callback is ' + callback)
        filetoupload = self.request.files['filetoupload']
        common.error_log('crash')
        isimage = 0
        if filetoupload:
            myfile = filetoupload[0]

            try:

                file_type = myfile['filename'].split('.')[-1].lower()

                isimage = file_type in ['jpg', 'jpeg', 'gif', 'bmp']
                new_file_name = "%s.%s" % (randomstr(10, 2), file_type)

            except Exception, e:
                print str(e)
                file_type = ''
                new_file_name = randomstr(10, 2)
                ##
            mime_type = myfile['content_type']
            encoding = None
            ###

            try:
                attachment_url = put_obj2storage(file_name=new_file_name, data=myfile['body'], expires='365',
                                                 content_type=mime_type, encoding=encoding)
            except Exception, e:
                print str(e)
                attachment_url = ''
Пример #3
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.")
Пример #4
0
def print_hmac():
    key = common.randomstr()
    msg = common.randomstr()
    print((Fore.GREEN + "HMAC-MD5    ( 32 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "md5"))
    print((Fore.GREEN + "HMAC-SHA224 ( 56 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha224"))
    print((Fore.GREEN + "HMAC-SHA256 ( 64 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha256"))
    print((Fore.GREEN + "HMAC-SHA512 (128 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha512"))
Пример #5
0
def print_hmac():
    key = common.randomstr()
    msg = common.randomstr()
    print((Fore.GREEN + "HMAC-MD5    ( 32 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "md5"))
    print((Fore.GREEN + "HMAC-SHA224 ( 56 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha224"))
    print((Fore.GREEN + "HMAC-SHA256 ( 64 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha256"))
    print((Fore.GREEN + "HMAC-SHA512 (128 bits):" + Fore.RESET + " %s") %
          common.hmacstr(key, msg, "sha512"))
Пример #6
0
    def post(self):
        import common

        username = self.get_argument('username', '')
        if not username:
            self.flash(u'请输入用户名')
            self.redirect('')
        else:
            user = BlogUser.get_user_by_username(username)
            if not user:
                self.flash(u'用户名不存在')
                self.redirect('')
            else:
                kvdata = {"user": username, "email": user.email}
                key = common.md5(common.randomstr(20))
                common.set_kvdb_value(key, kvdata)
                url = 'http://%s/reset/%s/' % (self.request.host, key)
                common.sendmail(user.email, u'重置密码', url)
                self.flash(u'邮件发送成功')
                self.redirect('')
Пример #7
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)
Пример #8
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)