Skip to content

Commit

Permalink
fix(service): register Zeroconf service for all non-loopback addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Cedric Hombourger <[email protected]>
  • Loading branch information
chombourger committed Dec 31, 2024
1 parent 0f6aad0 commit d22c116
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Depends: mtda-common,
python3-daemon,
python3-gevent,
python3-libgpiod,
python3-netifaces,
python3-psutil,
python3-requests,
python3-serial,
Expand Down
20 changes: 13 additions & 7 deletions mtda-service
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import argparse
import daemon
import lockfile
import netifaces
import os
import os.path
import signal
Expand Down Expand Up @@ -56,12 +57,16 @@ class Application:
status = self.server()
return status

def _ip(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('192.0.2.0', 0))
ip = s.getsockname()[0]
s.close()
return ip
def _addresses(self):
results = []
for iface in netifaces.interfaces():
if iface.startswith("lo"):
continue
if netifaces.AF_INET in netifaces.ifaddresses(iface):
addresses = netifaces.ifaddresses(iface)[netifaces.AF_INET]
for addr in addresses:
results.append(addr['addr'])
return results

def server(self):
# Start our agent
Expand All @@ -82,10 +87,11 @@ class Application:
}
deviceType = CONSTS.MDNS.TYPE
name = self.agent.name
addresses = self._addresses()
info = zeroconf.ServiceInfo(
type_=deviceType,
name=f'{name}.{deviceType}',
addresses=[socket.inet_aton(self._ip())],
addresses=[socket.inet_aton(addr) for addr in addresses],
port=int(self.agent.ctrlport),
properties=props,
server=f'{name}.local.')
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"gevent",
"gevent-websocket",
"kconfiglib",
"netifaces>=0.11.0",
"pyserial>=2.6",
"python-daemon>=2.0",
"pyusb>=1.0",
Expand Down

0 comments on commit d22c116

Please sign in to comment.