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 | |
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
-rw-r--r-- | API.rst | 28 | ||||
-rw-r--r-- | poetry.lock | 126 | ||||
-rw-r--r-- | pyproject.toml | 1 | ||||
-rw-r--r-- | service.py | 49 | ||||
-rw-r--r-- | session.py | 55 |
5 files changed, 241 insertions, 18 deletions
@@ -26,6 +26,34 @@ Start a new remote session and register it in RWA {"id": <pid>, "url": "<url>", "pin": <pin>} +status +^^^^^^ +Return the status of a session + +.. note:: + + This uses the last status version got by the update service in the background. + +**Arguments:** pid/id (integer) + +**Return type:** string (JSON) + + +**Structure of returned JSON:** + +:: + + {"id": <pid>, "status": <status>} + +**Possible status options:** + +- ``running``: The session is running and ready for connecting. +- ``joined``: The session is running and a the remote connected to the session. + +refresh_status +^^^^^^^^^^^^^^ +Same as ``status``, but updates status from Django before returning it here. + stop ^^^^ Stop a remote session diff --git a/poetry.lock b/poetry.lock index 0e68b69..1759d95 100644 --- a/poetry.lock +++ b/poetry.lock @@ -16,6 +16,14 @@ version = "3.0.4" [[package]] category = "main" +description = "Composable command line interface toolkit" +name = "click" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "7.1.2" + +[[package]] +category = "main" description = "Python bindings for libdbus" name = "dbus-python" optional = false @@ -24,6 +32,25 @@ version = "1.2.16" [[package]] category = "main" +description = "A simple framework for building complex web applications." +name = "flask" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "1.1.2" + +[package.dependencies] +Jinja2 = ">=2.10.1" +Werkzeug = ">=0.15" +click = ">=5.1" +itsdangerous = ">=0.24" + +[package.extras] +dev = ["pytest", "coverage", "tox", "sphinx", "pallets-sphinx-themes", "sphinxcontrib-log-cabinet", "sphinx-issues"] +docs = ["sphinx", "pallets-sphinx-themes", "sphinxcontrib-log-cabinet", "sphinx-issues"] +dotenv = ["python-dotenv"] + +[[package]] +category = "main" description = "Internationalized Domain Names in Applications (IDNA)" name = "idna" optional = false @@ -32,6 +59,36 @@ version = "2.10" [[package]] category = "main" +description = "Various helpers to pass data to untrusted environments and back." +name = "itsdangerous" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "1.1.0" + +[[package]] +category = "main" +description = "A very fast and expressive template engine." +name = "jinja2" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "2.11.2" + +[package.dependencies] +MarkupSafe = ">=0.23" + +[package.extras] +i18n = ["Babel (>=0.8)"] + +[[package]] +category = "main" +description = "Safely add untrusted strings to HTML/XML markup." +name = "markupsafe" +optional = false +python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*" +version = "1.1.1" + +[[package]] +category = "main" description = "NumPy is the fundamental package for array computing with Python." name = "numpy" optional = false @@ -118,8 +175,20 @@ version = "0.9.0" [package.dependencies] numpy = "*" +[[package]] +category = "main" +description = "The comprehensive WSGI web application library." +name = "werkzeug" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "1.0.1" + +[package.extras] +dev = ["pytest", "pytest-timeout", "coverage", "tox", "sphinx", "pallets-sphinx-themes", "sphinx-issues"] +watchdog = ["watchdog"] + [metadata] -content-hash = "647d3ef2f88b9c37462804e88709259cfabccd8b431cb5d998100c86e611819a" +content-hash = "b0f18bcb0fae5ffa552ced8d8334169e085cecbc91cc84431c9f6af8635757d9" python-versions = "^3.5" [metadata.files] @@ -131,13 +200,64 @@ chardet = [ {file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"}, {file = "chardet-3.0.4.tar.gz", hash = "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"}, ] +click = [ + {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, + {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, +] dbus-python = [ {file = "dbus-python-1.2.16.tar.gz", hash = "sha256:11238f1d86c995d8aed2e22f04a1e3779f0d70e587caffeab4857f3c662ed5a4"}, ] +flask = [ + {file = "Flask-1.1.2-py2.py3-none-any.whl", hash = "sha256:8a4fdd8936eba2512e9c85df320a37e694c93945b33ef33c89946a340a238557"}, + {file = "Flask-1.1.2.tar.gz", hash = "sha256:4efa1ae2d7c9865af48986de8aeb8504bf32c7f3d6fdc9353d34b21f4b127060"}, +] idna = [ {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, {file = "idna-2.10.tar.gz", hash = "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6"}, ] +itsdangerous = [ + {file = "itsdangerous-1.1.0-py2.py3-none-any.whl", hash = "sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749"}, + {file = "itsdangerous-1.1.0.tar.gz", hash = "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19"}, +] +jinja2 = [ + {file = "Jinja2-2.11.2-py2.py3-none-any.whl", hash = "sha256:f0a4641d3cf955324a89c04f3d94663aa4d638abe8f733ecd3582848e1c37035"}, + {file = "Jinja2-2.11.2.tar.gz", hash = "sha256:89aab215427ef59c34ad58735269eb58b1a5808103067f7bb9d5836c651b3bb0"}, +] +markupsafe = [ + {file = "MarkupSafe-1.1.1-cp27-cp27m-macosx_10_6_intel.whl", hash = "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161"}, + {file = "MarkupSafe-1.1.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7"}, + {file = "MarkupSafe-1.1.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183"}, + {file = "MarkupSafe-1.1.1-cp27-cp27m-win32.whl", hash = "sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b"}, + {file = "MarkupSafe-1.1.1-cp27-cp27m-win_amd64.whl", hash = "sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e"}, + {file = "MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f"}, + {file = "MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1"}, + {file = "MarkupSafe-1.1.1-cp34-cp34m-macosx_10_6_intel.whl", hash = "sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5"}, + {file = "MarkupSafe-1.1.1-cp34-cp34m-manylinux1_i686.whl", hash = "sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1"}, + {file = "MarkupSafe-1.1.1-cp34-cp34m-manylinux1_x86_64.whl", hash = "sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735"}, + {file = "MarkupSafe-1.1.1-cp34-cp34m-win32.whl", hash = "sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21"}, + {file = "MarkupSafe-1.1.1-cp34-cp34m-win_amd64.whl", hash = "sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235"}, + {file = "MarkupSafe-1.1.1-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b"}, + {file = "MarkupSafe-1.1.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f"}, + {file = "MarkupSafe-1.1.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905"}, + {file = "MarkupSafe-1.1.1-cp35-cp35m-win32.whl", hash = "sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1"}, + {file = "MarkupSafe-1.1.1-cp35-cp35m-win_amd64.whl", hash = "sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d"}, + {file = "MarkupSafe-1.1.1-cp36-cp36m-macosx_10_6_intel.whl", hash = "sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff"}, + {file = "MarkupSafe-1.1.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473"}, + {file = "MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e"}, + {file = "MarkupSafe-1.1.1-cp36-cp36m-win32.whl", hash = "sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66"}, + {file = "MarkupSafe-1.1.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5"}, + {file = "MarkupSafe-1.1.1-cp37-cp37m-macosx_10_6_intel.whl", hash = "sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d"}, + {file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e"}, + {file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6"}, + {file = "MarkupSafe-1.1.1-cp37-cp37m-win32.whl", hash = "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2"}, + {file = "MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-win32.whl", hash = "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be"}, + {file = "MarkupSafe-1.1.1.tar.gz", hash = "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b"}, +] numpy = [ {file = "numpy-1.18.5-cp35-cp35m-macosx_10_9_intel.whl", hash = "sha256:e91d31b34fc7c2c8f756b4e902f901f856ae53a93399368d9a0dc7be17ed2ca0"}, {file = "numpy-1.18.5-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:7d42ab8cedd175b5ebcb39b5208b25ba104842489ed59fbb29356f671ac93583"}, @@ -195,3 +315,7 @@ urllib3 = [ websockify = [ {file = "websockify-0.9.0.tar.gz", hash = "sha256:c35b5b79ebc517d3b784dacfb993be413a93cda5222c6f382443ce29c1a6cada"}, ] +werkzeug = [ + {file = "Werkzeug-1.0.1-py2.py3-none-any.whl", hash = "sha256:2de2a5db0baeae7b2d2664949077c2ac63fbd16d98da0ff71837f7d1dea3fd43"}, + {file = "Werkzeug-1.0.1.tar.gz", hash = "sha256:6c80b1e5ad3665290ea39320b91e1be1e0d5f60652b964a3070216de83d2e47c"}, +] diff --git a/pyproject.toml b/pyproject.toml index acdfc6b..7f2b787 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ port_for = "^0.4" requests = "^2.24.0" websockify = "^0.9.0" psutil = "^5.7.2" +flask = "^1.1.2" [tool.poetry.dev-dependencies] @@ -31,38 +31,53 @@ class RWAService(dbus.service.Object): return json.dumps(session.client_meta) + @dbus.service.method("de.rwa.rwa", in_signature="i", out_signature="s") + def status(self, pid: int) -> str: + """Get status information about a service.""" + return self._get_status(pid) + + @dbus.service.method("de.rwa.rwa", in_signature="i", out_signature="s") + def refresh_status(self, pid: int) -> str: + """Get status information about a service and refresh status before.""" + self._update_session(pid) + return self._get_status(pid) + @dbus.service.method("de.rwa.rwa", in_signature="i") def stop(self, pid: int): """Stop a remote session.""" session = self.sessions[pid] session.stop() + def _get_status(self, pid: int) -> str: + session = self.sessions[pid] + return json.dumps(session.status) + def _ensure_update_service(self): """Start session update thread if it isn't already running.""" if not self.update_service_running: self.update_thread = Thread(target=self._update_sessions) self.update_thread.start() - def _update_sessions(self): - """Go through all running sessions and update their status. + def _update_session(self, pid: int): + """Update the status of a session.""" + session = self.sessions[pid] + print(f"Session #{session.pid}") - Things that this function will do: - - Check if VNC is still running - - Kill websockify if VNC process is dead - """ - while len(self.sessions.values()) > 0: - for session in list(self.sessions.values()): - print(f"Session #{session.pid}") + # Check if VNC process is still running + running = session.vnc_process_running + if running: + print("Session is running") + else: + print("Session is dead.") - # Check if VNC process is still running - running = session.vnc_process_running - if running: - print("Session is running") - else: - print("Session is dead.") + session.stop() + del self.sessions[session.pid] - session.stop() - del self.sessions[session.pid] + def _update_sessions(self): + """Go through all running sessions and update their status using ``_update_session``.""" + while len(self.sessions.values()) > 0: + for session in list(self.sessions.values()): + self._update_session(session.pid) time.sleep(2) @@ -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} |