Skip to content

Commit

Permalink
organize nameservers by asn
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechromancer committed Sep 2, 2024
1 parent ff2d7ac commit 94f2b53
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/whoami.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Collect stats on DNS servers using whoami.akamai.net

on:
push:
paths:
- 'nameservers.txt'
workflow_dispatch:

jobs:
file-updated:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Install ASN database
run: |
pip install pyasn
python3 pyasn_util_download.py --latestv46
python3 pyasn_util_convert.py --single rib.*.bz2 asn.db
- name: Publish stats
run: |
python3 public_dns_servers/whoami.py | tee nameservers_by_asn.json
- name: git add nameservers_by_asn.json
uses: EndBug/add-and-commit@v9
with:
add: "nameservers_by_asn.json"
default_author: github_actions
message: "refresh nameservers_by_asn.json"
51 changes: 51 additions & 0 deletions public_dns_servers/whoami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import sys
import json
import dns.resolver
import pyasn
import ipaddress

def query_dns(nameserver, domain):
resolver = dns.resolver.Resolver(configure=False)
resolver.timeout = 1
resolver.lifetime = 1
resolver.nameservers = [nameserver]
try:
answers = resolver.resolve(domain, 'A')
return str(answers[0])
except Exception as e:
return f"Error querying {nameserver}: {str(e)}"

def main():
# List of DNS nameservers to query
nameservers = open('nameservers.txt').read().splitlines()

# Domain to query
domain = 'whoami.akamai.net'

asndb = pyasn.pyasn('asn.db')

asns = {}

try:
for nameserver in nameservers:
ip = query_dns(nameserver, domain)
if not ip.startswith('Error'):
try:
asn, subnet = asndb.lookup(ip)
except Exception as e:
print(f"Error getting subnet for {ip}: {str(e)}")
continue
print(f"Nameserver {nameserver} uses IP {ip} (subnet: {subnet}, ASN: {asn})")
try:
asns[asn].add(nameserver)
except KeyError:
asns[asn] = {nameserver}
else:
print(ip)
print(file=sys.stderr)
finally:
asns = {k:sorted(v) for k,v in asns.items()}
print(json.dumps(asns, sort_keys=True, indent=2))

if __name__ == "__main__":
main()

0 comments on commit 94f2b53

Please sign in to comment.