Skip to content

Commit

Permalink
diagnostics/traffic: Add IPv6 support to traffic view
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashus committed Oct 18, 2024
1 parent 70df0a1 commit de69071
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/opnsense/scripts/interfaces/traffic_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dns.resolver
from dns.asyncresolver import Resolver
from concurrent.futures import ThreadPoolExecutor
import ipaddress


def iftop(interface, target):
Expand Down Expand Up @@ -94,15 +95,32 @@ async def request_ittr(self, addresses):
dnsResolver.timeout = 2
tasks = []
for address in addresses:
tasks.append(dnsResolver.resolve_address(address))
try:
if ":" in address:
ipv6_obj = ipaddress.IPv6Address(address)
reverse_ip = '.'.join(reversed(ipv6_obj.exploded.replace(":", ""))) + ".ip6.arpa"
else:
reverse_ip = '.'.join(reversed(address.split("."))) + ".in-addr.arpa"

tasks.append(dnsResolver.resolve(reverse_ip, "PTR"))
except Exception as e:
return

responses = await asyncio.gather(*tasks, return_exceptions=True)
for response in responses:
if type(response) is dns.resolver.Answer:
addr = ".".join(reversed(response.canonical_name.to_text().replace('.in-addr.arpa.', '').split('.')))

for response, address in zip(responses, addresses):
if isinstance(response, dns.resolver.Answer):
if ":" in address:
addr = str(ipaddress.IPv6Address(address))
else:
addr = ".".join(reversed(response.canonical_name.to_text().replace('.in-addr.arpa.', '').split('.')))

for item in response.response.answer:
if type(item) is dns.rrset.RRset and len(item.items) > 0:
if isinstance(item, dns.rrset.RRset) and len(item.items) > 0:
self._results[addr] = str(list(item.items)[0])

return self._results

def collect(self, addresses):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Expand Down

0 comments on commit de69071

Please sign in to comment.