def client(): with app.test_client() as client: with client.session_transaction() as session: session['uuid'] = 'test' session['key'] = generate_user_key() session['config'] = {} yield client
def before_request_func(): g.request_params = ( request.args if request.method == 'GET' else request.form ) # Skip pre-request actions if verifying session if '/session' in request.path and not valid_user_session(session): return default_config = json.load(open(app.config['DEFAULT_CONFIG'])) \ if os.path.exists(app.config['DEFAULT_CONFIG']) else {} # Generate session values for user if unavailable if (not valid_user_session(session) and 'cookies_disabled' not in request.args): session['config'] = default_config session['uuid'] = str(uuid.uuid4()) session['key'] = generate_user_key() # Skip checking for session on any searches that don't # require a valid session if (not Endpoint.autocomplete.in_path(request.path) and not Endpoint.healthz.in_path(request.path)): return redirect(url_for( 'session_check', session_id=session['uuid'], follow=get_request_url(request.url)), code=307) else: g.user_config = Config(**session['config']) elif 'cookies_disabled' not in request.args: # Set session as permanent session.permanent = True app.permanent_session_lifetime = timedelta(days=365) g.user_config = Config(**session['config']) else: # User has cookies disabled, fall back to immutable default config session.pop('_permanent', None) g.user_config = Config(**default_config) if not g.user_config.url: g.user_config.url = get_request_url(request.url_root) g.user_request = Request( request.headers.get('User-Agent'), get_request_url(request.url_root), config=g.user_config) g.app_location = g.user_config.url
def get_search_results(data): secret_key = generate_user_key() soup = Filter(user_key=secret_key, config=Config(**demo_config)).clean( BeautifulSoup(data, 'html.parser')) main_divs = soup.find('div', {'id': 'main'}) assert len(main_divs) > 1 result_divs = [] for div in main_divs: # Result divs should only have 1 inner div if (len(list(div.children)) != 1 or not div.findChild() or 'div' not in div.findChild().name): continue result_divs.append(div) return result_divs
import os from stem import Signal import threading from dotenv import load_dotenv app = Flask(__name__, static_folder=os.path.dirname(os.path.abspath(__file__)) + '/static') # Load .env file if enabled if os.getenv('WHOOGLE_DOTENV', ''): dotenv_path = '../whoogle.env' load_dotenv( os.path.join(os.path.dirname(os.path.abspath(__file__)), dotenv_path)) app.default_key = generate_user_key() app.config['SECRET_KEY'] = os.urandom(32) app.config['SESSION_TYPE'] = 'filesystem' app.config['SESSION_COOKIE_SAMESITE'] = 'strict' if os.getenv('HTTPS_ONLY'): app.config['SESSION_COOKIE_NAME'] = '__Secure-session' app.config['SESSION_COOKIE_SECURE'] = True app.config['VERSION_NUMBER'] = '0.7.1' app.config['APP_ROOT'] = os.getenv('APP_ROOT', os.path.dirname(os.path.abspath(__file__))) app.config['STATIC_FOLDER'] = os.getenv( 'STATIC_FOLDER', os.path.join(app.config['APP_ROOT'], 'static')) app.config['BUILD_FOLDER'] = os.path.join(app.config['STATIC_FOLDER'], 'build') app.config['CACHE_BUSTING_MAP'] = {}
def before_request_func(): global bang_json # Check for latest version if needed now = datetime.now() if now - timedelta(hours=24) > app.config['LAST_UPDATE_CHECK']: app.config['LAST_UPDATE_CHECK'] = now app.config['HAS_UPDATE'] = check_for_update( app.config['RELEASES_URL'], app.config['VERSION_NUMBER']) g.request_params = ( request.args if request.method == 'GET' else request.form ) # Skip pre-request actions if verifying session if '/session' in request.path and not valid_user_session(session): return default_config = json.load(open(app.config['DEFAULT_CONFIG'])) \ if os.path.exists(app.config['DEFAULT_CONFIG']) else {} # Generate session values for user if unavailable if (not valid_user_session(session) and 'cookies_disabled' not in request.args): session['config'] = default_config session['uuid'] = str(uuid.uuid4()) session['key'] = generate_user_key() # Skip checking for session on any searches that don't # require a valid session if (not Endpoint.autocomplete.in_path(request.path) and not Endpoint.healthz.in_path(request.path) and not Endpoint.opensearch.in_path(request.path)): return redirect(url_for( 'session_check', session_id=session['uuid'], follow=get_request_url(request.url)), code=307) else: g.user_config = Config(**session['config']) elif 'cookies_disabled' not in request.args: # Set session as permanent session.permanent = True app.permanent_session_lifetime = timedelta(days=365) g.user_config = Config(**session['config']) else: # User has cookies disabled, fall back to immutable default config session.pop('_permanent', None) g.user_config = Config(**default_config) if not g.user_config.url: g.user_config.url = get_request_url(request.url_root) g.user_request = Request( request.headers.get('User-Agent'), get_request_url(request.url_root), config=g.user_config) g.app_location = g.user_config.url # Attempt to reload bangs json if not generated yet if not bang_json and os.path.getsize(app.config['BANG_FILE']) > 4: try: bang_json = json.load(open(app.config['BANG_FILE'])) except json.decoder.JSONDecodeError: # Ignore decoding error, can occur if file is still # being written pass