-
Notifications
You must be signed in to change notification settings - Fork 8
/
cdn.py
136 lines (116 loc) · 2.56 KB
/
cdn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
import dns.resolver
import requests
import ipaddress
import geoip2.database
import socket
import sys
import re
from concurrent.futures import ThreadPoolExecutor,wait, ALL_COMPLETED
from const import all_CNAME,cdns,ASNS
def matched(obj,list):
#print(obj)
for i in list:
if i in obj:
return True
return False
def getCNAMES(domain):
cnames = []
cname = getCNAME(domain)
if cname is not None:
cnames.append(cname)
while(cname != None):
cname = getCNAME(cname)
if cname is not None:
cnames.append(cname)
return cnames
def getCNAME(domain):
try:
answer = dns.resolver.resolve(domain,'CNAME')
except:
return None
cname = [_.to_text() for _ in answer][0]
return cname
def checkIP(ip):
try:
for cdn in cdns:
if ipaddress.ip_address(ip) in ipaddress.ip_network(cdn):
return True
return False
except:
return False
def getIP(domain):
try:
addr = socket.getaddrinfo(domain,None)
except:
return None
return str(addr[0][4][0])
def checkASN(ip):
try:
with geoip2.database.Reader('GeoLite2-ASN.mmdb') as reader:
response = reader.asn(ip)
for i in ASNS:
if response.autonomous_system_number == int(i):
return True
except:
return False
return False
def wFile(file,str):
try:
f = open(file,'a')
f.write(str)
f.write('\n')
finally:
f.close()
def check(data):
if not re.search(r'\d+\.\d+\.\d+\.\d+', data):
ip = getIP(data)
else:
ip = data
if ip is None:
return
cdnip = checkIP(ip)
if cdnip == True:
print(data+": CDN")
wFile('cdn.txt',data)
return
cdnasn = checkASN(ip)
if cdnasn == True:
print(data+": CDN")
wFile('cdn.txt',data)
return
if not re.search(r'\d+\.\d+\.\d+\.\d+', data):
cnames = getCNAMES(data)
match = False
for i in cnames:
match = matched(i,all_CNAME)
if match == True:
break
if match == True:
print(data+": CDN")
wFile('cdn.txt',data)
return
print(data+": notCDN")
wFile('ip.txt',data)
#wFile('../ip.txt',ip)
return
if __name__ == '__main__':
if len(sys.argv) != 2:
print("error command -h for help")
exit()
if sys.argv[1] == '-h':
print("")
print("checkCDN.py list.txt")
print("")
exit()
dataList = []
try:
f = open(sys.argv[1])
for text in f.readlines():
data = text.strip('\n')
dataList.append(data)
finally:
f.close()
with ThreadPoolExecutor(max_workers=100) as pool:
all_task = [pool.submit(check,data) for data in dataList]
wait(all_task, return_when=ALL_COMPLETED)