2020-04-24 05:59:43 +03:00
|
|
|
from app import app
|
2020-04-16 02:41:53 +03:00
|
|
|
from app.filter import Filter
|
2020-04-24 05:59:43 +03:00
|
|
|
from app.request import Request, gen_query
|
2020-01-23 09:19:17 +03:00
|
|
|
from bs4 import BeautifulSoup
|
2020-04-29 03:19:34 +03:00
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
2020-04-28 05:21:36 +03:00
|
|
|
from flask import g, make_response, request, redirect, render_template, send_file
|
|
|
|
import io
|
2020-04-05 04:30:53 +03:00
|
|
|
import json
|
2020-01-21 23:26:49 +03:00
|
|
|
import os
|
2020-02-22 02:52:29 +03:00
|
|
|
import urllib.parse as urlparse
|
2020-01-21 23:26:49 +03:00
|
|
|
|
2020-04-16 02:41:53 +03: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-06 02:59:50 +03:00
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
user_config = json.load(open(app.config['STATIC_FOLDER'] + '/config.json'))
|
2020-04-05 04:30:53 +03:00
|
|
|
|
2020-02-22 02:52:29 +03:00
|
|
|
|
2020-04-24 05:59:43 +03:00
|
|
|
@app.before_request
|
|
|
|
def before_request_func():
|
|
|
|
g.user_request = Request(request.headers.get('User-Agent'))
|
2020-01-22 08:51:02 +03:00
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
|
2020-05-06 03:28:43 +03:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def unknown_page(e):
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
2020-04-08 21:47:21 +03:00
|
|
|
bg = '#000' if 'dark' in user_config and user_config['dark'] else '#fff'
|
2020-04-24 05:59:43 +03:00
|
|
|
return render_template('index.html', bg=bg, ua=g.user_request.modified_user_agent)
|
2020-01-21 23:26:49 +03:00
|
|
|
|
|
|
|
|
2020-04-25 03:45:57 +03: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 03:28:43 +03:00
|
|
|
template = render_template('opensearch.xml', main_url=url_root)
|
2020-04-25 03:45:57 +03:00
|
|
|
response = make_response(template)
|
|
|
|
response.headers['Content-Type'] = 'application/xml'
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-04-29 03:19:34 +03:00
|
|
|
@app.route('/search', methods=['GET', 'POST'])
|
2020-01-21 23:26:49 +03:00
|
|
|
def search():
|
2020-05-06 03:28:43 +03: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 03:19:34 +03:00
|
|
|
try:
|
|
|
|
q = Fernet(app.secret_key).decrypt(q.encode()).decode()
|
|
|
|
except InvalidToken:
|
|
|
|
pass
|
2020-04-06 01:15:46 +03:00
|
|
|
|
2020-02-22 02:52:29 +03:00
|
|
|
user_agent = request.headers.get('User-Agent')
|
2020-04-16 02:41:53 +03:00
|
|
|
mobile = 'Android' in user_agent or 'iPhone' in user_agent
|
2020-01-23 09:19:17 +03:00
|
|
|
|
2020-04-29 03:19:34 +03:00
|
|
|
content_filter = Filter(mobile, user_config, secret_key=app.secret_key)
|
2020-05-06 03:28:43 +03:00
|
|
|
full_query = gen_query(q, request_params, content_filter.near)
|
2020-04-24 05:59:43 +03:00
|
|
|
get_body = g.user_request.send(query=full_query)
|
|
|
|
|
2020-05-05 03:00:43 +03:00
|
|
|
results = content_filter.reskin(get_body)
|
|
|
|
formatted_results = content_filter.clean(BeautifulSoup(results, 'html.parser'))
|
2020-01-23 09:19:17 +03:00
|
|
|
|
2020-04-24 05:59:43 +03:00
|
|
|
return render_template('display.html', query=urlparse.unquote(q), response=formatted_results)
|
2020-01-21 23:26:49 +03:00
|
|
|
|
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
@app.route('/config', methods=['GET', 'POST'])
|
2020-04-06 02:59:50 +03:00
|
|
|
def config():
|
|
|
|
global user_config
|
2020-04-16 02:41:53 +03:00
|
|
|
if request.method == 'GET':
|
|
|
|
return json.dumps(user_config)
|
|
|
|
else:
|
2020-04-29 05:50:12 +03:00
|
|
|
config_data = request.form.to_dict()
|
2020-04-16 02:41:53 +03:00
|
|
|
with open(app.config['STATIC_FOLDER'] + '/config.json', 'w') as config_file:
|
2020-04-29 05:50:12 +03:00
|
|
|
config_file.write(json.dumps(config_data, indent=4))
|
2020-04-16 02:41:53 +03:00
|
|
|
config_file.close()
|
2020-04-06 02:59:50 +03:00
|
|
|
|
2020-04-29 05:50:12 +03:00
|
|
|
user_config = config_data
|
2020-04-06 02:59:50 +03:00
|
|
|
|
2020-04-29 05:50:12 +03:00
|
|
|
return redirect('/')
|
2020-04-06 02:59:50 +03:00
|
|
|
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
@app.route('/url', methods=['GET'])
|
|
|
|
def url():
|
2020-01-23 09:19:17 +03:00
|
|
|
if 'url' in request.args:
|
|
|
|
return redirect(request.args.get('url'))
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
q = request.args.get('q')
|
|
|
|
if len(q) > 0 and 'http' in q:
|
|
|
|
return redirect(q)
|
|
|
|
else:
|
2020-04-07 23:12:16 +03:00
|
|
|
return render_template('error.html', query=q)
|
2020-01-21 23:26:49 +03:00
|
|
|
|
|
|
|
|
2020-01-23 09:19:17 +03:00
|
|
|
@app.route('/imgres')
|
|
|
|
def imgres():
|
|
|
|
return redirect(request.args.get('imgurl'))
|
|
|
|
|
|
|
|
|
2020-04-28 05:21:36 +03:00
|
|
|
@app.route('/tmp')
|
|
|
|
def tmp():
|
2020-04-29 03:19:34 +03: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 05:21:36 +03: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-22 02:52:29 +03:00
|
|
|
@app.route('/window')
|
|
|
|
def window():
|
2020-04-24 05:59:43 +03:00
|
|
|
get_body = g.user_request.send(base_url=request.args.get('location'))
|
2020-02-22 02:52:29 +03: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 05:59:43 +03:00
|
|
|
results = BeautifulSoup(get_body, 'html.parser')
|
2020-02-22 02:52:29 +03:00
|
|
|
|
|
|
|
try:
|
2020-04-24 05:59:43 +03:00
|
|
|
for script in results('script'):
|
2020-02-22 02:52:29 +03:00
|
|
|
script.decompose()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2020-04-24 05:59:43 +03:00
|
|
|
return render_template('display.html', response=results)
|
2020-02-22 02:52:29 +03:00
|
|
|
|
|
|
|
|
2020-01-21 23:26:49 +03:00
|
|
|
if __name__ == '__main__':
|
2020-04-11 23:37:15 +03:00
|
|
|
app.run(debug=True)
|