class App(Application): def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] RequestHandler.set_default_headers = App._set_default_header self.shadowsocks = Shadowsocks() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) if options.password_timeout > 0: PeriodicCallback(self._reset_password, options.password_timeout).start() @staticmethod def _set_default_header(handler): handler.set_header('Server', 'Lover') def _reset_password(self): self.shadowsocks.new_password() if self.shadowsocks.running: self.shadowsocks.stop()
def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] RequestHandler.set_default_headers = App._set_default_header self.shadowsocks = Shadowsocks() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) if options.password_timeout > 0: PeriodicCallback(self._reset_password, options.password_timeout).start()
def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, autoreload=True, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/save', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] if options.wx_token is None: logging.info('wx_token is not configured') else: handlers.append((r'/weixin', WeiXinHandler)) if options.wx_app_id is None or options.wx_secret is None or options.wx_template_id is None: logging.info( 'wx_app_id/wx_secret/wx_template_id is not configured') else: Shadowsocks.add_password_callback( lambda ss: WeiXin.send_ss_info(ss)) RequestHandler.set_default_headers = App._set_default_header try: ss_config = Shadowsocks.read_config() except FileNotFoundError as e: print('can\'t found ' + e.filename) sys.exit(e.errno) Shadowsocks.workers = [ Shadowsocks(i, ss_config) for i in range(options.workers) ] for index in ss_config['running']: Shadowsocks.workers[index].start() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) self.reset_timer = None if options.password_timeout > 0: self._reset_timer_callback() from tornado import autoreload autoreload.add_reload_hook(App._stop_all_worker)
def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, autoreload=True, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/save', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] if options.wx_token is None: logging.info('wx_token is not configured') else: handlers.append((r'/weixin', WeiXinHandler)) if options.wx_app_id is None or options.wx_secret is None or options.wx_template_id is None: logging.info('wx_app_id/wx_secret/wx_template_id is not configured') else: Shadowsocks.add_password_callback(lambda ss: WeiXin.send_ss_info(ss)) RequestHandler.set_default_headers = App._set_default_header try: ss_config = Shadowsocks.read_config() except FileNotFoundError as e: print('can\'t found ' + e.filename) sys.exit(e.errno) Shadowsocks.workers = [Shadowsocks(i, ss_config) for i in range(options.workers)] for index in ss_config['running']: Shadowsocks.workers[index].start() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) self.reset_timer = None if options.password_timeout > 0: self._reset_timer_callback() from tornado import autoreload autoreload.add_reload_hook(App._stop_all_worker)
def get(self): if self.request.path == '/save': Shadowsocks.save_config(Shadowsocks.workers) self.write("OK!") return sid = self.get_query_argument('id', None) if sid is None: self.redirect('/?id=%d' % Shadowsocks.find_latest(Shadowsocks.workers).index) return ss = self._get_ss(sid) qrcode = ss.qrcode(self._get_host()) self.render("index.html", config=ss, qrcode=qrcode)
def _reset_timer_callback(self): looper = ioloop.IOLoop.current() if self.reset_timer is not None: looper.remove_timeout(self.reset_timer) self.reset_timer = None oldest_worker = Shadowsocks.find_oldest(Shadowsocks.workers) if not oldest_worker.running: logging.info("no more running task, try later") looper.call_later(3600, self._reset_timer_callback) return oldest_time = oldest_worker.next_time from datetime import datetime left_time = oldest_time - datetime.now() if left_time.total_seconds() <= 0: logging.info("already timeout, reset password") self._reset_password(oldest_worker) return logging.info("schedule next call later %s" % left_time) self.reset_timer = looper.call_later(left_time.total_seconds(), self._reset_password)
def _build_ss_info(): ss = Shadowsocks.find_latest(Shadowsocks.workers) if not ss.running: ss.start() content = 'Id: %d\n' \ 'Port: %d\n' \ 'Password: %s' \ % (ss.index, ss.port, ss.password) return content
def _reset_password(self, oldest_worker=None): if oldest_worker is None: oldest_worker = Shadowsocks.find_oldest(Shadowsocks.workers) oldest_worker.new_password(is_manual=False) if oldest_worker.running: oldest_worker.stop() oldest_worker.start() self._reset_timer_callback()
def _stop_all_worker(): Shadowsocks.save_config(Shadowsocks.workers) for ss in Shadowsocks.workers: ss.stop()