-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
86 lines (73 loc) · 2.93 KB
/
server.py
File metadata and controls
86 lines (73 loc) · 2.93 KB
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
80
81
82
83
84
85
86
import os
import flask
import requests
import json
import urllib
from verify import verifySignature, getAuth
from data import Request
app = flask.Flask(__name__)
sess = requests.Session()
@app.route("/", defaults={"path": ""}, methods=["GET", "POST", "DELETE"])
@app.route("/<path:path>", methods=["GET", "POST", "DELETE"])
def proxy(path):
try:
#print(flask.request.cookies)
auth = flask.request.cookies.get('auth')
userinfo = getAuth(auth)
except Exception as e:
#except ZeroDivisionError as e:
if flask.request.cookies.get('auth'):
print(e)
return flask.Response("<h1>Unauthorized</h1><p>Your client did not send any authorization data. Please obtain an authorization token and try again.</p>", status=401, mimetype='text/html')
try:
resp = {"status_code": 400}
url = os.environ["REPLIT_DB_URL"]
if flask.request.path != "/":
url += flask.request.path
proxreq = requests.Request(flask.request.method, url, data=flask.request.form, params=flask.request.args).prepare()
if flask.request.method == "GET":
resp = sess.send(proxreq)
req = Request(flask.request, resp.text, auth)
else:
resp = False
req = Request(flask.request, False, auth)
#print("RESP:", resp.text)
if (filteredResp := accessCheck(req)) is None:
return flask.Response("<h1>Forbidden</h1><p>Your client tried to access a resource but doesn't have the required permissions to view them.</p>", status=403, mimetype='text/html')
if flask.request.method != "GET":
resp = sess.send(proxreq)
#print("REPR RESP.TEXT:", repr(resp.text))
#print("RESP.TEXT", resp.text)
#print("FILTEREDRESPTEXT:", repr(urllib.parse.quote("\n".join(filteredResp))))
output = ""
if req._method == "LIST":
output = "\n".join([urllib.parse.quote(x) for x in filteredResp])
elif req._method == "GET":
output = json.dumps(filteredResp)
else:
output = ""
#print("REPR OUTPUT:", repr(output))
#
#print("OUTPUT:", output)
#
#print("RESP:", resp.status_code)
#
#print("CONDITION:", resp.text == output)
except Exception as e:
if resp.status_code == 400:
return flask.Response("<h1>Bad or malformed request</h1><p>Your client tried to tell us something, but we couldn't quite understand what they were trying to say.</p>", status=400, mimetype='text/html')
else:
return flask.Response("<h1>Database Error</h1>Your client tried to access a resource, but the database server couldn't complete that request.</p>", status=resp.status_code, mimetype='text/html')
print(e, resp.status_code)
proxy_resp = flask.make_response(output)
proxy_resp.status_code = resp.status_code
for k, v in resp.headers.items():
if k.lower() != "content-length":
proxy_resp.headers[k] = v
else:
proxy_resp.headers[k] = len(output)
return proxy_resp
def start(acl):
global accessCheck
accessCheck = acl
return app.run