[mdns] Allow for multiple yunohost.local

This commit is contained in:
tituspijean 2021-09-21 00:09:49 +02:00
parent bcb48b4948
commit a313a86b8b
No known key found for this signature in database
GPG key ID: EF3B0D7CC0A94720

View file

@ -10,7 +10,7 @@ from time import sleep
from typing import List, Dict from typing import List, Dict
import ifaddr import ifaddr
from zeroconf import Zeroconf, ServiceInfo from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]: def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]:
@ -32,6 +32,23 @@ def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]:
} }
return interfaces return interfaces
# Listener class, to detect duplicates on the network
# Stores the list of servers in its list property
class Listener:
def __init__(self):
self.list = []
def remove_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type, name)
self.list.remove(info.server)
def update_service(self, zeroconf, type, name):
pass
def add_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type, name)
self.list.append(info.server[:-1])
def main() -> bool: def main() -> bool:
### ###
@ -51,8 +68,25 @@ def main() -> bool:
print("No interface listed for broadcast.") print("No interface listed for broadcast.")
return True return True
if "yunohost.local" not in config["domains"]: # Let's discover currently published .local domains accross the network
config["domains"].append("yunohost.local") zc = Zeroconf()
listener = Listener()
browser = ServiceBrowser(zc, "_device-info._tcp.local.", listener)
sleep(2)
browser.cancel()
zc.close()
# If yunohost.local already exists, try yunohost-2.local, and so on.
def yunohost_local(i):
return "yunohost.local" if i < 2 else "yunohost-"+str(i)+".local"
i=1
while yunohost_local(i) in listener.list:
print("Uh oh, "+yunohost_local(i)+" already exists on the network...")
if yunohost_local(i) in config['domains']:
config['domains'].remove(yunohost_local(i))
i += 1
if yunohost_local(i) not in config['domains']:
print("Adding "+yunohost_local(i)+" to the domains to publish.")
config['domains'].append(yunohost_local(i))
zcs: Dict[Zeroconf, List[ServiceInfo]] = {} zcs: Dict[Zeroconf, List[ServiceInfo]] = {}