mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
The IP diagnosers now uses network.py functions
This commit is contained in:
parent
f7e88aaa75
commit
6288d9f32c
2 changed files with 22 additions and 4 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Add table
Reference in a new issue