diff options
author | Jonathan Weth <git@jonathanweth.de> | 2020-07-21 17:39:59 +0200 |
---|---|---|
committer | Jonathan Weth <git@jonathanweth.de> | 2020-07-21 17:39:59 +0200 |
commit | 741d4498f76f743df42a7dfa7906700e9c754658 (patch) | |
tree | 01e1c16fbbaf6afa5e9014d6c19cf84f6b723839 /session.py | |
parent | 4eb1dc6b06867a1f1906fa32ab6448d0a00b9700 (diff) | |
download | RWA.Support.SessionService-741d4498f76f743df42a7dfa7906700e9c754658.tar.gz RWA.Support.SessionService-741d4498f76f743df42a7dfa7906700e9c754658.tar.bz2 RWA.Support.SessionService-741d4498f76f743df42a7dfa7906700e9c754658.zip |
Add trigger option and API methods for updating and getting session status
Diffstat (limited to 'session.py')
-rw-r--r-- | session.py | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -1,10 +1,13 @@ import os import secrets import signal +from multiprocessing import Process import psutil import requests +import port_for +from flask import Flask, abort, request from vnc import run_vnc, save_password API_SERVER = "http://127.0.0.1:8000" @@ -12,9 +15,16 @@ REGISTER_URL = API_SERVER + "/app/rwa/api/register/" class Session: + #: Session is running + STATUS_RUNNING = "running" + + #: Remote has joined the session + STATUS_JOINED = "joined" + def __init__(self): self._generate_password() self._start_vnc() + self._start_trigger_service() self._register_session() @property @@ -47,6 +57,8 @@ class Session: "port": self.ws_port, "password": self.password, "pid": self.vnc_pid, + "trigger_port": self.trigger_port, + "trigger_token": self.trigger_token, }, ) print(r) @@ -56,6 +68,39 @@ class Session: self.api_token = self.meta["token"] self.pin = self.meta["pin"] + def _start_trigger_service(self): + self.trigger_port = port_for.select_random() + self.trigger_token = secrets.token_urlsafe(20) + + app = Flask(__name__) + + @app.route("/", methods=["POST"]) + def trigger(): + json = request.json + if json.get("token", "") == self.trigger_token: + self._trigger() + return "Successful triggered" + else: + return abort(403) + + self.trigger_thread = Process( + target=lambda: app.run("0.0.0.0", port=self.trigger_port) + ) + self.trigger_thread.start() + + def _trigger(self): + """Event triggered by Django.""" + print("Triggered") + self.pull() + + def pull(self): + """Update status: Get status from Django.""" + pass + + def push(self): + """Update status: Push status to Django.""" + pass + def stop(self): """Stop session and clean up.""" # Kill VNC @@ -73,6 +118,12 @@ class Session: print("Delete password file") os.remove(self.pw_filename) + if hasattr(self, "trigger_thread"): + print("Kill trigger service.") + self.trigger_thread.terminate() + + self.push() + # Delete self del self @@ -89,3 +140,7 @@ class Session: @property def client_meta(self): return {"id": self.pid, "url": self.web_url, "pin": self.pin} + + @property + def status(self): + return {"id": self.pid, "status": self.STATUS_RUNNING} |