-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
44 lines (40 loc) · 1.28 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import json
import urllib.request
from base64 import b64encode
from flask import Flask, request, jsonify
from urllib.parse import urlparse
import requests
import validators
app = Flask(__name__)
@app.get('/insecure/optimize')
def insecure_optimize_website():
url = request.json.get('url')
response = urllib.request.urlopen(url)
html = response.read()
domain_name = urlparse(url).netloc
data_dict = {
'raw_data': b64encode(html).decode(),
'domain': domain_name,
'url': url
}
return jsonify({"data": data_dict}), 200
@app.get('/secure/optimize')
def secure_optimize_website():
blocked_urls = ['localhost', '127.0.0.1', '169.254.169.254']
url = request.json.get('url')
verify_url = validators.url(url)
hostname = urlparse(url).hostname
if not verify_url or not hostname or hostname in blocked_urls:
return jsonify({"message": "Invalid URL"}), 400
response = requests.get(url)
html = response.content
domain_name = urlparse(url).netloc
data_dict = {
'raw_data': b64encode(html).decode(),
'domain': domain_name,
'url': url
}
return jsonify({"data": data_dict}), 200
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')