-
Notifications
You must be signed in to change notification settings - Fork 25
/
dnsproxy.py
executable file
·142 lines (131 loc) · 5.59 KB
/
dnsproxy.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
#!/usr/bin/env python
# coding:utf-8
import sys
import os
try:
import gevent
import gevent.queue
import gevent.monkey
import gevent.coros
import gevent.server
import gevent.pool
gevent.monkey.patch_all(dns=gevent.version_info[0]>=1)
except ImportError:
if os.name == 'nt':
sys.stderr.write('WARNING: python-gevent not installed. `http://code.google.com/p/gevent/downloads/list`\n')
else:
sys.stderr.write('WARNING: python-gevent not installed. `curl -k -L http://git.io/I9B7RQ|sh`\n')
import Queue
import thread
import threading
import SocketServer
def GeventImport(name):
import sys
sys.modules[name] = type(sys)(name)
return sys.modules[name]
def GeventSpawn(target, *args, **kwargs):
return thread.start_new_thread(target, args, kwargs)
def GeventSpawnLater(seconds, target, *args, **kwargs):
def wrap(*args, **kwargs):
import time
time.sleep(seconds)
return target(*args, **kwargs)
return thread.start_new_thread(wrap, args, kwargs)
class GeventServerStreamServer(SocketServer.ThreadingTCPServer):
allow_reuse_address = True
def __init__(self, server_address, *args, **kwargs):
SocketServer.ThreadingTCPServer.__init__(self, server_address, *args, **kwargs)
self.address = self.server_address
def finish_request(self, request, client_address):
self.RequestHandlerClass(request, client_address)
class GeventServerDatagramServer(SocketServer.ThreadingUDPServer):
allow_reuse_address = True
def __init__(self, server_address, *args, **kwargs):
SocketServer.ThreadingUDPServer.__init__(self, server_address, self.RequestHandlerClass, *args, **kwargs)
self.address = self.server_address
self._writelock = threading.Semaphore()
def sendto(self, *args):
self._writelock.acquire()
try:
self.socket.sendto(*args)
finally:
self._writelock.release()
def RequestHandlerClass(self, (data, server_socket), client_addr, server):
return self.handle(data, client_addr)
def handle(self, data, address):
raise NotImplemented()
class GeventPoolPool(object):
def __init__(self, size):
self._lock = threading.Semaphore(size)
def __target_wrapper(self, target, args, kwargs):
t = threading.Thread(target=target, args=args, kwargs=kwargs)
try:
t.start()
t.join()
except Exception as e:
logging.error('threading.Thread target=%r error:%s', target, e)
finally:
self._lock.release()
def spawn(self, target, *args, **kwargs):
self._lock.acquire()
return thread.start_new_thread(self.__target_wrapper, (target, args, kwargs))
gevent = GeventImport('gevent')
gevent.queue = GeventImport('gevent.queue')
gevent.coros = GeventImport('gevent.coros')
gevent.server = GeventImport('gevent.server')
gevent.pool = GeventImport('gevent.pool')
gevent.queue.Queue = Queue.Queue
gevent.coros.Semaphore = threading.Semaphore
gevent.getcurrent = threading.currentThread
gevent.spawn = GeventSpawn
gevent.spawn_later = GeventSpawnLater
gevent.server.StreamServer = GeventServerStreamServer
gevent.server.DatagramServer = GeventServerDatagramServer
gevent.pool.Pool = GeventPoolPool
del GeventImport, GeventSpawn, GeventSpawnLater, GeventServerStreamServer, GeventServerDatagramServer, GeventPoolPool
import re
import time
import SocketServer
import threading
import logging
import socket
import struct
class DNSServer(gevent.server.DatagramServer):
"""DNS Proxy over TCP to avoid DNS poisoning"""
remote_address = ('8.8.8.8', 53)
max_retry = 3
timeout = 3
def __init__(self, *args, **kwargs):
gevent.server.DatagramServer.__init__(self, *args, **kwargs)
self.cache = {}
def handle(self, data, address):
cache = self.cache
timeout = self.timeout
reqid = data[:2]
domain = data[12:data.find('\x00', 12)]
if domain not in cache:
qname = re.sub(r'[\x01-\x10]', '.', domain[1:])
for i in xrange(self.max_retry):
logging.info('DNSServer resolve domain=%r to iplist', qname)
remote_sock = None
try:
remote_sock = socket.create_connection(self.remote_address, timeout=timeout)
remote_sock.sendall(struct.pack('!h', len(data)) + data)
remote_data = remote_sock.recv(512)
if remote_data:
cache[domain] = remote_data[2:]
break
except socket.error as e:
logging.error('DNSServer resolve domain=%r to iplist failed:%s', qname, e)
finally:
if remote_sock:
remote_sock.close()
reply = reqid + cache[domain][2:]
self.sendto(reply, address)
def main():
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(asctime)s %(message)s', datefmt='[%b %d %H:%M:%S]')
server = DNSServer(('', 53))
logging.info('serving at %r', server.address)
server.serve_forever()
if __name__ == '__main__':
main()