-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_dns_ad_cache.py
executable file
·106 lines (97 loc) · 2.76 KB
/
get_dns_ad_cache.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
#!/usr/bin/python
import sys, re, fileinput
if len(sys.argv) is not 4:
print "usage: ./get_dnsmasq_conf.py <TYPE> <IP(TYPE=DNSMASQ)|ZONE(TYPE=BIND8)> <ad_domain_list>"
print "example: ./get_dnsmasq_conf DNSMASQ 192.168.0.8 ad_domain_list.txt"
print "example: ./get_dnsmasq_conf BIND8 /etc/zone/master/null.zone.file ad_domain_list.txt"
exit()
TYPE = sys.argv[1]
IP = sys.argv[2]
ZONE = sys.argv[2]
re_file=re.compile("\.(png|gif|jpg|css|htm|html|json)$", re.IGNORECASE)
re_ip=re.compile("^[0-9.]*$")
urls = dict()
for line in fileinput.input(sys.argv[3]):
if "!" in line:
continue
if "," in line:
continue
if "#" in line:
continue
if "@" in line:
continue
if "?" in line:
continue
if "*" in line:
continue
if "[" in line:
continue
if "]" in line:
continue
if "/" in line:
continue
if "=" in line:
continue
if "&" in line:
continue
if ":" in line:
continue
line = re.sub('\^$','', line.rstrip())
line = re.sub('^\|','', line.rstrip())
line = re.sub('^\||','', line.rstrip())
#line = line.replace("^$third-party","")
line = re.sub('\^\$third-party$','', line.rstrip())
if "$" in line:
continue
if line[0] == '-' or line[0] == '_' or line[0] == '.' or line[0] == '*':
continue
line = line.replace("\r","").replace("\n","")
if line[-1] == '-' or line[-1] == '_' or line[-1] == '.' or line[-1] == '*' or line[-1] == '|':
continue
if line[-1] == '-' or line[-1] == '_' or line[-1] == '.' or line[-1] == '*' or line[-1] == '|':
continue
if re_ip.match(line) != None:
continue
if re_file.match(line) != None:
continue
#remove general subdomain
#line = line.replace("www.","")
line = re.sub("^www\.", "", line)
#change top domain
line = line.replace(".co.kr",".co_kr")
#get domain name
parse = [x for x in line.split('.')]
name = parse[-2]
#revert top domain
line = line.replace("co_kr","co.kr")
#insert url to dict without duplicates
if name in urls:
if line not in urls[name]:
urls[name].append(line)
else:
urls[name]=[]
urls[name].append(line)
for name in sorted(urls.keys()):
#get urls_to_exclude via exclude sub domain
#urls[name] = [y[::-1] for y in sorted([x[::-1] for x in urls[name]])]
urls_to_exclude = []
#print ""
#print name
#print "urls:\t",
#print urls[name]
for url in urls[name]:
u = [s for s in urls[name] if url in s][1:]
urls_to_exclude = urls_to_exclude + u
#print "sub[%s]:\t" % url,
#print u
urls[name] = [url for url in urls[name] if url not in urls_to_exclude]
if "DNSMASQ" in TYPE:
#generate dnsmasq conf file
for url in sorted(urls[name]):
#print url
print "address=/.%s/%s\t#%s" % (url, IP, name)
else:
#generate bind8 conf file
for url in sorted(urls[name]):
#print url
print "zone \"%s\" { type master; notify no; file \"%s\"; };" % (url, ZONE)