def on_put_file(self, req: HTTPRequest, res: HTTPResponse): c = self.get_client(req, res) id = str(uuid.uuid4()) path = "download/" + id with open(path, "wb") as f: f.write(req.data) self._clients.add_file(id, req.header("x-filename"), c.id) return res.ok(errors.OK, "OK", id) return res.unauthorized(errors.ERROR_HTTP, "", "Unknown error")
def on_connect(self, req: HTTPRequest, res: HTTPResponse): data = req.body_json() c = self._clients.connect(data) id = str(uuid.uuid4()) self._connected[id] = c c.status = Client.STATUS_CONNECTED res.header("x-session-id", id) self._clients.save(False) c.save() res.ok(errors.OK, "OK", None)
def get_client(self, req: HTTPRequest, res: HTTPResponse): if not req.header("x-session-id"): res.bad_request(errors.ERROR_HTTP, "Le header 'x-seesion-id' n'est pas transmis", []) return None id = req.header("x-session-id") if not id in self._connected: res.unauthorized(errors.BAD_SESSION, "Session invalide", []) return None return self._connected[id]
def on_get_file(self, req: HTTPRequest, res: HTTPResponse): id = req.params["id"] path = "download/" + id if not self._clients.has_file(id): return res.not_found(errors.FILE_NOT_FOUND, "Not found", None) res.header( "Content-Disposition", 'attachment; filename="' + self._clients.get_file_info(id)["filename"] + '"') res.serve_file(path) os.remove(path) self._clients.remove_file(id)
def on_result(self, req: HTTPRequest, res: HTTPResponse): c = self.get_client(req, res) if not c: return js = req.body_json() ret = c.result(js) if ret: res.ok(errors.OK, "OK", None) else: res.not_found(errors.ID_NOT_FOUND, "L'id de la réponse n'existe pas")
def admin_on_auth(self, req: HTTPRequest, res: HTTPResponse): if not req.header("x-user") or not req.header("x-password"): return res.bad_request(errors.ERROR_HTTP, "Identifiant non fourni", None) if not self._clients.auth(req.header("x-user"), req.header("x-password")): return res.unauthorized(errors.ERROR_HTTP, "Mot de passe ou login invalid", None) id = str(uuid.uuid4()) t = 100000 self._admins[id] = time.time() + t res.header("Set-Cookie", "session-id=" + id + "; Max-Age=" + str(t))
def _on_command(self, cmd, req: HTTPRequest, res: HTTPResponse): data = req.body_json() id = data["target"] sync = data["sync"] if not id in self._clients._clients: return res.not_found(errors.ID_NOT_FOUND, "Id " + id + " not found", None) c = self._clients._clients[id] c.send(cmd) if sync: r = c.find_response(cmd.id) while not r: time.sleep(0.01) r = c.find_response(cmd.id) res.ok(errors.OK, "OK", r) else: res.ok(errors.OK, "OK", CommandReturn(errors.OK, "").json())
def on_get_result(self, req: HTTPRequest, res: HTTPResponse): if not self.is_authorized(req, res): return cid = req.params["clientid"] cmdid = req.params["cmdid"] if not self._clients.has(cid): return res.not_found(errors.ID_NOT_FOUND, "Client not found", None) client = self._clients[cid] if not client.has_command(cmdid): return res.not_found(errors.ID_NOT_FOUND, "Command not found", None) r = client.find_response(cmdid) while not r: time.sleep(0.01) client.find_response(cmdid) res.ok(errors.OK, "OK", r)
def admin_moustache(self, req: HTTPRequest, res: HTTPResponse): needAuth = True for k in ALLOWED: if req.path.startswith(k): needAuth = False break if needAuth: if not self.is_authorized(req, res): return path = os.path.abspath("www/" + req.path[7:]) data = self.get_moustache_data() if req.path in MOUSTACHE_CLIENT_DATA: post = req.body_json() if not "id" in post: return res.bad_request(errors.ERROR_HTTP, "Le champs id n'est pas fourni (post)", None) if not post["id"] in self._clients._clients: return res.bad_request( errors.ID_NOT_FOUND, "L'id " + str(post["id"]) + " est incorrecte", None) data = self._clients._clients[post["id"]].get_moustache_data() res.end(html_template(path, data))
def admin_on_disconnect(self, req: HTTPRequest, res: HTTPResponse): res.header( "Set-Cookie", "token = deleted; path = /; expires = Thu, 01 Jan 1970 00: 00:00 GMT" ) if not "session-id" in req.cookies: res.temporary_redirect("/admin/login.html") return False id = req.cookies["session-id"] if not id in self._admins: res.temporary_redirect("/admin/login.html") return False del self._admins[id]
def test(req, res: HTTPResponse): res.end("Default")
def admin_on_server_command(self, req: HTTPRequest, res: HTTPResponse): if not self.is_authorized(req, res): return data = req.body_json() cmd = Command.from_text(data["cmd"]) out = cmd.start(self).json() res.ok(errors.OK, "OK", out)
def on_wait(self, req: HTTPRequest, res: HTTPResponse): c = self.get_client(req, res) if not c: return cmd = c.wait_fo_command() c.status = Client.STATUS_WAITING res.ok(errors.OK, "OK", cmd.json())