2020-04-24 02:59:43 +00:00
|
|
|
from app import app
|
2020-04-15 23:41:53 +00:00
|
|
|
from app.filter import Filter
|
2020-04-24 02:59:43 +00:00
|
|
|
from app.request import Request, gen_query
|
2020-05-07 00:13:02 +00:00
|
|
|
import argparse
|
2020-01-23 06:19:17 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2020-04-29 00:19:34 +00:00
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
2020-04-28 02:21:36 +00:00
|
|
|
from flask import g, make_response, request, redirect, render_template, send_file
|
|
|
|
import io
|
2020-04-05 01:30:53 +00:00
|
|
|
import json
|
2020-01-21 20:26:49 +00:00
|
|
|
import os
|
2020-02-21 23:52:29 +00:00
|
|
|
import urllib.parse as urlparse
|
2020-01-21 20:26:49 +00:00
|
|
|
|
2020-04-15 23:41:53 +00:00
|
|
|
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'))
|
2020-04-05 23:59:50 +00:00
|
|
|
|
2020-05-07 00:39:12 +00:00
|
|
|
CONFIG_PATH = app.config['STATIC_FOLDER'] + '/config.json'
|
2020-04-05 01:30:53 +00:00
|
|
|
|
2020-02-21 23:52:29 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request_func():
|
|
|
|
g.user_request = Request(request.headers.get('User-Agent'))
|
2020-05-07 00:39:12 +00:00
|
|
|
g.user_config = json.load(open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else {}
|
2020-01-22 05:51:02 +00:00
|
|
|
|
2020-01-21 20:26:49 +00:00
|
|
|
|
2020-05-06 00:28:43 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def unknown_page(e):
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2020-01-21 20:26:49 +00:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
2020-05-07 00:39:12 +00:00
|
|
|
bg = '#000' if 'dark' in g.user_config and g.user_config['dark'] else '#fff'
|
2020-04-24 02:59:43 +00:00
|
|
|
return render_template('index.html', bg=bg, ua=g.user_request.modified_user_agent)
|
2020-01-21 20:26:49 +00:00
|
|
|
|
|
|
|
|
2020-04-25 00:45:57 +00:00
|
|
|
@app.route('/opensearch.xml', methods=['GET'])
|
|
|
|
def opensearch():
|
|
|
|
url_root = request.url_root
|
|
|
|
if url_root.endswith('/'):
|
|
|
|
url_root = url_root[:-1]
|
|
|
|
|
2020-05-06 00:28:43 +00:00
|
|
|
template = render_template('opensearch.xml', main_url=url_root)
|
2020-04-25 00:45:57 +00:00
|
|
|
response = make_response(template)
|
|
|
|
response.headers['Content-Type'] = 'application/xml'
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-04-29 00:19:34 +00:00
|
|
|
@app.route('/search', methods=['GET', 'POST'])
|
2020-01-21 20:26:49 +00:00
|
|
|
def search():
|
2020-05-06 00:28:43 +00:00
|
|
|
request_params = request.args if request.method == 'GET' else request.form
|
|
|
|
q = request_params.get('q')
|
|
|
|
|
|
|
|
if q is None or len(q) == 0:
|
|
|
|
return redirect('/')
|
|
|
|
else:
|
|
|
|
# Attempt to decrypt if this is an internal link
|
2020-04-29 00:19:34 +00:00
|
|
|
try:
|
|
|
|
q = Fernet(app.secret_key).decrypt(q.encode()).decode()
|
|
|
|
except InvalidToken:
|
|
|
|
pass
|
2020-04-05 22:15:46 +00:00
|
|
|
|
2020-02-21 23:52:29 +00:00
|
|
|
user_agent = request.headers.get('User-Agent')
|
2020-04-15 23:41:53 +00:00
|
|
|
mobile = 'Android' in user_agent or 'iPhone' in user_agent
|
2020-01-23 06:19:17 +00:00
|
|
|
|
2020-05-07 00:39:12 +00:00
|
|
|
content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
|
2020-05-06 00:28:43 +00:00
|
|
|
full_query = gen_query(q, request_params, content_filter.near)
|
2020-04-24 02:59:43 +00:00
|
|
|
get_body = g.user_request.send(query=full_query)
|
|
|
|
|
2020-05-05 00:00:43 +00:00
|
|
|
results = content_filter.reskin(get_body)
|
|
|
|
formatted_results = content_filter.clean(BeautifulSoup(results, 'html.parser'))
|
2020-01-23 06:19:17 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
return render_template('display.html', query=urlparse.unquote(q), response=formatted_results)
|
2020-01-21 20:26:49 +00:00
|
|
|
|
|
|
|
|
2020-04-15 23:41:53 +00:00
|
|
|
@app.route('/config', methods=['GET', 'POST'])
|
2020-04-05 23:59:50 +00:00
|
|
|
def config():
|
2020-04-15 23:41:53 +00:00
|
|
|
if request.method == 'GET':
|
2020-05-07 00:39:12 +00:00
|
|
|
return json.dumps(g.user_config)
|
2020-04-15 23:41:53 +00:00
|
|
|
else:
|
2020-04-29 02:50:12 +00:00
|
|
|
config_data = request.form.to_dict()
|
2020-04-15 23:41:53 +00:00
|
|
|
with open(app.config['STATIC_FOLDER'] + '/config.json', 'w') as config_file:
|
2020-04-29 02:50:12 +00:00
|
|
|
config_file.write(json.dumps(config_data, indent=4))
|
2020-04-15 23:41:53 +00:00
|
|
|
config_file.close()
|
2020-04-05 23:59:50 +00:00
|
|
|
|
2020-04-29 02:50:12 +00:00
|
|
|
return redirect('/')
|
2020-04-05 23:59:50 +00:00
|
|
|
|
|
|
|
|
2020-01-21 20:26:49 +00:00
|
|
|
@app.route('/url', methods=['GET'])
|
|
|
|
def url():
|
2020-01-23 06:19:17 +00:00
|
|
|
if 'url' in request.args:
|
|
|
|
return redirect(request.args.get('url'))
|
|
|
|
|
2020-01-21 20:26:49 +00:00
|
|
|
q = request.args.get('q')
|
|
|
|
if len(q) > 0 and 'http' in q:
|
|
|
|
return redirect(q)
|
|
|
|
else:
|
2020-04-07 20:12:16 +00:00
|
|
|
return render_template('error.html', query=q)
|
2020-01-21 20:26:49 +00:00
|
|
|
|
|
|
|
|
2020-01-23 06:19:17 +00:00
|
|
|
@app.route('/imgres')
|
|
|
|
def imgres():
|
|
|
|
return redirect(request.args.get('imgurl'))
|
|
|
|
|
|
|
|
|
2020-04-28 02:21:36 +00:00
|
|
|
@app.route('/tmp')
|
|
|
|
def tmp():
|
2020-04-29 00:19:34 +00:00
|
|
|
cipher_suite = Fernet(app.secret_key)
|
|
|
|
img_url = cipher_suite.decrypt(request.args.get('image_url').encode()).decode()
|
|
|
|
file_data = g.user_request.send(base_url=img_url, return_bytes=True)
|
2020-04-28 02:21:36 +00:00
|
|
|
tmp_mem = io.BytesIO()
|
|
|
|
tmp_mem.write(file_data)
|
|
|
|
tmp_mem.seek(0)
|
|
|
|
|
|
|
|
return send_file(
|
|
|
|
tmp_mem,
|
|
|
|
as_attachment=True,
|
|
|
|
attachment_filename='tmp.png',
|
|
|
|
mimetype='image/png'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-21 23:52:29 +00:00
|
|
|
@app.route('/window')
|
|
|
|
def window():
|
2020-04-24 02:59:43 +00:00
|
|
|
get_body = g.user_request.send(base_url=request.args.get('location'))
|
2020-02-21 23:52:29 +00:00
|
|
|
get_body = get_body.replace('src="/', 'src="' + request.args.get('location') + '"')
|
|
|
|
get_body = get_body.replace('href="/', 'href="' + request.args.get('location') + '"')
|
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
results = BeautifulSoup(get_body, 'html.parser')
|
2020-02-21 23:52:29 +00:00
|
|
|
|
|
|
|
try:
|
2020-04-24 02:59:43 +00:00
|
|
|
for script in results('script'):
|
2020-02-21 23:52:29 +00:00
|
|
|
script.decompose()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
return render_template('display.html', response=results)
|
2020-02-21 23:52:29 +00:00
|
|
|
|
|
|
|
|
2020-05-07 00:13:02 +00:00
|
|
|
def run_app():
|
|
|
|
parser = argparse.ArgumentParser(description='Whoogle Search console runner')
|
|
|
|
parser.add_argument('--port', default=8888, metavar='<port number>',
|
|
|
|
help='Specifies a port to run on (default 8888)')
|
|
|
|
parser.add_argument('--host', default='127.0.0.1', metavar='<ip address>',
|
|
|
|
help='Specifies the host address to use (default 127.0.0.1)')
|
|
|
|
parser.add_argument('--debug', default=False, action='store_true',
|
|
|
|
help='Activates debug mode for the Flask server (default False)')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
app.run(host=args.host, port=args.port, debug=args.debug)
|