-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdns2tcp.py
58 lines (49 loc) · 1.47 KB
/
dns2tcp.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2012-09-28
@author: shell.xu
'''
import os, sys, struct, socket, logging
DNSSERVER = '8.8.8.8'
def initlog(lv, logfile=None):
rootlog = logging.getLogger()
if logfile: handler = logging.FileHandler(logfile)
else: handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
'%(asctime)s,%(msecs)03d %(name)s[%(levelname)s]: %(message)s',
'%H:%M:%S'))
rootlog.addHandler(handler)
rootlog.setLevel(lv)
def on_datagram(data):
sock = socket.socket()
try:
sock.connect((DNSSERVER, 53))
stream = sock.makefile()
s = struct.pack('!H', len(data))
stream.write(s+data)
stream.flush()
s = stream.read(2)
if len(s) == 0: raise EOFError()
count = struct.unpack('!H', s)[0]
reply = stream.read(count)
if len(reply) == 0: raise EOFError()
finally: sock.close()
return reply
def server(port=53):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', port))
logging.info('init DNS Server')
while True:
data, addr = sock.recvfrom(1024)
logging.debug('data come in from %s' % str(addr))
try:
r = on_datagram(data)
if r is None: continue
sock.sendto(r, addr)
except Exception, err: logging.exception(err)
def main():
initlog(logging.DEBUG)
server()
if __name__ == '__main__': main()