From 4d5ce06eb67175fd5ed61db94fa721978238d530 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Sun, 20 Jun 2021 16:37:04 -0700 Subject: [PATCH] Configuration file (#15) * Add support for config file Closes #1 * Support changing config path The path to the config file is accepted as an argument * Remove old code Remove the code for the old backend selection method --- .gitignore | 1 + server/backends/x11.py | 3 +++ server/parse_config.py | 31 +++++++++++++++++++++++++++++++ server/wss.py | 22 +++++++++++++--------- 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 server/parse_config.py diff --git a/.gitignore b/.gitignore index 90b2f75..e071289 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ pieces newlist oldlist +conf.txt diff --git a/server/backends/x11.py b/server/backends/x11.py index 6ee279f..a99c410 100644 --- a/server/backends/x11.py +++ b/server/backends/x11.py @@ -5,12 +5,14 @@ import time def _long_touch(x, y): + os.environ['DISPLAY'] = config['display'] subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'mousedown', '1']) time.sleep(2) subprocess.run(['xdotool', 'mouseup', '1']) def get_img(): + os.environ['DISPLAY'] = config['display'] xwd = subprocess.Popen(['xwd', '-root', '-silent'], stdout=subprocess.PIPE) convert = subprocess.Popen(['convert', 'xwd:-', 'bmp:img.bmp'], stdin=xwd.stdout) convert.wait() @@ -18,6 +20,7 @@ def get_img(): def touch(x, y, is_long): + os.environ['DISPLAY'] = config['display'] if is_long: threading.Thread(target=_long_touch, args=(x, y,)).start() else: diff --git a/server/parse_config.py b/server/parse_config.py new file mode 100644 index 0000000..4b9e16f --- /dev/null +++ b/server/parse_config.py @@ -0,0 +1,31 @@ +import shlex + + +class ConfigSyntaxError(BaseException): + pass + + +def loadl(lines): + output = {} + + for line in [ i.strip() for i in lines ]: + words = shlex.split(line, comments=True) + + if len(words) == 0: + pass + elif len(words) == 2: + if words[0] in output: + raise ConfigSyntaxError('keys cannot be redefined') + output[words[0]] = words[1] + else: + raise ConfigSyntaxError('lines must consist of exactly two tokens') + + return output + + +def loads(string): + return loadl(string.split('\n')) + + +def load(f): + return loadl(f.readlines()) diff --git a/server/wss.py b/server/wss.py index a54fbce..c591ca1 100644 --- a/server/wss.py +++ b/server/wss.py @@ -1,10 +1,3 @@ -# Select backend - 'port8080' uses HamClock's port 8080 service; 'x11' uses an -# X11 server (typically Xvfb) (make sure DISPLAY is set correctly!) - -use_backend = 'x11' -# use_backend = 'port8080' - - import os import base64 import threading @@ -17,9 +10,20 @@ import imgproc as hcapi import argon2 import asyncio import importlib +import parse_config +import sys + +try: + conf = sys.argv[1] +except IndexError: + conf = 'conf.txt' +with open(conf) as f: + config_data = parse_config.load(f) -hcapi.backend = importlib.import_module(f'backends.{use_backend}') +hcapi.backend = importlib.import_module(f'backends.{config_data["backend"]}') +hcapi.backend.config = config_data +hcapi.config = config_data ph = argon2.PasswordHasher() @@ -91,7 +95,7 @@ class HCRAServer(tornado.websocket.WebSocketHandler): else: _, password, x, y, w, is_long = message.split(' ') try: - ph.verify('$argon2id$v=19$m=102400,t=2,p=8$NExqSUh+0wzBznBG9jM6ww$MkaPLZ6WPAegb8BI+IL7Bg', password) + ph.verify(config_data['password_argon2'], password) x, y, w, is_long = int(x), int(y), int(w), is_long == 'true' hcapi.touch(x, y, w, is_long) except argon2.exceptions.VerifyMismatchError: