From 6288d9f32c2761cb875b17bd140311a0c851d647 Mon Sep 17 00:00:00 2001 From: "theo@manjaro" Date: Wed, 29 Jun 2022 10:37:33 +0200 Subject: [PATCH] The IP diagnosers now uses network.py functions --- src/diagnosers/10-ip.py | 5 +++-- src/utils/network.py | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/diagnosers/10-ip.py b/src/diagnosers/10-ip.py index dd71f3a85..609411e04 100644 --- a/src/diagnosers/10-ip.py +++ b/src/diagnosers/10-ip.py @@ -12,6 +12,7 @@ from moulinette.utils.filesystem import read_file from yunohost.diagnosis import Diagnoser from yunohost.utils.network import get_network_interfaces +from yunohost.utils.network import get_public_ip from yunohost.settings import settings_get logger = log.getActionLogger("yunohost.diagnosis") @@ -85,8 +86,8 @@ class MyDiagnoser(Diagnoser): # to a web server to fetch current IPv4 and v6 # # ##################################################### # - ipv4 = self.get_public_ip(4) if can_ping_ipv4 else None - ipv6 = self.get_public_ip(6) if can_ping_ipv6 else None + ipv4 = get_public_ip(4) if can_ping_ipv4 else None + ipv6 = get_public_ip(6) if can_ping_ipv6 else None network_interfaces = get_network_interfaces() diff --git a/src/utils/network.py b/src/utils/network.py index 3fafc844e..134b23909 100644 --- a/src/utils/network.py +++ b/src/utils/network.py @@ -83,19 +83,36 @@ def get_public_ip_from_remote_server(protocol=4): ) return None + # Check URLS + ip_list = get_public_ips(protocol) + if len(ip_list)>0: + return ip_list[0] + + return None + +def get_public_ips(protocol=4): + """Retrieve a list (sorted by frequency) of different public IP addresses from the IPmirrors""" + ip_url_yunohost_tab = settings_get("security.ipmirrors.v"+str(protocol)).split(",") + ip_count = {} # Count the number of times an IP has appeared # Check URLS for url in ip_url_yunohost_tab: logger.debug("Fetching IP from %s " % url) try: - return download_text(url, timeout=30).strip() + ip = download_text(url, timeout=30).strip() + if ip in ip_count.keys(): + ip_count[ip]+=1 + else: + ip_count[ip]=1 except Exception as e: logger.debug( "Could not get public IPv%s from %s : %s" % (str(protocol), url, str(e)) ) - return None + ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ] + ip_list_with_count.sort(key=lambda x: x[1]) # Sort by frequency + return [ x[0] for x in ip_list_with_count ] def get_network_interfaces():