Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying which IP addresses to listen on #526

Merged
merged 14 commits into from
Feb 6, 2024
10 changes: 10 additions & 0 deletions matter_server/server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
DEFAULT_VENDOR_ID = 0xFFF1
DEFAULT_FABRIC_ID = 1
DEFAULT_PORT = 5580
# Default to None to bind to all addresses on both IPv4 and IPv6
DEFAULT_LISTEN_ADDRESS = None
DEFAULT_STORAGE_PATH = os.path.join(Path.home(), ".matter_server")

# Get parsed passed in arguments.
Expand Down Expand Up @@ -45,6 +47,13 @@
default=DEFAULT_PORT,
help=f"TCP Port to run the websocket server, defaults to {DEFAULT_PORT}",
)
parser.add_argument(
"--listen-address",
type=str,
action="append",
default=DEFAULT_LISTEN_ADDRESS,
help="IP address to bind the websocket server to, defaults to listen on any interface (IPv4 and IPv6).",
)
parser.add_argument(
"--log-level",
type=str,
Expand Down Expand Up @@ -95,6 +104,7 @@ def main() -> None:
int(args.vendorid),
int(args.fabricid),
int(args.port),
args.listen_address,
args.primary_interface,
)

Expand Down
9 changes: 6 additions & 3 deletions matter_server/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ def __init__(
vendor_id: int,
fabric_id: int,
port: int,
primary_interface: str | None,
listen_addresses: list[str] | None = None,
primary_interface: str | None = None,
) -> None:
"""Initialize the Matter Server."""
self.storage_path = storage_path
self.vendor_id = vendor_id
self.fabric_id = fabric_id
self.port = port
self.listen_addresses = listen_addresses
self.primary_interface = primary_interface
self.logger = logging.getLogger(__name__)
self.app = web.Application()
Expand Down Expand Up @@ -102,8 +104,9 @@ async def start(self) -> None:
self.app.router.add_route("GET", "/", self._handle_info)
self._runner = web.AppRunner(self.app, access_log=None)
await self._runner.setup()
# set host to None to bind to all addresses on both IPv4 and IPv6
self._http = web.TCPSite(self._runner, host=None, port=self.port)
self._http = web.TCPSite(
self._runner, host=self.listen_addresses, port=self.port
)
await self._http.start()
self.logger.debug("Webserver initialized.")

Expand Down
Loading