mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
mdns: misc fixes for ip parsing
This commit is contained in:
parent
212ea635df
commit
f1444bc36f
1 changed files with 24 additions and 29 deletions
53
bin/yunomdns
53
bin/yunomdns
|
@ -47,7 +47,7 @@ def _extract_inet(string, skip_netmask=False, skip_loopback=True):
|
|||
ip4_pattern = (
|
||||
r"((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}"
|
||||
)
|
||||
ip6_pattern = r"(((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)"
|
||||
ip6_pattern = r"(((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::?((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)"
|
||||
ip4_pattern += r"/[0-9]{1,2})" if not skip_netmask else ")"
|
||||
ip6_pattern += r"/[0-9]{1,3})" if not skip_netmask else ")"
|
||||
result = {}
|
||||
|
@ -74,14 +74,16 @@ def _extract_inet(string, skip_netmask=False, skip_loopback=True):
|
|||
|
||||
# Helper command taken from Moulinette
|
||||
def get_network_interfaces():
|
||||
|
||||
# Get network devices and their addresses (raw infos from 'ip addr')
|
||||
devices_raw = {}
|
||||
output = check_output("ip addr show")
|
||||
for d in re.split(r"^(?:[0-9]+: )", output, flags=re.MULTILINE):
|
||||
# Extract device name (1) and its addresses (2)
|
||||
m = re.match(r"([^\s@]+)(?:@[\S]+)?: (.*)", d, flags=re.DOTALL)
|
||||
if m:
|
||||
devices_raw[m.group(1)] = m.group(2)
|
||||
output = check_output("ip --brief a").split("\n")
|
||||
for line in output:
|
||||
line = line.split()
|
||||
iname = line[0]
|
||||
ips = ' '.join(line[2:])
|
||||
|
||||
devices_raw[iname] = ips
|
||||
|
||||
# Parse relevant informations for each of them
|
||||
devices = {
|
||||
|
@ -122,25 +124,18 @@ if __name__ == '__main__':
|
|||
ips = [] # Human-readable IPs
|
||||
b_ips = [] # Binary-convered IPs
|
||||
|
||||
# Parse the IPs and prepare their binary version
|
||||
addressed = False
|
||||
try:
|
||||
ip = interfaces[interface]['ipv4'].split('/')[0]
|
||||
if len(ip)>0: addressed = True
|
||||
ips.append(ip)
|
||||
b_ips.append(socket.inet_pton(socket.AF_INET, ip))
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
ip = interfaces[interface]['ipv6'].split('/')[0]
|
||||
if len(ip)>0: addressed = True
|
||||
ips.append(ip)
|
||||
b_ips.append(socket.inet_pton(socket.AF_INET6, ip))
|
||||
except:
|
||||
pass
|
||||
ipv4 = interfaces[interface]['ipv4'].split('/')[0]
|
||||
if ipv4:
|
||||
ips.append(ipv4)
|
||||
b_ips.append(socket.inet_pton(socket.AF_INET, ipv4))
|
||||
|
||||
ipv6 = interfaces[interface]['ipv6'].split('/')[0]
|
||||
if ipv6:
|
||||
ips.append(ipv6)
|
||||
b_ips.append(socket.inet_pton(socket.AF_INET6, ipv6))
|
||||
|
||||
# If at least one IP is listed
|
||||
if addressed:
|
||||
if ips:
|
||||
# Create a Zeroconf object, and store the ServiceInfos
|
||||
zc = Zeroconf(interfaces=ips)
|
||||
zcs[zc]=[]
|
||||
|
@ -151,11 +146,11 @@ if __name__ == '__main__':
|
|||
else:
|
||||
# Create a ServiceInfo object for each .local domain
|
||||
zcs[zc].append(ServiceInfo(
|
||||
type_='_device-info._tcp.local.',
|
||||
name=interface+': '+d_domain+'._device-info._tcp.local.',
|
||||
addresses=b_ips,
|
||||
port=80,
|
||||
server=d+'.',
|
||||
type_='_device-info._tcp.local.',
|
||||
name=interface+': '+d_domain+'._device-info._tcp.local.',
|
||||
addresses=b_ips,
|
||||
port=80,
|
||||
server=d+'.',
|
||||
))
|
||||
print('Adding '+d+' with addresses '+str(ips)+' on interface '+interface)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue