From 21a269edd2ff62e0efd8665e9336edc0966945a4 Mon Sep 17 00:00:00 2001 From: andrewmv Date: Fri, 13 Sep 2024 14:02:12 -0700 Subject: [PATCH] feat: Moonraker route prefix support (#1464) * Added config directives to specify moonraker route prefix, and explicitly specify SSL * Added newly-implemented options to documentation * Fixed boolean handling * Cleanup * Fixed trailing whitespace * refactor: simplifications and lint fixes --------- Co-authored-by: Alfredo Monclus --- docs/Configuration.md | 7 ++++++- ks_includes/KlippyRest.py | 7 +++++-- ks_includes/KlippyWebsocket.py | 8 +++++--- ks_includes/config.py | 6 ++++-- screen.py | 4 ++++ 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index a3a07a4d..b9b3576a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -46,8 +46,13 @@ Multiple printers can be defined [printer MyPrinter] # Define the moonraker host/port if different from 127.0.0.1 and 7125 moonraker_host: 127.0.0.1 -# ports 443 and 7130 will use https/wss moonraker_port: 7125 +# Use HTTPS/WSS. Defaults to True for ports 443 or 7130, False for any other port +# moonraker_ssl: False +# If you're using the route_prefix option in your moonraker config, specify it here. +# This can be useful for running multiple printers behind a path-based reverse proxy. +# Most installs will not need this. +# moonraker_path: printer1 # Moonraker API key if this host is not connecting from a trusted client IP # moonraker_api_key: False diff --git a/ks_includes/KlippyRest.py b/ks_includes/KlippyRest.py index edb1a824..2234f670 100644 --- a/ks_includes/KlippyRest.py +++ b/ks_includes/KlippyRest.py @@ -4,15 +4,18 @@ import requests class KlippyRest: - def __init__(self, ip, port=7125, api_key=False): + def __init__(self, ip, port=7125, api_key=False, path='', ssl=None): self.ip = ip self.port = port + self.path = f"/{path}" if path else '' + self.ssl = ssl self.api_key = api_key + self.ssl = int(self.port) in {443, 7130} if ssl is None else bool(ssl) self.status = '' @property def endpoint(self): - return f"{'https' if int(self.port) in {443, 7130} else 'http'}://{self.ip}:{self.port}" + return f"{'https' if self.ssl else 'http'}://{self.ip}:{self.port}{self.path}" @staticmethod def process_response(response): diff --git a/ks_includes/KlippyWebsocket.py b/ks_includes/KlippyWebsocket.py index db6d04d1..24748e76 100644 --- a/ks_includes/KlippyWebsocket.py +++ b/ks_includes/KlippyWebsocket.py @@ -20,7 +20,7 @@ class KlippyWebsocket(threading.Thread): reconnect_count = 0 max_retries = 4 - def __init__(self, callback, host, port, api_key): + def __init__(self, callback, host, port, api_key, path='', ssl=None): threading.Thread.__init__(self) self._wst = None self.ws_url = None @@ -30,16 +30,18 @@ class KlippyWebsocket(threading.Thread): self.closing = False self.host = host self.port = port + self.path = f"/{path}" if path else '' + self.ssl = int(self.port) in {443, 7130} if ssl is None else bool(ssl) self.header = {"x-api-key": api_key} if api_key else {} self.api_key = api_key @property def _url(self): - return f"{self.host}:{self.port}" + return f"{self.host}:{self.port}{self.path}" @property def ws_proto(self): - return "wss" if int(self.port) in {443, 7130} else "ws" + return "wss" if self.ssl else "ws" def initial_connect(self): if self.connect() is not False: diff --git a/ks_includes/config.py b/ks_includes/config.py index ce497169..ecd85548 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -98,6 +98,8 @@ class KlipperScreenConfig: {printer[8:]: { "moonraker_host": self.config.get(printer, "moonraker_host", fallback="127.0.0.1"), "moonraker_port": self.config.get(printer, "moonraker_port", fallback="7125"), + "moonraker_path": self.config.get(printer, "moonraker_path", fallback='').strip('/'), + "moonraker_ssl": self.config.getboolean(printer, "moonraker_ssl", fallback=None), "moonraker_api_key": self.config.get(printer, "moonraker_api_key", fallback="").replace('"', '') }} for printer in printers ] @@ -173,10 +175,10 @@ class KlipperScreenConfig: ) elif section.startswith('printer '): bools = ( - 'invert_x', 'invert_y', 'invert_z', + 'invert_x', 'invert_y', 'invert_z', 'moonraker_ssl', ) strs = ( - 'moonraker_api_key', 'moonraker_host', 'titlebar_name_type', + 'moonraker_api_key', 'moonraker_host', 'moonraker_path', 'titlebar_name_type', 'screw_positions', 'power_devices', 'titlebar_items', 'z_babystep_values', 'extrude_distances', 'extrude_speeds', 'move_distances', 'zcalibrate_custom_commands' ) diff --git a/screen.py b/screen.py index bb858999..6eb2cffb 100755 --- a/screen.py +++ b/screen.py @@ -235,6 +235,8 @@ class KlipperScreen(Gtk.Window): self.printers[ind][name]["moonraker_host"], self.printers[ind][name]["moonraker_port"], self.printers[ind][name]["moonraker_api_key"], + self.printers[ind][name]["moonraker_path"], + self.printers[ind][name]["moonraker_ssl"], ) self._ws = KlippyWebsocket( { @@ -246,6 +248,8 @@ class KlipperScreen(Gtk.Window): self.printers[ind][name]["moonraker_host"], self.printers[ind][name]["moonraker_port"], self.printers[ind][name]["moonraker_api_key"], + self.printers[ind][name]["moonraker_path"], + self.printers[ind][name]["moonraker_ssl"], ) if self.files is None: self.files = KlippyFiles(self)