-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.py
170 lines (152 loc) · 3.85 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
from flask import Flask, request
from flask_cors import CORS
from werkzeug.routing import Rule
import handler
import handlers
from handlers import *
from metamodel import createLocalSession, closeLocalSession
app = Flask(__name__)
CORS(app, resources={r'/x/*': {'origins' : '*'}, r'/target/log': {'origins' : '*'}})
app.debug = True
app.secret_key = key = 'SECRET HERE'
app.config.update(
SESSION_COOKIE_NAME='__Host-session',
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax'
)
@app.teardown_request
def session_clear(exception=None):
if not hasattr(request, '_session'):
return
request._session.remove()
if exception and request._session.is_active:
request._session.rollback()
def reroute(noId, withId):
def sub(id=None, *args, **kwargs):
try:
if id == None:
return noId(*args, **kwargs)
else:
return withId(id, *args, **kwargs)
except:
import traceback
traceback.print_exc()
sub.func_name = '__reroute_' + noId.func_name
return sub
for module, sub in handler.all.items():
for name, (method, args, rpc, (noId, withId)) in sub.items():
if module == 'index':
route = '/'
trailing = True
else:
route = '/%s' % module
trailing = False
if name != 'index':
if not trailing:
route += '/'
route += '%s' % name
trailing = False
if noId != None and withId != None:
func = reroute(noId, withId)
elif noId != None:
func = noId
else:
func = withId
if withId != None:
iroute = route
if not trailing:
iroute += '/'
iroute += '<int:id>'
app.route(iroute, methods=[method])(func)
if noId != None:
app.route(route, methods=[method])(func)
@app.route('/favicon.ico')
def favicon():
return app.send_static_file('favicon.png')
@app.route('/css/<fn>')
@app.route('/fonts/<fn>')
@app.route('/img/<fn>')
@app.route('/js/<fn>')
@app.route('/js/<dir>/<fn>')
def staticfiles(fn, dir=None):
if '..' in fn or (dir is not None and '..' in dir):
return 'There is only one god and his name is Directory Traversal. And there is only one thing we say to Directory Traversal: "Not today."'
return app.send_static_file(request.path[1:])
rpcStubTemplate = '''%s: function(%s, callback) {
$.ajax(%r,
{
success: function(data) {
if(callback !== undefined)
callback(data)
},
error: function() {
if(callback !== undefined)
callback()
},
dataType: 'json',
data: {csrf: $csrf, %s},
type: 'POST'
}
)
}'''
cachedRpc = None
@app.route('/rpc.js')
def rpc():
global cachedRpc
if cachedRpc:
return cachedRpc
modules = []
for module, sub in handler.all.items():
module = [module]
for name, (method, args, rpc, funcs) in sub.items():
if not rpc:
continue
func = funcs[0] if funcs[0] else funcs[1]
name = name[4:]
method = rpcStubTemplate % (
name, ', '.join(args),
func.url(),
', '.join('%s: %s' % (arg, arg) for arg in args)
)
module.append(method)
if len(module) > 1:
modules.append(module)
cachedRpc = 'var $rpc = {%s};' % (', '.join('%s: {%s}' % (module[0], ', '.join(module[1:])) for module in modules))
return cachedRpc
@app.route('/scripts/<fn>')
def script(fn):
try:
if not fn.endswith('.js'):
return ''
fn = 'scripts/' + fn[:-3]
if os.path.exists(fn + '.js'):
return file(fn + '.js', 'rb').read()
return ''
except:
import traceback
traceback.print_exc()
app.url_map.add(Rule('/x/<link>', endpoint='hit'))
@app.endpoint('hit')
def hit(link):
if '.' in link:
link, ext = link.split('.', 1)
else:
ext = ''
req = '%s %s HTTP/1.1\r\n' % (request.method, request.url)
for k, v in request.headers:
req += '%s: %s\r\n' % (k, v)
req += '\r\n'
req += request.get_data()
createLocalSession()
try:
ret = handlers.target.hit(link, ext, req)
except:
closeLocalSession(True)
raise
else:
closeLocalSession(False)
return ret
if __name__=='__main__':
app.run(host='')