Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
@app.route('/proxy')
|
8 |
+
def proxy():
|
9 |
+
url = request.args.get('url')
|
10 |
+
if not url:
|
11 |
+
return jsonify({'error': 'URL parameter is missing'}), 400
|
12 |
+
|
13 |
+
try:
|
14 |
+
headers = {key: value for (key, value) in request.headers if key != 'Host'}
|
15 |
+
cookies = request.cookies
|
16 |
+
method = request.method
|
17 |
+
data = request.get_data()
|
18 |
+
|
19 |
+
response = requests.request(
|
20 |
+
method=method,
|
21 |
+
url=url,
|
22 |
+
headers=headers,
|
23 |
+
cookies=cookies,
|
24 |
+
data=data,
|
25 |
+
allow_redirects=False
|
26 |
+
)
|
27 |
+
|
28 |
+
if 'text/html' in response.headers.get('content-type', ''):
|
29 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
30 |
+
for tag in soup.find_all(href=True):
|
31 |
+
tag['href'] = '/proxy?url=' + tag['href']
|
32 |
+
for tag in soup.find_all(src=True):
|
33 |
+
tag['src'] = '/proxy?url=' + tag['src']
|
34 |
+
response.content = soup.encode()
|
35 |
+
|
36 |
+
return response.content, response.status_code, response.headers.items()
|
37 |
+
except requests.RequestException as e:
|
38 |
+
return jsonify({'error': str(e)}), 500
|
39 |
+
|
40 |
+
if __name__ == '__main__':
|
41 |
+
app.run(host="0.0.0.0", ssl_context='adhoc', port=7680) # Use 'adhoc' to enable HTTPS locally
|