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 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 _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 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_command(self, req: HTTPRequest, res: HTTPResponse): if not self.is_authorized(req, res): return data = req.body_json() cmd = Command.from_js(data["cmd"]) self._on_command(cmd, req, res)
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)