Browse Source

dnsToIp and other DNSUtils primitives uses sets instead of lists now (speed-up search of ip, e. g. ignoreself/ignoreip check process)

pull/2259/head
sebres 6 years ago
parent
commit
e99635650a
  1. 10
      fail2ban/server/ipdns.py
  2. 9
      fail2ban/tests/filtertestcase.py
  3. 12
      fail2ban/tests/utils.py

10
fail2ban/server/ipdns.py

@ -64,7 +64,7 @@ class DNSUtils:
if ips is not None:
return ips
# retrieve ips
ips = list()
ips = set()
saveerr = None
for fam, ipfam in ((socket.AF_INET, IPAddr.FAM_IPv4), (socket.AF_INET6, IPAddr.FAM_IPv6)):
try:
@ -75,7 +75,7 @@ class DNSUtils:
# (some python-versions resp. host configurations causes returning of integer there):
ip = IPAddr(str(result[4][0]), ipfam)
if ip.isValid:
ips.append(ip)
ips.add(ip)
except Exception as e:
saveerr = e
if not ips and saveerr:
@ -103,19 +103,19 @@ class DNSUtils:
def textToIp(text, useDns):
""" Return the IP of DNS found in a given text.
"""
ipList = list()
ipList = set()
# Search for plain IP
plainIP = IPAddr.searchIP(text)
if plainIP is not None:
ip = IPAddr(plainIP)
if ip.isValid:
ipList.append(ip)
ipList.add(ip)
# If we are allowed to resolve -- give it a try if nothing was found
if useDns in ("yes", "warn") and not ipList:
# Try to get IP from possible DNS
ip = DNSUtils.dnsToIp(text)
ipList.extend(ip)
ipList.update(ip)
if ip and useDns == "warn":
logSys.warning("Determined IP using DNS Lookup: %s = %s",
text, ipList)

9
fail2ban/tests/filtertestcase.py

@ -1800,7 +1800,7 @@ class DNSUtilsNetworkTests(unittest.TestCase):
def testUseDns(self):
res = DNSUtils.textToIp('www.example.com', 'no')
self.assertEqual(res, [])
self.assertSortedEqual(res, [])
res = DNSUtils.textToIp('www.example.com', 'warn')
# sort ipaddr, IPv4 is always smaller as IPv6
self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'])
@ -1821,12 +1821,13 @@ class DNSUtilsNetworkTests(unittest.TestCase):
# sort ipaddr, IPv4 is always smaller as IPv6
self.assertSortedEqual(res, ['93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'])
else:
self.assertEqual(res, [])
self.assertSortedEqual(res, [])
# pure ips:
for s in ('93.184.216.34', '2606:2800:220:1:248:1893:25c8:1946'):
ips = DNSUtils.textToIp(s, 'yes')
self.assertEqual(ips, [s])
self.assertTrue(isinstance(ips[0], IPAddr))
self.assertSortedEqual(ips, [s])
for ip in ips:
self.assertTrue(isinstance(ip, IPAddr))
def testIpToName(self):
unittest.F2B.SkipIfNoNetwork()

12
fail2ban/tests/utils.py

@ -322,12 +322,16 @@ def initTests(opts):
# precache all wrong dns to ip's used in test cases:
c = DNSUtils.CACHE_nameToIp
for i in (
('999.999.999.999', []),
('abcdef.abcdef', []),
('192.168.0.', []),
('failed.dns.ch', []),
('999.999.999.999', set()),
('abcdef.abcdef', set()),
('192.168.0.', set()),
('failed.dns.ch', set()),
):
c.set(*i)
# if fast - precache all host names as localhost addresses (speed-up getSelfIPs/ignoreself):
if unittest.F2B.fast: # pragma: no cover
for i in DNSUtils.getSelfNames():
c.set(i, DNSUtils.dnsToIp('localhost'))
def mtimesleep():

Loading…
Cancel
Save