From f1f67a1c80fe9fb7439b72de754cc1993a30bdd7 Mon Sep 17 00:00:00 2001 From: Lobod Date: Mon, 17 Jun 2024 18:09:47 +0200 Subject: [PATCH 1/4] spoolman: add support for aditional headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Damian Łoboda --- moonraker/components/spoolman.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/moonraker/components/spoolman.py b/moonraker/components/spoolman.py index d8a3e1d0b..0818ae859 100644 --- a/moonraker/components/spoolman.py +++ b/moonraker/components/spoolman.py @@ -1,6 +1,7 @@ # Integration with Spoolman # # Copyright (C) 2023 Daniel Hultgren +# Copyright (C) 2024 Damian Łoboda # # This file may be distributed under the terms of the GNU GPLv3 license. @@ -10,6 +11,7 @@ import re import contextlib import tornado.websocket as tornado_ws +from tornado import httpclient as tornado_httpclient from ..common import RequestType, HistoryFieldData from ..utils import json_wrapper as jsonw from typing import ( @@ -38,6 +40,7 @@ class SpoolManager: def __init__(self, config: ConfigHelper): self.server = config.get_server() + self.additional_headers = config.getdict("additional_headers", default={}) self.eventloop = self.server.get_event_loop() self._get_spoolman_urls(config) self.sync_rate_seconds = config.getint("sync_rate", default=5, minval=1) @@ -129,9 +132,13 @@ async def _connect_websocket(self) -> None: logging.info(f"Connecting To Spoolman: {self.ws_url}") log_connect = False try: + ws_request = tornado_httpclient.HTTPRequest( + self.ws_url, + headers=self.additional_headers, + connect_timeout=5., + ) self.spoolman_ws = await tornado_ws.websocket_connect( - self.ws_url, - connect_timeout=5., + ws_request, ping_interval=20., ping_timeout=60. ) @@ -216,7 +223,8 @@ async def _check_spool_deleted(self) -> None: if self.spool_id is not None: response = await self.http_client.get( f"{self.spoolman_url}/v1/spool/{self.spool_id}", - connect_timeout=1., request_timeout=2. + connect_timeout=1., request_timeout=2., + headers=self.additional_headers, ) if response.status_code == 404: logging.info(f"Spool ID {self.spool_id} not found, setting to None") @@ -305,7 +313,8 @@ async def report_extrusion(self, eventtime: float) -> float: response = await self.http_client.request( method="PUT", url=f"{self.spoolman_url}/v1/spool/{spool_id}/use", - body={"use_length": used_length} + body={"use_length": used_length}, + headers=self.additional_headers, ) if response.has_error(): if response.status_code == 404: @@ -367,6 +376,7 @@ async def _proxy_spoolman_request(self, web_request: WebRequest): method=method, url=full_url, body=body, + headers=self.additional_headers, ) if not use_v2_response: response.raise_for_status() From 54b2e3e84254371f19f07c5d913dad6c226b15fb Mon Sep 17 00:00:00 2001 From: Lobod Date: Mon, 17 Jun 2024 18:14:33 +0200 Subject: [PATCH 2/4] docs: add documentation for spoolmans additional headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Damian Łoboda --- docs/configuration.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 22a8c6a27..1365b9ac0 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -3141,6 +3141,9 @@ server: http://192.168.0.123:7912 sync_rate: 5 # The interval, in seconds, between sync requests with the # Spoolman server. The default is 5. +additional_headers: +# Additional headers for spoolman connection. Usefull for connecting to for example CloudFlare Zero Trust. +# Dict "name=value" list. The default is empty. ``` #### Setting the active spool from Klipper From 20b1dd2618fc4605539bf1f9a7a47540ac7ac7fe Mon Sep 17 00:00:00 2001 From: Lobod Date: Mon, 17 Jun 2024 20:37:38 +0200 Subject: [PATCH 3/4] spoolman: add support for aditional headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Damian Łoboda --- moonraker/components/spoolman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonraker/components/spoolman.py b/moonraker/components/spoolman.py index 0818ae859..ae433b9ce 100644 --- a/moonraker/components/spoolman.py +++ b/moonraker/components/spoolman.py @@ -40,7 +40,7 @@ class SpoolManager: def __init__(self, config: ConfigHelper): self.server = config.get_server() - self.additional_headers = config.getdict("additional_headers", default={}) + self.additional_headers: Dict[str, str] = config.getdict("additional_headers", default={}) self.eventloop = self.server.get_event_loop() self._get_spoolman_urls(config) self.sync_rate_seconds = config.getint("sync_rate", default=5, minval=1) From b6e25201ceee3256ffe0de693bfdafb41e72442f Mon Sep 17 00:00:00 2001 From: Lobod Date: Mon, 17 Jun 2024 20:41:49 +0200 Subject: [PATCH 4/4] spoolman: add support for aditional headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Damian Łoboda --- moonraker/components/spoolman.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/moonraker/components/spoolman.py b/moonraker/components/spoolman.py index ae433b9ce..c1178561d 100644 --- a/moonraker/components/spoolman.py +++ b/moonraker/components/spoolman.py @@ -40,7 +40,9 @@ class SpoolManager: def __init__(self, config: ConfigHelper): self.server = config.get_server() - self.additional_headers: Dict[str, str] = config.getdict("additional_headers", default={}) + self.additional_headers: Dict[str, str] = config.getdict( + "additional_headers", default={} + ) self.eventloop = self.server.get_event_loop() self._get_spoolman_urls(config) self.sync_rate_seconds = config.getint("sync_rate", default=5, minval=1) @@ -133,9 +135,9 @@ async def _connect_websocket(self) -> None: log_connect = False try: ws_request = tornado_httpclient.HTTPRequest( - self.ws_url, - headers=self.additional_headers, - connect_timeout=5., + self.ws_url, + headers=self.additional_headers, + connect_timeout=5., ) self.spoolman_ws = await tornado_ws.websocket_connect( ws_request, @@ -376,7 +378,7 @@ async def _proxy_spoolman_request(self, web_request: WebRequest): method=method, url=full_url, body=body, - headers=self.additional_headers, + headers=self.additional_headers, ) if not use_v2_response: response.raise_for_status()