Added a function to check if an IP is local

This commit is contained in:
theo@manjaro 2022-06-29 17:23:23 +02:00
parent 4aea200575
commit 75dd3e5b1d
2 changed files with 9 additions and 12 deletions

View file

@ -31,6 +31,13 @@ from yunohost.settings import settings_get
logger = logging.getLogger("yunohost.utils.network") logger = logging.getLogger("yunohost.utils.network")
def is_ip_local(ip):
"""Returns True if the provided Ip is local"""
filters = ["192.168", "172.16.", "10."]
for filter in filters:
if ip.startswith(filter):
return True
def get_public_ip(protocol=4): def get_public_ip(protocol=4):

View file

@ -7,6 +7,7 @@ import re
from yunohost.domain import _get_maindomain, domain_list from yunohost.domain import _get_maindomain, domain_list
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from yunohost.utils.network import is_ip_local
logger = logging.getLogger("yunohost.utils.yunopaste") logger = logging.getLogger("yunohost.utils.yunopaste")
@ -89,18 +90,7 @@ def anonymize(data):
ipsv6 = re.findall(ipv6regex, data) ipsv6 = re.findall(ipv6regex, data)
# Filter local IPs # Filter local IPs
filters = ["192.168", "172.16.", "10."] ipsv4 = filter(lambda x: not is_ip_local(x), ipsv4)
i = 0
while i<len(ipsv4):
matched = False
for filter in filters:
if ipsv4[i].startswith(filter):
matched = True
break
if matched:
del(ipsv4[i])
else:
i+=1
def gen_anonymized_ip(length,counter,sep='.'): def gen_anonymized_ip(length,counter,sep='.'):
# Generate anonymized IPs like "xx.xx.xx.yy" # Generate anonymized IPs like "xx.xx.xx.yy"