-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab.py
267 lines (226 loc) · 7.21 KB
/
lab.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/python3
"""
Sandbox
"""
import os
import json
import random
from doc import protect
from py3 import run
from web import instanciate_core, websocket
from tools import answer, read_cookie, write_cookie, tobytes, readfile
print("lab.py reloaded ...")
os.chdir(os.getcwd())
www_dir = "./www"
core = instanciate_core(reset=True)
def echo(par, pre=True):
"""
Annotation pour afficher en console le contenu d'un paramètre
@param par nom du paramètre à afficher
@param pre si le message doit être afficher en début
"""
def spam2(f):
def ff_spam(*args, **argv):
headers = args[0]
params = args[1]
if not pre:
return f(*args, **argv)
v = None
if par in params.keys():
v = params[par]
print("@echo", par, "=", v)
if pre:
return f(*args, **argv)
protect(ff_spam, f)
return ff_spam
return spam2
def rejet(par):
"""
Annotation pour rejeter lorsque le paramètre requis n'est pas présent
@use answer
@param par nom du paramètre à contrôler
"""
# FIXME contrôler
def spam2(f):
def ff_spam(headers, params):
return answer(code=403)
return f(headers, params)
docannot = "@Rejet %s" % (par)
protect(ff_spam, f, docannot)
return ff_spam
return spam2
def wsecho(ctx, data):
"""
Gestion des messages reçus par une websocket
@param ctx contexte
@param data message
"""
send = ctx["send"]
if type(data) == bool:
if data:
send("Bienvenue sur " + ctx["salon"])
else:
print("Aurevoir")
else:
send(ctx["salon"] + ">+ " + data)
@core.http("GET|POST", "/test/unique/{id}")
@echo("pfile")
def action_test_unique(
headers,
params,
ptexte: "text [a-zA-Z]{10}",
htexte: "header",
pnum: "number .{10}"=1,
pfile: "file"=None):
"""
Fonction de test
@param headers entetes
@param params disctionnaire des paramètres
@param ptexte test
@param htexte test
@param pnum test
@param pfile test
"""
return answer(200, body="action_test_unique", mime="text/html")
@core.http("GET", "/auth.do")
@echo("Cookie")
# @echo("@ip@")
def action1_auth(entetes, lus) -> 403:
"""
@param entetes = http.client.HTTPMessage
@param lus paramètres dans l'url ou dans le formule post
"""
# print("param_text", lus["param_text"])
chaine = random.getrandbits(90)
id_session = str(chaine) + "@@" # + str(lus["@ip@"])
hs = write_cookie("session", id_session, seconds=30)
return answer(code=203, headers=hs, body=read_cookie(entetes))
@core.http("GET", "/verif.do")
@echo("Cookie")
@rejet("ADMIN")
@echo("@ip@")
# FIXME read_cookie
def action1_verif(entetes, lus):
"""
@param entetes headers
@param lus paramètres dans l'url ou dans le formule post
"""
cookie = read_cookie(entetes)
cook = ""
if cookie is not None:
print(cookie["name"], "==", cookie["value"])
cook = cookie["value"]
hs = {}
return answer(code=203, headers=hs, body=cook)
@core.http("POST", "/poste")
@echo("path1")
@echo("trois")
def action1_global(entetes: "entetes p", lus: "lus p") -> "ne retourne rien":
"""
@param entetes headers
@param lus paramètres dans l'url ou dans le formule post
"""
print("actions1.lus", lus)
# print("actions1.entetes", entetes)
return answer(code=203, headers={"ent1": "val1"}, body="corps reponse")
@core.http(".*", "/{path1}\.{ext}")
def action2_fichier(entetes, lus):
"""
@param entetes headers
@param lus paramètres dans l'url ou dans le formule post
"""
mimes = {
"html": "text/html", "md": "text/plain",
"css": "text/css", "js": "text/javascript"}
print("MON NOUVEAU", type(entetes), type(lus))
try:
if not lus["ext"] in mimes.keys():
raise Exception("forbidden")
hs = {"Content-Type": mimes[lus["ext"]]}
if "file" in lus.keys():
hs["Content-Disposition"] = (
"attachment; filename=\"%s\"" % lus["file"])
bincont = readfile(www_dir + "/" + lus["path1"] + "." + lus["ext"])
return answer(code=202, headers=hs, body=bincont)
except Exception as ex:
print("action2", ex)
return answer(
code=404, body=lus["path1"] + "." + lus["ext"],
mime=mimes["html"])
@core.http("GET", "/codem/{path1}\.{ext}")
def action2_fichier_codem_niv1(entetes, lus):
"""
@param entetes headers
@param lus paramètres dans l'url ou dans le formule post
"""
mimes = {
"html": "text/html", "css": "text/css",
"js": "text/javascript", "md": "text/plain"}
try:
if not lus["ext"] in mimes.keys():
raise Exception("forbidden")
hs = {"Content-Type": mimes[lus["ext"]]}
if "file" in lus.keys():
hs["Content-Disposition"] = (
"attachment; filename=\"%s\"" % lus["file"])
bincont = readfile(
www_dir + "/codem/" + lus["path1"] + "." + lus["ext"])
return answer(code=202, headers=hs, body=bincont)
except Exception as ex:
print("action2", ex)
return answer(
code=404, body=lus["path1"] + "." + lus["ext"],
mime=mimes["html"])
@core.http("GET", "/codem/{path2}/{path1}\.{ext}")
def action2_fichier_codem_niv2(entetes: "un"="zapata", lus: "deux"="vide"):
"""
@param entetes headers
@param lus paramètres dans l'url ou dans le formule post
"""
mimes = {
"html": "text/html", "css": "text/css",
"js": "text/javascript", "md": "text/plain"}
try:
if not lus["ext"] in mimes.keys():
raise Exception("forbidden")
hs = {"Content-Type": mimes[lus["ext"]]}
if "file" in lus.keys():
hs["Content-Disposition"] = (
"attachment; filename=\"%s\"" % lus["file"])
bincont = readfile(
www_dir + "/codem/" + lus["path2"] + "/" +
lus["path1"] + "." + lus["ext"])
return answer(code=202, headers=hs, body=bincont)
except Exception as ex:
print("action2", ex)
return answer(
code=404, body=lus["path1"] + "." + lus["ext"],
mime=mimes["html"])
@core.http(".*", "/ws/{chat}")
@websocket(wsecho)
def wshandler(headers, params, chat: "path"):
"""
@param headers headers
@param params paramètres dans l'url ou dans le formule post
@param chat nom du salon.
"""
print("chat", chat)
return {"salon": params["chat"]}
@core.http(".*", "/echo")
def echoparams(headers, params):
"""
@param headers headers
@param params paramètres dans l'url ou dans le formule post
"""
print({"h": str(headers), "p": params})
return answer(body={"h": str(headers._headers), "pip": params})
@core.http(".*", "/*")
def defaulthandler(headers, params):
"""
@param headers headers
@param params paramètres dans l'url ou dans le formule post
"""
return answer(code=509)
if __name__ == "__main__":
core.prepare_test(test=False, chemin_js="www/test.js")
run(secured=False, port=8080, core=core)