add config option to allow binding to a specific address

original idea by @OH2LAK (see #367)
This commit is contained in:
Jakob Ketterl 2024-01-18 00:59:43 +01:00
parent 4ef27b5064
commit 6aabff034a
3 changed files with 17 additions and 4 deletions

View file

@ -6,6 +6,10 @@ log_level = INFO
[web] [web]
port = 8073 port = 8073
ipv6 = true 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] [aprs]
# path to the aprs symbols repository (get it here: https://github.com/hessu/aprs-symbols) # path to the aprs symbols repository (get it here: https://github.com/hessu/aprs-symbols)

View file

@ -27,11 +27,11 @@ import socket
class ThreadedHttpServer(ThreadingMixIn, HTTPServer): class ThreadedHttpServer(ThreadingMixIn, HTTPServer):
def __init__(self, web_port, RequestHandlerClass, use_ipv6): def __init__(self, web_port, RequestHandlerClass, use_ipv6, bind_address=None):
bind_address = "0.0.0.0" if bind_address is None:
bind_address = "::" if use_ipv6 else "0.0.0.0"
if use_ipv6: if use_ipv6:
self.address_family = socket.AF_INET6 self.address_family = socket.AF_INET6
bind_address = "::"
super().__init__((bind_address, web_port), RequestHandlerClass) super().__init__((bind_address, web_port), RequestHandlerClass)
@ -135,7 +135,9 @@ Support and info: https://groups.io/g/openwebrx
Services.start() Services.start()
try: 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.") logger.info("Ready to serve requests.")
server.serve_forever() server.serve_forever()
except SignalException: except SignalException:

View file

@ -1,6 +1,7 @@
from owrx.config import ConfigError from owrx.config import ConfigError
from configparser import ConfigParser from configparser import ConfigParser
from pathlib import Path from pathlib import Path
from typing import Optional
import os import os
@ -16,6 +17,8 @@ class CoreConfig(object):
"web": { "web": {
"port": 8073, "port": 8073,
"ipv6": True, "ipv6": True,
# won't work this way because values must be strings, but this is effectively the way it behaves.
#"bind_address": None,
}, },
"aprs": { "aprs": {
"symbols_path": "/usr/share/aprs-symbols/png" "symbols_path": "/usr/share/aprs-symbols/png"
@ -64,6 +67,7 @@ class CoreConfig(object):
self.log_level = config.get("core", "log_level") self.log_level = config.get("core", "log_level")
self.web_port = config.getint("web", "port") self.web_port = config.getint("web", "port")
self.web_ipv6 = config.getboolean("web", "ipv6") 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") self.aprs_symbols_path = config.get("aprs", "symbols_path")
@staticmethod @staticmethod
@ -81,6 +85,9 @@ class CoreConfig(object):
def get_web_ipv6(self) -> bool: def get_web_ipv6(self) -> bool:
return self.web_ipv6 return self.web_ipv6
def get_web_bind_address(self) -> Optional[str]:
return self.web_bind_address
def get_data_directory(self) -> str: def get_data_directory(self) -> str:
return self.data_directory return self.data_directory