Skip to content

Commit

Permalink
server: detect if ipv6 is available
Browse files Browse the repository at this point in the history
When ipv6 is not available make sure that http and websocket
clients set the "allow_ipv6" flag to false.

Signed-off-by:  Eric Callahan <[email protected]>
  • Loading branch information
Arksine committed Feb 12, 2025
1 parent 9ed0239 commit f74cdab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
7 changes: 5 additions & 2 deletions moonraker/components/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ async def request(
req_args: Dict[str, Any] = dict(
body=body,
request_timeout=request_timeout,
connect_timeout=connect_timeout
connect_timeout=connect_timeout,
allow_ipv6=self.server.ipv6_enabled()
)
if basic_auth_user is not None:
assert basic_auth_pass is not None
Expand Down Expand Up @@ -261,8 +262,10 @@ async def download_file(
url, headers={"Accept": content_type},
connect_timeout=connect_timeout,
request_timeout=request_timeout,
allow_ipv6=self.server.ipv6_enabled(),
streaming_callback=dl.on_chunk_recd,
header_callback=dl.on_headers_recd)
header_callback=dl.on_headers_recd
)
timeout = connect_timeout + request_timeout + 1.
resp = await asyncio.wait_for(fut, timeout)
except asyncio.CancelledError:
Expand Down
9 changes: 6 additions & 3 deletions moonraker/components/simplyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pathlib
import base64
import tornado.websocket
from tornado.httpclient import HTTPRequest
from tornado.escape import url_escape
import logging.handlers
import tempfile
Expand Down Expand Up @@ -197,10 +198,12 @@ async def _connect(self) -> None:
if log_connect:
logging.info(f"Connecting To SimplyPrint: {url}")
log_connect = False
req = HTTPRequest(
url, connect_timeout=5.,
allow_ipv6=self.server.ipv6_enabled()
)
try:
self.ws = await tornado.websocket.websocket_connect(
url, connect_timeout=5.,
)
self.ws = await tornado.websocket.websocket_connect(req)
setattr(self.ws, "on_ping", self._on_ws_ping)
cur_time = self.eventloop.get_loop_time()
self._last_ping_received = cur_time
Expand Down
8 changes: 6 additions & 2 deletions moonraker/components/spoolman.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import contextlib
import tornado.websocket as tornado_ws
from tornado.httpclient import HTTPRequest
from ..common import RequestType, HistoryFieldData
from ..utils import json_wrapper as jsonw
from typing import (
Expand Down Expand Up @@ -128,10 +129,13 @@ async def _connect_websocket(self) -> None:
if log_connect:
logging.info(f"Connecting To Spoolman: {self.ws_url}")
log_connect = False
req = HTTPRequest(
self.ws_url, connect_timeout=5.,
allow_ipv6=self.server.ipv6_enabled()
)
try:
self.spoolman_ws = await tornado_ws.websocket_connect(
self.ws_url,
connect_timeout=5.,
req,
ping_interval=20.,
ping_timeout=60.
)
Expand Down
3 changes: 3 additions & 0 deletions moonraker/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def is_debug_enabled(self) -> bool:
def is_verbose_enabled(self) -> bool:
return self.app_args["verbose"]

def ipv6_enabled(self) -> bool:
return socket.has_ipv6

def _parse_config(self) -> confighelper.ConfigHelper:
config = confighelper.get_configuration(self, self.app_args)
# log config file
Expand Down

0 comments on commit f74cdab

Please sign in to comment.