forked from henices/Tcp-DNS-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpdns.py
executable file
·209 lines (175 loc) · 6.03 KB
/
tcpdns.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/bin/python
# -*- coding: utf-8 -*-
# cody by zhouzhenster@gmail.com
# ver: 0.2 update 2011-10-23
# use SocketServer to run a multithread udp server
# update:
# 2012-04-16, add more public dns servers support tcp dns query
# 2013-05-14 merge code from linkerlin, add gevent support
# 2013-06-24 add lru cache support
# 2013-08-14 add option to disable cache
# 2014-01-04 add option "servers", "timeout" @jinxingxing
# 8.8.8.8 google
# 8.8.4.4 google
# 156.154.70.1 Dnsadvantage
# 156.154.71.1 Dnsadvantage
# 208.67.222.222 OpenDNS
# 208.67.220.220 OpenDNS
# 198.153.192.1 Norton
# 198.153.194.1 Norton
import os, sys
import socket
import struct
import threading
import SocketServer
import traceback
import random
import optparse
from pylru import lrucache
try:
import gevent
from gevent import monkey
except:
print "*** Install gevent will save a lot of CPU time\n"
else:
monkey.patch_all()
DHOSTS = ['8.8.8.8',
'8.8.4.4',
'156.154.70.1',
'156.154.71.1',
'208.67.222.222',
'208.67.220.220',
#'198.153.192.1',
#'198.153.194.1',
'74.207.247.4',
'209.244.0.3',
'8.26.56.26'
]
DPORT = 53
TIMEOUT = 20
LRUCACHE = None
#-------------------------------------------------------------
# Hexdump Cool :)
# default width 16
#--------------------------------------------------------------
def hexdump( src, width=16 ):
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
result=[]
for i in xrange(0, len(src), width):
s = src[i:i+width]
hexa = ' '.join(["%02X"%ord(x) for x in s])
printable = s.translate(FILTER)
result.append("%04X %s %s\n" % (i, hexa, printable))
return ''.join(result)
#---------------------------------------------------------------
# bytetodomain
# 03www06google02cn00 => www.google.cn
#--------------------------------------------------------------
def bytetodomain(s):
domain = ''
i = 0
length = struct.unpack('!B', s[0:1])[0]
while length != 0 :
i += 1
domain += s[i:i+length]
i += length
length = struct.unpack('!B', s[i:i+1])[0]
if length != 0 :
domain += '.'
return domain
#--------------------------------------------------
# tcp dns request
#---------------------------------------------------
def QueryDNS(server, port, querydata):
# length
Buflen = struct.pack('!h', len(querydata))
sendbuf = Buflen + querydata
data=None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT) # set socket timeout
s.connect((server, int(port)))
s.send(sendbuf)
data = s.recv(2048)
except Exception, e:
print '[ERROR] QueryDNS: %s' % e.message
finally:
if s: s.close()
return data
#-----------------------------------------------------
# send udp dns respones back to client program
#----------------------------------------------------
def transfer(querydata, addr, server):
if not querydata: return
domain = bytetodomain(querydata[12:-4])
qtype = struct.unpack('!h', querydata[-4:-2])[0]
print 'domain:%s, qtype:%x, thread:%d' % \
(domain, qtype, threading.activeCount())
sys.stdout.flush()
response=None
t_id = querydata[:2]
key = querydata[2:].encode('hex')
if LRUCACHE is not None:
try:
response = LRUCACHE[key]
server.sendto(t_id + response[4:], addr)
except KeyError:
pass
if response is not None:
return
for DHOST in DHOSTS:
if DHOST.find(':') >= 0:
ip, port = DHOST.split(':')
else:
ip, port = DHOST, DPORT
response = QueryDNS(ip, port, querydata)
if response is None:
continue
if LRUCACHE is not None:
LRUCACHE[key] = response
# udp dns packet no length
server.sendto(response[2:], addr)
break
if response is None:
print "[ERROR] Tried many times and failed to resolve %s" % domain
class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
def __init__(self, s, t):
SocketServer.UDPServer.__init__(self, s, t)
class ThreadedUDPRequestHandler(SocketServer.BaseRequestHandler):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
def handle(self):
data = self.request[0]
socket = self.request[1]
addr = self.client_address
transfer(data, addr, socket)
#------------------------------------------------------
# main entry
#------------------------------------------------------
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-c", "--cached", action="store_true", dest="cache", default=False, help="Enable LRU cache")
parser.add_option("-s", "--servers", action="store", dest="dns_servers",
help="""Specifies the DNS server, separated by "," , default port 53 (eg. 8.8.8.8:53,8.8.4.4:53)""")
parser.add_option("-t", "--timeout", action="store", dest="query_timeout", help="DNS query timeout")
options, _ = parser.parse_args()
if options.query_timeout:
TIMEOUT = float(options.query_timeout)
if options.dns_servers:
DHOSTS = options.dns_servers.strip(" ,").split(',')
if options.cache:
LRUCACHE = lrucache(100)
if os.name == 'nt':
os.system('title tcpdnsproxy')
print '>> TCP DNS Proxy, https://github.com/henices/Tcp-DNS-proxy'
print '>> DNS Servers:\n%s' % ('\n'.join(DHOSTS))
print '>> Query Timeout: %f' % (TIMEOUT)
print '>> Enable Cache: %r' % (options.cache)
print '>> Please wait program init....'
server = ThreadedUDPServer(('127.0.0.1', 53), ThreadedUDPRequestHandler)
print '>> Init finished!'
print '>> Now you can set dns server to 127.0.0.1'
server.serve_forever()
server.shutdown()