def setup(cls): def app(env, start_response): result = ('Proxied: ' + env['PATH_INFO']).encode('utf-8') headers = [('Content-Length', str(len(result)))] start_response('200 OK', headers=headers) return iter([result]) from wsgiprox.wsgiprox import WSGIProxMiddleware wsgiprox = WSGIProxMiddleware(app, '/') class NoLogServer(WSGIServer): def handle_error(self, request, client_address): pass server = make_server('localhost', 0, wsgiprox, server_class=NoLogServer) addr, cls.port = server.socket.getsockname() cls.proxies = {'https': 'localhost:' + str(cls.port), 'http': 'localhost:' + str(cls.port) } def run(): try: server.serve_forever() except Exception as e: print(e) thread = threading.Thread(target=run) thread.daemon = True thread.start() time.sleep(0.1)
def init_proxy(self, config): """Initialize and start proxy mode. If proxy configuration entry is not contained in the config this is a no op. Causes handler to become an instance of WSGIProxMiddleware. :param dict config: The configuration object used to configure this instance of FrontEndApp """ proxy_config = config.get('proxy') if not proxy_config: return if isinstance(proxy_config, str): proxy_coll = proxy_config proxy_config = {} else: proxy_coll = proxy_config['coll'] if '/' in proxy_coll: raise Exception('Proxy collection can not contain "/"') proxy_config['ca_name'] = proxy_config.get('ca_name', self.PROXY_CA_NAME) proxy_config['ca_file_cache'] = proxy_config.get( 'ca_file_cache', self.PROXY_CA_PATH) if proxy_config.get('recording'): logging.info( 'Proxy recording into collection "{0}"'.format(proxy_coll)) if proxy_coll in self.warcserver.list_fixed_routes(): raise Exception('Can not record into fixed collection') proxy_coll += self.RECORD_ROUTE if not config.get('recorder'): config['recorder'] = 'live' else: logging.info( 'Proxy enabled for collection "{0}"'.format(proxy_coll)) if proxy_config.get('enable_content_rewrite', True): self.proxy_prefix = '/{0}/bn_/'.format(proxy_coll) else: self.proxy_prefix = '/{0}/id_/'.format(proxy_coll) self.proxy_coll = proxy_coll self.handler = WSGIProxMiddleware(self.handle_request, self.proxy_route_request, proxy_host=proxy_config.get( 'host', 'pywb.proxy'), proxy_options=proxy_config)
def make_application(test_ca_file=None, proxy_options=None): proxy_options = proxy_options or {} if test_ca_file is None: test_ca_file = os.environ.get('CA_ROOT_FILE', os.path.join('.', 'wsgiprox-ca-test.pem')) proxy_options['ca_name'] = 'wsgiprox test ca' proxy_options['ca_file_cache'] = test_ca_file from wsgiprox.wsgiprox import WSGIProxMiddleware return WSGIProxMiddleware(TestWSGI(), '/prefix/', proxy_options=proxy_options, proxy_apps={'proxy-alias': '', 'proxy-app-1': CustomApp() } )
def init_proxy(self, config): proxy_config = config.get('proxy') self.proxy_prefix = None if not proxy_config: return if isinstance(proxy_config, str): proxy_coll = proxy_config proxy_config = {} else: proxy_coll = proxy_config['coll'] if '/' in proxy_coll: raise Exception('Proxy collection can not contain "/"') proxy_config['ca_name'] = proxy_config.get('ca_name', self.PROXY_CA_NAME) proxy_config['ca_file_cache'] = proxy_config.get( 'ca_file_cache', self.PROXY_CA_PATH) if proxy_config.get('recording'): logging.info( 'Proxy recording into collection "{0}"'.format(proxy_coll)) if proxy_coll in self.warcserver.list_fixed_routes(): raise Exception('Can not record into fixed collection') proxy_coll += self.RECORD_ROUTE if not config.get('recorder'): config['recorder'] = 'live' else: logging.info( 'Proxy enabled for collection "{0}"'.format(proxy_coll)) if proxy_config.get('use_head_insert', True): self.proxy_prefix = '/{0}/bn_/'.format(proxy_coll) else: self.proxy_prefix = '/{0}/id_/'.format(proxy_coll) self.handler = WSGIProxMiddleware(self.handle_request, self.proxy_route_request, proxy_host=proxy_config.get( 'host', 'pywb.proxy'), proxy_options=proxy_config)
def init_proxy(self, config): """Initialize and start proxy mode. If proxy configuration entry is not contained in the config this is a no op. Causes handler to become an instance of WSGIProxMiddleware. :param dict config: The configuration object used to configure this instance of FrontEndApp """ proxy_config = config.get('proxy') if not proxy_config: return if isinstance(proxy_config, str): proxy_coll = proxy_config proxy_config = {} else: proxy_coll = proxy_config['coll'] if '/' in proxy_coll: raise Exception('Proxy collection can not contain "/"') proxy_config['ca_name'] = proxy_config.get('ca_name', self.PROXY_CA_NAME) proxy_config['ca_file_cache'] = proxy_config.get( 'ca_file_cache', self.PROXY_CA_PATH) if proxy_config.get('recording'): logging.info( 'Proxy recording into collection "{0}"'.format(proxy_coll)) if proxy_coll in self.warcserver.list_fixed_routes(): raise Exception('Can not record into fixed collection') proxy_route = proxy_coll + self.RECORD_ROUTE if not config.get('recorder'): config['recorder'] = 'live' self.proxy_record = True else: logging.info( 'Proxy enabled for collection "{0}"'.format(proxy_coll)) self.proxy_record = False proxy_route = proxy_coll if proxy_config.get('enable_content_rewrite', True): self.proxy_prefix = '/{0}/bn_/'.format(proxy_route) else: self.proxy_prefix = '/{0}/id_/'.format(proxy_route) self.proxy_default_timestamp = proxy_config.get('default_timestamp') if self.proxy_default_timestamp: if not self.ALL_DIGITS.match(self.proxy_default_timestamp): try: self.proxy_default_timestamp = iso_date_to_timestamp( self.proxy_default_timestamp) except Exception: raise Exception( 'Invalid Proxy Timestamp: Must Be All-Digit Timestamp or ISO Date Format' ) self.proxy_coll = proxy_coll self.handler = WSGIProxMiddleware(self.handle_request, self.proxy_route_request, proxy_host=proxy_config.get( 'host', 'pywb.proxy'), proxy_options=proxy_config)
def __init__(self, redis_url=None): self._init_logging() if getattr(sys, 'frozen', False): self.static_root = os.path.join(sys._MEIPASS, 'webrecorder', 'static/') else: self.static_root = resource_filename('webrecorder', 'static/') # only launch if running in place, not from installed package if '.egg' not in __file__: spawn_once(default_build, worker=1, force_build=False) BaseRequest.MEMFILE_MAX = 500000 # 500kb bottle_app = APIBottle() self.bottle_app = bottle_app # JSON encoding for datetime objects # self.bottle_app.install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=CustomJSONEncoder))) config = load_wr_config() # Init Redis if not redis_url: redis_url = os.environ['REDIS_BASE_URL'] self.redis = redis.StrictRedis.from_url(redis_url, decode_responses=True) browser_redis = redis.StrictRedis.from_url(os.environ['REDIS_BROWSER_URL'], decode_responses=True) session_redis = redis.StrictRedis.from_url(os.environ['REDIS_SESSION_URL']) self.content_error_redirect = os.environ.get('CONTENT_ERROR_REDIRECT') # Init Jinja jinja_env = self.init_jinja_env(config) # Init Cork cork = WebRecCork.create_cork(self.redis, config) # User Manager user_manager = UserManager(redis=self.redis, cork=cork, config=config) # Init Browser Mgr browser_mgr = BrowserManager(config, browser_redis, user_manager) # Init Dat Share DatShare.dat_share = DatShare(self.redis) # Init Content Loader/Rewriter content_app = ContentController(app=bottle_app, jinja_env=jinja_env, user_manager=user_manager, config=config, browser_mgr=browser_mgr, redis=self.redis, cork=cork) # Init Sesion temp_prefix Session.temp_prefix = config['temp_prefix'] kwargs = dict(app=bottle_app, jinja_env=jinja_env, user_manager=user_manager, browser_mgr=browser_mgr, content_app=content_app, cork=cork, redis=self.redis, session_redis=session_redis, config=config) # Init Core app controllers for controller_type in self.ALL_CONTROLLERS: x = controller_type(**kwargs) # Set Error Handler bottle_app.default_error_handler = self.make_err_handler( bottle_app.default_error_handler) final_app = RedisSessionMiddleware(bottle_app, cork, session_redis, config, access_cls=SessionAccessCache, access_redis=self.redis) final_app = WSGIProxMiddleware(final_app, '/_proxy/', proxy_host='webrecorder.proxy', proxy_options=self._get_proxy_options()) kwargs['app'] = final_app super(MainController, self).__init__(**kwargs) self.browser_mgr = browser_mgr self.content_app = content_app wr_api_spec.build_api_spec()
def __init__(self, redis_url=None): self._init_logging() if getattr(sys, 'frozen', False): self.static_root = os.path.join(sys._MEIPASS, 'webrecorder', 'static/') else: self.static_root = resource_filename('webrecorder', 'static/') bottle_app = Bottle() self.bottle_app = bottle_app # JSON encoding for datetime objects self.bottle_app.install( JSONPlugin( json_dumps=lambda s: json.dumps(s, cls=CustomJSONEncoder))) config = load_wr_config() # Init Redis if not redis_url: redis_url = os.environ['REDIS_BASE_URL'] self.redis = redis.StrictRedis.from_url(redis_url, decode_responses=True) self.browser_redis = redis.StrictRedis.from_url( os.environ['REDIS_BROWSER_URL'], decode_responses=True) self.session_redis = redis.StrictRedis.from_url( os.environ['REDIS_SESSION_URL']) # Auto Upload on Init Id self.init_upload_id = config.get('init_upload_id') self.init_upload_user = config.get('init_upload_user') self.init_upload_coll = config.get('init_upload_coll') # Init Jinja jinja_env = self.init_jinja_env(config) # Init Content Loader/Rewriter self.content_app = ContentController(app=bottle_app, jinja_env=jinja_env, config=config, redis=self.redis) # Init Browser Mgr self.browser_mgr = BrowserManager(config, self.browser_redis, self.content_app) # Init Cork self.cork = WebRecCork.create_cork(self.redis, config) # Init Manager manager = RedisDataManager(self.redis, self.cork, self.content_app, self.browser_redis, self.browser_mgr, config) # Init Sesion temp_prefix Session.temp_prefix = config['temp_prefix'] # Init Core app controllers for controller_type in self.ALL_CONTROLLERS: x = controller_type(app=bottle_app, jinja_env=jinja_env, manager=manager, config=config) # Set Error Handler bottle_app.default_error_handler = self.make_err_handler( bottle_app.default_error_handler) final_app = RedisSessionMiddleware(bottle_app, self.cork, self.session_redis, config) final_app = WSGIProxMiddleware(final_app, '/_proxy/', proxy_host='webrecorder.proxy', proxy_options=self._get_proxy_options()) super(AppController, self).__init__(final_app, jinja_env, manager, config)