Skip to content

Commit

Permalink
fix edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Nov 17, 2024
1 parent b41bb86 commit 91f43d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/builder/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ def build_dbs_ip_asn(reports: dict, ptrs: dict, lookup_lists: dict, networks: di
IPv4Address(ip)
mmdb4.insert_network(ipset, {**asn_info['full'], **net})

if asn not in json4:
json4[asn] = {}
if asn != 0:
if asn not in json4:
json4[asn] = {}

json4[asn][ip] = {**asn_info['small'], **net_sm}
json4[asn][ip] = {**asn_info['small'], **net_sm}

except AddressValueError:
mmdb6.insert_network(ipset, {**asn_info['full'], **net})

if asn not in json6:
json6[asn] = {}
if asn != 0:
if asn not in json6:
json6[asn] = {}

json6[asn][ip] = {**asn_info['small'], **net_sm}
json6[asn][ip] = {**asn_info['small'], **net_sm}

if asn == 0:
continue

if asn not in asn_reports:
try:
Expand Down Expand Up @@ -94,13 +99,13 @@ def build_dbs_ip_asn(reports: dict, ptrs: dict, lookup_lists: dict, networks: di
}

if not asn_reports[asn]['kind']['hosting']:
asn_info = str(asn_reports[asn]['info']['org']).lower()
if asn_info.find('cloud') != -1 or asn_info.find('hosting') != -1:
asn_org = str(asn_reports[asn]['info']['org']).lower()
if asn_org.find('cloud') != -1 or asn_org.find('host') != -1:
asn_reports[asn]['kind']['hosting'] = True

except KeyError as e:
print(f'ERROR: Failed to lookup metadata of ASN {asn} ({e})')
asn_reports[asn] = {'reports': ip_reports}
print(f'ERROR: Failed to lookup metadata of ASN {asn} (KeyError: {e})')
continue

else:
for report_type, report_count in ip_reports.items():
Expand Down
12 changes: 9 additions & 3 deletions src/builder/enrich_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ def ip_asn_info(ip: str, reports: dict, lookup_lists: dict, ptrs: dict) -> dict:
ip_md = m.get(ip)

try:
asn = int(ip_md['asn'][2:])
asn = int(ip_md['asn'])
# asn = int(ip_md['asn'][2:]) # ipinfo DB

except ValueError:
return {}
return {
'nr': 0,
'full': {},
'small': {},
}

d = {
'asn': asn,
Expand Down Expand Up @@ -110,7 +115,8 @@ def net_asn_info(ip: str) -> dict:
ip_md = m.get(ip)

try:
asn = int(ip_md['asn'][2:])
asn = int(ip_md['asn'])
# asn = int(ip_md['asn'][2:]) # ipinfo DB

except ValueError:
return {}
Expand Down

0 comments on commit 91f43d2

Please sign in to comment.