From 6aabff034a462d7448945bccfdad61d6c0e95754 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 18 Jan 2024 00:59:43 +0100 Subject: [PATCH] add config option to allow binding to a specific address original idea by @OH2LAK (see #367) --- openwebrx.conf | 4 ++++ owrx/__main__.py | 10 ++++++---- owrx/config/core.py | 7 +++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/openwebrx.conf b/openwebrx.conf index 9bbfe806..6131a8d8 100644 --- a/openwebrx.conf +++ b/openwebrx.conf @@ -6,6 +6,10 @@ log_level = INFO [web] port = 8073 ipv6 = true +# Uncomment bind_address to bind OpenWebRX to a specific IP-address. +# By default, OpenWebRX will bind to all interfaces. +# Use ::1 for localhost only, or any other configured address to bind to that address only. +#bind_address = ::1 [aprs] # path to the aprs symbols repository (get it here: https://github.com/hessu/aprs-symbols) diff --git a/owrx/__main__.py b/owrx/__main__.py index 5ed8b569..d48d5de7 100644 --- a/owrx/__main__.py +++ b/owrx/__main__.py @@ -27,11 +27,11 @@ import socket class ThreadedHttpServer(ThreadingMixIn, HTTPServer): - def __init__(self, web_port, RequestHandlerClass, use_ipv6): - bind_address = "0.0.0.0" + def __init__(self, web_port, RequestHandlerClass, use_ipv6, bind_address=None): + if bind_address is None: + bind_address = "::" if use_ipv6 else "0.0.0.0" if use_ipv6: self.address_family = socket.AF_INET6 - bind_address = "::" super().__init__((bind_address, web_port), RequestHandlerClass) @@ -135,7 +135,9 @@ Support and info: https://groups.io/g/openwebrx Services.start() try: - server = ThreadedHttpServer(coreConfig.get_web_port(), RequestHandler, coreConfig.get_web_ipv6()) + server = ThreadedHttpServer( + coreConfig.get_web_port(), RequestHandler, coreConfig.get_web_ipv6(), coreConfig.get_web_bind_address() + ) logger.info("Ready to serve requests.") server.serve_forever() except SignalException: diff --git a/owrx/config/core.py b/owrx/config/core.py index 0d8341cb..f1714336 100644 --- a/owrx/config/core.py +++ b/owrx/config/core.py @@ -1,6 +1,7 @@ from owrx.config import ConfigError from configparser import ConfigParser from pathlib import Path +from typing import Optional import os @@ -16,6 +17,8 @@ class CoreConfig(object): "web": { "port": 8073, "ipv6": True, + # won't work this way because values must be strings, but this is effectively the way it behaves. + #"bind_address": None, }, "aprs": { "symbols_path": "/usr/share/aprs-symbols/png" @@ -64,6 +67,7 @@ class CoreConfig(object): self.log_level = config.get("core", "log_level") self.web_port = config.getint("web", "port") self.web_ipv6 = config.getboolean("web", "ipv6") + self.web_bind_address = config.get("web", "bind_address", fallback=None) self.aprs_symbols_path = config.get("aprs", "symbols_path") @staticmethod @@ -81,6 +85,9 @@ class CoreConfig(object): def get_web_ipv6(self) -> bool: return self.web_ipv6 + def get_web_bind_address(self) -> Optional[str]: + return self.web_bind_address + def get_data_directory(self) -> str: return self.data_directory