From d41feaec6dc40dfba904b5bb29644ec1372ea999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 2 Nov 2020 12:02:42 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20discovery:=20Allow=20address/interf?= =?UTF-8?q?ace=20restriction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements #3791 --- docs/bundledplugins/discovery.rst | 50 ++++----------------- src/octoprint/plugins/discovery/__init__.py | 29 ++++++++---- src/octoprint/util/net.py | 14 ++++-- 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/docs/bundledplugins/discovery.rst b/docs/bundledplugins/discovery.rst index 36f9911e0..1bc8fac22 100644 --- a/docs/bundledplugins/discovery.rst +++ b/docs/bundledplugins/discovery.rst @@ -7,9 +7,8 @@ Discovery Plugin The OctoPrint Discovery Plugin comes bundled with OctoPrint. -It allows discovery of the OctoPrint instances via SSDP/UPNP. If -`pybonjour `_ is installed OctoPrint -will additionally support discovery via ZeroConf, also known as Bonjour or Avahi. +It allows discovery of the OctoPrint instances via SSDP/UPNP and ZeroConf +(also known as Bonjour or Avahi). The SSDP/UPNP support allows OctoPrint to announce itself to machines on the same network running Microsoft Windows. You will be able to just double click on the @@ -30,45 +29,6 @@ line) to scan for available instances. Various discovered OctoPrint instances in Windows Explorer -.. _sec-bundledplugins-discovery-firststeps: - -First Steps ------------ - -.. _sec-bundledplugins-discovery-firststeps-pybonjour: - -Installing pybonjour -++++++++++++++++++++ - -.. note:: - - OctoPi versions 0.12.0 and later already come with pybonjour installed and ready to go, - you don't need to perform these steps there. - -.. note:: - - Currently there are no releases for pybonjour available on the Python Package Index PyPI. The latest pybonjour - release is still available in the `Google Code Archive `_. - Since that URL is hilariously long though, a shortened version is provided with https://goo.gl/SxQZ06 and - used in the installation instructions below. - -In order for the Zeroconf discovery to work, the -`pybonjour package `_ needs to be available -to the Python installation running OctoPrint. - -It can be installed via ``pip``. Let's assume you installed OctoPrint manually -into some folder ``~/OctoPrint``. You executed ``python setup.py install`` within a -virtualenv in the same folder called ``venv``. In order to install ``pybonjour`` -so it will be available to OctoPrint you'll need to do the following:: - - venv/bin/pip install https://goo.gl/SxQZ06 - -**Linux users:** You'll need to install an additional dependency for this to work, the -libdnssd compatibility layer for libavahi. On Debian/Ubuntu that can be achieved with:: - - sudo apt-get install libavahi-compat-libdnssd-dev - - .. _sec-bundledplugins-discovery-configuration: Configuring the plugin @@ -76,6 +36,10 @@ Configuring the plugin The plugin supports the following configuration keys: + * ``addresses``: List of IP addresses for which to enable discovery, if unset all + addresses available on the host will be used + * ``interfaces``: List of network interfaces for which to enable discovery, if unset + all interfaces on the host will be used * ``publicPort``: Public port number OctoPrint is reachable under, optional, if not set the port OctoPrint itself was started under will be used * ``pathPrefix``: Path prefix OctoPrint is running under, optional, if not @@ -130,6 +94,8 @@ The following snippet is a valid configuration example for the discovery plugin plugins: discovery: + interfaces: + - eth0 publicPort: 443 useSsl: true zeroConf: diff --git a/src/octoprint/plugins/discovery/__init__.py b/src/octoprint/plugins/discovery/__init__.py index 4bbf856af..801a0dfa7 100644 --- a/src/octoprint/plugins/discovery/__init__.py +++ b/src/octoprint/plugins/discovery/__init__.py @@ -99,6 +99,8 @@ class DiscoveryPlugin( "vendor": None, "vendorUrl": None, }, + "addresses": None, + "interfaces": None, } ##~~ BlueprintPlugin API -- used for providing the SSDP device descriptor XML @@ -235,7 +237,7 @@ class DiscoveryPlugin( key = (reg_type, port) addresses = list( - map(lambda x: socket.inet_aton(x), octoprint.util.net.interface_addresses()) + map(lambda x: socket.inet_aton(x), self.get_interface_addresses()) ) try: @@ -457,7 +459,7 @@ class DiscoveryPlugin( ) for _ in range(retries): - for addr in octoprint.util.interface_addresses(): + for addr in self.get_interface_addresses(): try: sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP @@ -621,7 +623,7 @@ class DiscoveryPlugin( # the monitor already shut down, alive messages don't make sense anymore as byebye will shortly follow return - for addr in octoprint.util.interface_addresses(): + for addr in self.get_interface_addresses(): try: sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP @@ -754,14 +756,14 @@ class DiscoveryPlugin( ) and request.headers["MAN"] == '"ssdp:discover"' ): - interface_address = octoprint.util.address_for_client(*address) + interface_address = octoprint.util.address_for_client( + *address, + addresses=self._settings.get(["addresses"]), + interfaces=self._settings.get(["interfaces"]) + ) if not interface_address: - self._logger.warning( - "Can't determine address to user for client {}, not sending a M-SEARCH reply".format( - address - ) - ) continue + message = location_message.format( uuid=self.get_uuid(), location="http://{host}:{port}/plugin/discovery/discovery.xml".format( @@ -805,6 +807,15 @@ class DiscoveryPlugin( else: return "OctoPrint instance on {}".format(socket.gethostname()) + def get_interface_addresses(self): + addresses = self._settings.get(["addresses"]) + if addresses: + return addresses + else: + return octoprint.util.interface_addresses( + interfaces=self._settings.get(["interfaces"]) + ) + __plugin_name__ = "Discovery" __plugin_author__ = "Gina Häußge" diff --git a/src/octoprint/util/net.py b/src/octoprint/util/net.py index 3f0acada3..70f81b3d0 100644 --- a/src/octoprint/util/net.py +++ b/src/octoprint/util/net.py @@ -146,7 +146,7 @@ def unmap_v4_as_v6(address): return address -def interface_addresses(family=None): +def interface_addresses(family=None, interfaces=None): """ Retrieves all of the host's network interface addresses. """ @@ -156,7 +156,10 @@ def interface_addresses(family=None): if not family: family = netifaces.AF_INET - for interface in netifaces.interfaces(): + if interfaces is None: + interfaces = netifaces.interfaces() + + for interface in interfaces: try: ifaddresses = netifaces.ifaddresses(interface) except Exception: @@ -167,12 +170,15 @@ def interface_addresses(family=None): yield ifaddress["addr"] -def address_for_client(host, port, timeout=3.05): +def address_for_client(host, port, timeout=3.05, addresses=None, interfaces=None): """ Determines the address of the network interface on this host needed to connect to the indicated client host and port. """ - for address in interface_addresses(): + if addresses is None: + addresses = interface_addresses(interfaces=interfaces) + + for address in addresses: try: if server_reachable(host, port, timeout=timeout, proto="udp", source=address): return address