Don't contact ip6.yunohost if we can know right away that there's no IPv6 at all on the system

This commit is contained in:
Alexandre Aubin 2020-04-17 19:38:46 +02:00
parent 040bc1d09f
commit 61ef67252e
2 changed files with 18 additions and 5 deletions

View file

@ -106,7 +106,7 @@ class IPDiagnoser(Diagnoser):
# If we are indeed connected in ipv4 or ipv6, we should find a default route
routes = check_output("ip -%s route" % protocol).split("\n")
if not [r for r in routes if r.startswith("default")]:
if not any(r.startswith("default") for r in routes):
return False
# We use the resolver file as a list of well-known, trustable (ie not google ;)) IPs that we can ping

View file

@ -18,10 +18,12 @@
along with this program; if not, see http://www.gnu.org/licenses
"""
import logging
import os
import re
import subprocess
import logging
from moulinette.utils.network import download_text
from moulinette.utils.process import check_output
logger = logging.getLogger('yunohost.utils.network')
@ -36,6 +38,17 @@ def get_public_ip(protocol=4):
else:
raise ValueError("invalid protocol version")
# We can know that ipv6 is not available directly if this file does not exists
if protocol == 6 and not os.path.exists("/proc/net/if_inet6"):
logger.debug("IPv6 appears not at all available on the system, so assuming there's no IP address for that version")
return None
# If we are indeed connected in ipv4 or ipv6, we should find a default route
routes = check_output("ip -%s route" % protocol).split("\n")
if not any(r.startswith("default") for r in routes):
logger.debug("No default route for IPv%s, so assuming there's no IP address for that version" % protocol)
return None
try:
return download_text(url, timeout=30).strip()
except Exception as e:
@ -47,7 +60,7 @@ def get_network_interfaces():
# Get network devices and their addresses (raw infos from 'ip addr')
devices_raw = {}
output = subprocess.check_output('ip addr show'.split())
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)
@ -62,7 +75,7 @@ def get_network_interfaces():
def get_gateway():
output = subprocess.check_output('ip route show'.split())
output = check_output('ip route show')
m = re.search(r'default via (.*) dev ([a-z]+[0-9]?)', output)
if not m:
return None