|
4 | 4 | from os import system as os_shell
|
5 | 5 | from ipaddress import ip_address
|
6 | 6 |
|
7 |
| -from dns.resolver import Resolver, NoAnswer, NXDOMAIN, LifetimeTimeout, NoNameservers |
8 |
| -from dns.exception import SyntaxError as DNSSyntaxError |
| 7 | +from oxl_utils.ps import process_list_in_threads |
| 8 | +from oxl_utils.net import resolve_dns |
9 | 9 | from maxminddb import open_database as mmdb_database
|
10 | 10 |
|
11 | 11 | from config import *
|
12 | 12 |
|
13 |
| -dns_resolver = Resolver(configure=False) |
14 |
| -dns_resolver.lifetime = 0.1 |
15 |
| -dns_resolver.timeout = 0.1 |
16 |
| -dns_resolver.nameservers = NAMESERVERS |
| 13 | + |
17 | 14 | ptr_cache_lock = Lock()
|
18 | 15 |
|
19 | 16 |
|
20 | 17 | def lookup_ptrs(reports: list[dict]) -> dict:
|
21 |
| - batch = 0 |
22 | 18 | ptrs = {}
|
23 |
| - reports_lst = list(reports) |
24 | 19 |
|
25 | 20 | def _ptr_lookup(ip: str):
|
26 | 21 | try:
|
27 |
| - ptr = [p.to_text() for p in dns_resolver.resolve_address(ip)][0] |
| 22 | + ptr = resolve_dns(ip, t='PTR')[0] |
28 | 23 | with ptr_cache_lock:
|
29 | 24 | ptrs[ip] = ptr
|
30 | 25 |
|
31 |
| - except (IndexError, NoAnswer, NXDOMAIN, DNSSyntaxError, NoNameservers, LifetimeTimeout): |
| 26 | + except IndexError: |
32 | 27 | pass
|
33 | 28 |
|
34 |
| - while batch * PTR_LOOKUP_THREADS < len(reports_lst): |
35 |
| - threads = [] |
36 |
| - for i in range(PTR_LOOKUP_THREADS): |
37 |
| - idx = (batch * PTR_LOOKUP_THREADS) + i |
38 |
| - if idx > len(reports_lst) - 1: |
39 |
| - break |
40 |
| - |
41 |
| - threads.append(Thread( |
42 |
| - target=_ptr_lookup, |
43 |
| - kwargs={'ip': list(reports_lst)[idx]}, |
44 |
| - )) |
45 |
| - |
46 |
| - for t in threads: |
47 |
| - t.start() |
48 |
| - |
49 |
| - threads_done = False |
50 |
| - while not threads_done: |
51 |
| - threads_done = all(not t.is_alive() for t in threads) |
52 |
| - sleep(0.05) |
53 |
| - |
54 |
| - batch += 1 |
55 |
| - |
56 |
| - del reports_lst |
| 29 | + process_list_in_threads(callback=_ptr_lookup, to_process=list(reports), key='ip', parallel=PTR_LOOKUP_THREADS) |
57 | 30 | return ptrs
|
58 | 31 |
|
59 | 32 |
|
|
0 commit comments