diff options
Diffstat (limited to 'session.py')
-rw-r--r-- | session.py | 23 |
1 files changed, 17 insertions, 6 deletions
@@ -13,7 +13,9 @@ from flask import Flask, abort, request from vnc import run_vnc, save_password API_SERVER = "http://127.0.0.1:8000" -REGISTER_URL = API_SERVER + "/app/rwa/api/register/" +BASE_URL = API_SERVER + "/app/rwa/api/" +REGISTER_URL = BASE_URL + "register/" +STOP_URL = BASE_URL + "stop/" class Session: @@ -38,6 +40,10 @@ class Session: def port(self) -> int: return self.ws_port + @property + def _api_headers(self) -> dict: + return {"Authorization": f"Token {self.api_token}"} + def _generate_password(self): """Generate password for x11vnc and save it.""" self.password = secrets.token_urlsafe(20) @@ -130,17 +136,17 @@ class Session: """Update status: Push status to Django.""" pass - def stop(self): + def stop(self, triggered: bool =False): + """Stop session and clean up.""" if self.mockup_session: filename = f"/tmp/rwa/{str(self.ws_port) + str(self.vnc_port) + str(self.ws_pid) + str(self.vnc_pid)}.lock" if os.path.isfile(filename): - os.remove(filename); + os.remove(filename) # Delete self del self return - """Stop session and clean up.""" # Kill VNC if self.vnc_pid in psutil.pids(): print("Kill VNC.") @@ -158,20 +164,25 @@ class Session: if hasattr(self, "trigger_thread"): print("Kill trigger service.") - self.trigger_thread.terminate() + self.trigger_thread.kill() self.push() + if not triggered: + requests.post( + STOP_URL, json={"id": self.session_id}, headers=self._api_headers + ) + # Delete self del self @property def vnc_process_running(self): + """Check if the VNC process is still running.""" if self.mockup_session: filename = f"/tmp/rwa/{str(self.ws_port) + str(self.vnc_port) + str(self.ws_pid) + str(self.vnc_pid)}.lock" return os.path.isfile(filename) - """Check if the VNC process is still running.""" if self.vnc_pid in psutil.pids(): p = psutil.Process(self.vnc_pid) if p.status() == "zombie": |