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 = (
|
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}"
|
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 ")"
|
ip4_pattern += r"/[0-9]{1,2})" if not skip_netmask else ")"
|
||||||
ip6_pattern += r"/[0-9]{1,3})" if not skip_netmask else ")"
|
ip6_pattern += r"/[0-9]{1,3})" if not skip_netmask else ")"
|
||||||
result = {}
|
result = {}
|
||||||
|
@ -74,14 +74,16 @@ def _extract_inet(string, skip_netmask=False, skip_loopback=True):
|
||||||
|
|
||||||
# Helper command taken from Moulinette
|
# Helper command taken from Moulinette
|
||||||
def get_network_interfaces():
|
def get_network_interfaces():
|
||||||
|
|
||||||
# Get network devices and their addresses (raw infos from 'ip addr')
|
# Get network devices and their addresses (raw infos from 'ip addr')
|
||||||
devices_raw = {}
|
devices_raw = {}
|
||||||
output = check_output("ip addr show")
|
output = check_output("ip --brief a").split("\n")
|
||||||
for d in re.split(r"^(?:[0-9]+: )", output, flags=re.MULTILINE):
|
for line in output:
|
||||||
# Extract device name (1) and its addresses (2)
|
line = line.split()
|
||||||
m = re.match(r"([^\s@]+)(?:@[\S]+)?: (.*)", d, flags=re.DOTALL)
|
iname = line[0]
|
||||||
if m:
|
ips = ' '.join(line[2:])
|
||||||
devices_raw[m.group(1)] = m.group(2)
|
|
||||||
|
devices_raw[iname] = ips
|
||||||
|
|
||||||
# Parse relevant informations for each of them
|
# Parse relevant informations for each of them
|
||||||
devices = {
|
devices = {
|
||||||
|
@ -122,25 +124,18 @@ if __name__ == '__main__':
|
||||||
ips = [] # Human-readable IPs
|
ips = [] # Human-readable IPs
|
||||||
b_ips = [] # Binary-convered IPs
|
b_ips = [] # Binary-convered IPs
|
||||||
|
|
||||||
# Parse the IPs and prepare their binary version
|
ipv4 = interfaces[interface]['ipv4'].split('/')[0]
|
||||||
addressed = False
|
if ipv4:
|
||||||
try:
|
ips.append(ipv4)
|
||||||
ip = interfaces[interface]['ipv4'].split('/')[0]
|
b_ips.append(socket.inet_pton(socket.AF_INET, ipv4))
|
||||||
if len(ip)>0: addressed = True
|
|
||||||
ips.append(ip)
|
ipv6 = interfaces[interface]['ipv6'].split('/')[0]
|
||||||
b_ips.append(socket.inet_pton(socket.AF_INET, ip))
|
if ipv6:
|
||||||
except:
|
ips.append(ipv6)
|
||||||
pass
|
b_ips.append(socket.inet_pton(socket.AF_INET6, ipv6))
|
||||||
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
|
|
||||||
|
|
||||||
# If at least one IP is listed
|
# If at least one IP is listed
|
||||||
if addressed:
|
if ips:
|
||||||
# Create a Zeroconf object, and store the ServiceInfos
|
# Create a Zeroconf object, and store the ServiceInfos
|
||||||
zc = Zeroconf(interfaces=ips)
|
zc = Zeroconf(interfaces=ips)
|
||||||
zcs[zc]=[]
|
zcs[zc]=[]
|
||||||
|
@ -151,11 +146,11 @@ if __name__ == '__main__':
|
||||||
else:
|
else:
|
||||||
# Create a ServiceInfo object for each .local domain
|
# Create a ServiceInfo object for each .local domain
|
||||||
zcs[zc].append(ServiceInfo(
|
zcs[zc].append(ServiceInfo(
|
||||||
type_='_device-info._tcp.local.',
|
type_='_device-info._tcp.local.',
|
||||||
name=interface+': '+d_domain+'._device-info._tcp.local.',
|
name=interface+': '+d_domain+'._device-info._tcp.local.',
|
||||||
addresses=b_ips,
|
addresses=b_ips,
|
||||||
port=80,
|
port=80,
|
||||||
server=d+'.',
|
server=d+'.',
|
||||||
))
|
))
|
||||||
print('Adding '+d+' with addresses '+str(ips)+' on interface '+interface)
|
print('Adding '+d+' with addresses '+str(ips)+' on interface '+interface)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue