✨ discovery: Allow address/interface restriction

Implements #3791
This commit is contained in:
Gina Häußge committed 2020-11-02 12:02:42 +01:00
1 parent 18e4025c47
commit d41feaec6d
3 files changed
+38 -55

No files matched your search

+8 -42
View File
@@ -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 <https://pypi.python.org/pypi/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 <https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/pybonjour/pybonjour-1.1.1.tar.gz>`_.
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 <https://pypi.python.org/pypi/pybonjour>`_ 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:
+20 -9
View File
@@ -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"
+10 -4
View File
@@ -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