72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
def right_now():
|
|
from datetime import datetime, timezone
|
|
return datetime.now(timezone.utc)
|
|
|
|
def detect_gfw(r, ip, check):
|
|
# attempt to detect interference from the Great Firewall of China.
|
|
#from .ips import china
|
|
#if r in china: return True
|
|
|
|
# class D or class E, neither of which are correct for a (public?) DNS.
|
|
#if int(r.partition(".")[0]) >= 224: return True
|
|
|
|
rs = lambda prefix: r.startswith(prefix)
|
|
de = lambda suffix: check.domain.endswith(suffix)
|
|
hosted = de("facebook.com") or de("instagram.com") or de("whatsapp.com")
|
|
if rs("31.13.") and not hosted: return True
|
|
if rs("66.220."): return True
|
|
if rs("69.63."): return True
|
|
if rs("69.171.") and not rs("69.171.250."): return True
|
|
if rs("74.86."): return True
|
|
if rs("75.126."): return True
|
|
if r == "64.13.192.74": return True
|
|
# more non-facebook GFW stuff:
|
|
# 31.13.64.33
|
|
# 31.13.70.1
|
|
# 31.13.70.20
|
|
# 31.13.76.16
|
|
# 31.13.86.1
|
|
# 173.252.110.21
|
|
# 192.99.140.48
|
|
# 199.16.156.40
|
|
# 199.16.158.190
|
|
|
|
return False
|
|
|
|
async def getaddrs(server, domain, opts):
|
|
from dns.asyncresolver import Resolver
|
|
from dns.exception import Timeout
|
|
from dns.resolver import NXDOMAIN, NoAnswer, NoNameservers
|
|
#from dns.resolver import Resolver
|
|
from .ips import ipkey
|
|
|
|
res = Resolver(configure=False)
|
|
if opts.impatient:
|
|
res.timeout = 5
|
|
res.lifetime = 2
|
|
res.nameservers = [server]
|
|
try:
|
|
#ans = res.resolve(domain, "A", search=False)
|
|
ans = await res.resolve(domain, "A", search=False)
|
|
except NXDOMAIN:
|
|
return ["NXDOMAIN"]
|
|
except NoAnswer:
|
|
return ["NoAnswer"]
|
|
except NoNameservers:
|
|
return ["NoNameservers"]
|
|
except Timeout:
|
|
return ["Timeout"]
|
|
#return list(set(rr.address for rr in ans.rrset))
|
|
return sorted(set(rr.address for rr in ans.rrset), key=ipkey)
|
|
|
|
def read_ips(f):
|
|
# TODO: make async and more robust. (regex pls)
|
|
# TODO: does readlines() block if the pipe is left open i.e. user input?
|
|
for ip in f.readlines():
|
|
if "#" in ip:
|
|
ip, _, _ = ip.partition("#")
|
|
ip = ip.strip()
|
|
if ip.count(".") != 3:
|
|
continue
|
|
yield ip
|