-
Notifications
You must be signed in to change notification settings - Fork 10
/
daemon_dnsfilter.py
180 lines (136 loc) · 4.35 KB
/
daemon_dnsfilter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# coding:utf8
#
# master
# filter out the urls whoese hosts is accessible through the dns query
# insert into runque
# be sure check by bfdone
import threading
import dnslib
import socket
import os,time
from redis_inc import RedisQueueConnection
from pybloomfilter import BloomFilter
import sys,signal
from time import time,sleep
ThreadRunning = True
def exit_handler(signal, frame):
print "You press Ctrl + C"
ThreadRunning = False
signal.signal(signal.SIGINT, exit_handler)
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind(('', 5310))
cur = 0
s.settimeout(0.1)
done_sites_fname='done_sites.bin'
if os.path.isfile(done_sites_fname):
bfdone = BloomFilter.open(done_sites_fname)
else:
bfdone = BloomFilter(2**23, 10**(-5), done_sites_fname) #8M
urlsque = RedisQueueConnection('extracturls').conn
runque = RedisQueueConnection('running').conn
#query count send out, and success query url cont
querysent = 0
querysuc = 0
#dnslocalserver = "dns_server_ip"
serverips = ['127.0.0.1']
MAX_RUNNING_COUNT = 2**18 # 2**19 = 524 288
MAX_QPS = 100 # 700+
dns_server_died = False
def insert_redis(data):
#perform extract and reform url to runque
#domain format:
# 1. xianpei.360.cn. 320 IN CNAME xianpei-s.360.cn
# 2. 1.2.3.4, 1.2.3.4:8080
domain = str(data).split()[0]
#absoulte domain with a end dot
pre = "http://"
domain = str(pre + domain.rstrip('.'))
#add it to bfdone
# return False if not exists in bfdone, added at same time
ret = bfdone.add(domain)
#insert into redis runque
if not ret:
cnt = runque.put(domain)
def dns_server_check():
global dns_server_died
while True:
scmd = "ps aux | grep dnspod-sr | grep -v grep"
s = "dnspod-sr"
ret = os.popen(scmd)
ret = str(ret.read())
if ret.find(s):
dns_server_died = False
else:
dns_server_died = True
#return
sleep(1)
def handle_request(s):
st = time()
sleep(1)#avoid division by zero
global querysent
while ThreadRunning:
while urlsque.qsize() > 0:
left =urlsque.qsize()
running = runque.qsize()
if querysent % 100 == 0:
print "SEND: %7d \t SUCCESS: %7d \t URLS LEFT: %10d \t RUNNING: %10d SPEED: %6d"\
% (querysent, querysuc, left, running, querysent/int((time()-st)) )
if running > MAX_RUNNING_COUNT:
print "running queue too big: %d" % (running)
print
sleep(60)
url = urlsque.get()
if url in bfdone:
continue
# "http://"
domain = url.strip('http://')
# user:pass@ftp://xxx
ipdomain = domain.split(':')[0]
try:
socket.inet_aton(ipdomain)
#when get here, we believe this is a ipdomain
insert_redis(domain)
continue
except:
pass
dnsr = dnslib.DNSRecord.question(domain)
packet = dnsr.pack()
# send each test domain to each dns server
for ip in serverips:
ret = s.sendto(packet, (ip, 53))
sleep(1.0 / MAX_QPS)
querysent += 1
print "All urls filter done.wait for new urls"
sleep(60)
def handle_response(s):
global querysuc
s.settimeout(10)
while True:
try:
data,addr = s.recvfrom(8192)
addr = addr[0]
req=dnslib.DNSRecord.parse(data)
if req.header.qr:
try:
#answer
ret = req.a
insert_redis(ret)
querysuc += 1
except Exception,e:
# server not found
pass
except Exception,e:
#time out, Ctrl + C
if not ThreadRunning:
print "Exiting recieving"
return
def main():
t2 = threading.Thread(target=(handle_request),args=(s,))
t2.start()
t1 = threading.Thread(target=(handle_response), args=(s,))
t1.start()
t1.join()
t2.join()
if __name__ == '__main__':
main()