The IP diagnosers now uses network.py functions

This commit is contained in:
theo@manjaro 2022-06-29 10:37:33 +02:00
parent f7e88aaa75
commit 6288d9f32c
2 changed files with 22 additions and 4 deletions

View file

@ -12,6 +12,7 @@ from moulinette.utils.filesystem import read_file
from yunohost.diagnosis import Diagnoser from yunohost.diagnosis import Diagnoser
from yunohost.utils.network import get_network_interfaces from yunohost.utils.network import get_network_interfaces
from yunohost.utils.network import get_public_ip
from yunohost.settings import settings_get from yunohost.settings import settings_get
logger = log.getActionLogger("yunohost.diagnosis") logger = log.getActionLogger("yunohost.diagnosis")
@ -85,8 +86,8 @@ class MyDiagnoser(Diagnoser):
# to a web server to fetch current IPv4 and v6 # # to a web server to fetch current IPv4 and v6 #
# ##################################################### # # ##################################################### #
ipv4 = self.get_public_ip(4) if can_ping_ipv4 else None ipv4 = get_public_ip(4) if can_ping_ipv4 else None
ipv6 = self.get_public_ip(6) if can_ping_ipv6 else None ipv6 = get_public_ip(6) if can_ping_ipv6 else None
network_interfaces = get_network_interfaces() network_interfaces = get_network_interfaces()

View file

@ -83,19 +83,36 @@ def get_public_ip_from_remote_server(protocol=4):
) )
return None 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_url_yunohost_tab = settings_get("security.ipmirrors.v"+str(protocol)).split(",")
ip_count = {} # Count the number of times an IP has appeared
# Check URLS # Check URLS
for url in ip_url_yunohost_tab: for url in ip_url_yunohost_tab:
logger.debug("Fetching IP from %s " % url) logger.debug("Fetching IP from %s " % url)
try: 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: except Exception as e:
logger.debug( logger.debug(
"Could not get public IPv%s from %s : %s" % (str(protocol), url, str(e)) "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(): def get_network_interfaces():