-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
79 lines (63 loc) · 3.03 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
from flask import Flask, request, redirect, Response
from flask.scaffold import setupmethod
from flask_sqlalchemy import SQLAlchemy
from time import perf_counter
db = SQLAlchemy()
app = Flask(__name__, )
# Load config from file secret.py
app.config.from_pyfile('secret.py')
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = (f"mysql://{app.config['DB_USER']}:{app.config['DB_PASSWORD']}"
f"@{app.config['DB_HOST']}/{app.config['DB_NAME']}")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
@setupmethod
def create_user():
db.create_all()
db.session.commit()
return app
@app.route('/', defaults={'path': ''})
@app.route("/<path:path>", methods=["GET", "POST", "DELETE"])
def proxy(path):
start = perf_counter()
try:
global SITE_NAME
if request.method == "GET":
resp = requests.get(f"{SITE_NAME}{path}", headers=request.headers, cookies=request.cookies,
allow_redirects=False)
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
headers = [(name, value) for (name, value) in resp.raw.headers.items() if
name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
elif request.method == "POST":
print(request.headers)
content_type = request.headers["Content-Type"]
resp = None
if content_type == "application/x-www-form-urlencoded":
# all data is in the url
resp = requests.post(f"{SITE_NAME}{path}", headers=request.headers, cookies=request.cookies,
allow_redirects=False)
elif content_type == "application/json":
resp = requests.post(f"{SITE_NAME}{path}", headers=request.headers, cookies=request.cookies,
json=request.get_json(), allow_redirects=False)
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
headers = [(name, value) for (name, value) in resp.raw.headers.items() if
name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
elif request.method == "DELETE":
resp = requests.delete(f"{SITE_NAME}{path}", allow_redirects=False)
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
headers = [(name, value) for (name, value) in resp.raw.headers.items() if
name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
except Exception as e:
print(e)
return "awd"
finally:
print(f"{perf_counter()-start=}")
if __name__ == "__main__":
app.run(debug=True, port=8000)