#!/bin/env python
#-*- coding:utf-8 -*-
import socket
import os
import sys
def daemon():
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
print 'fork #1 fail', e
sys.exit(1)
os.chdir('/')
os.setsid()
os.umask(022)
try:
pid = os.fork()
if pid > 0:
print 'daemon pid %d' %pid
sys.exit(0)
except OSError, e:
print 'fork #2 fail', e
sys.exit(1)
sys.stdout.flush()
sys.stderr.flush()
nulldev = '/dev/null'
stdin = file(nulldev, 'r')
stdout = file(nulldev, 'a+')
stderr = file(nulldev, 'a+', 0)
os.dup2(stdin.fileno(), sys.stdin.fileno())
os.dup2(stdin.fileno(), sys.stdout.fileno())
os.dup2(stdin.fileno(), sys.stderr.fileno())
return pid
def proxy(data):
dnsList = [('192.168.4.88', 53), ('202.106.0.20', 53), ('8.8.8.8', 53)]
for i in dnsList:
dnsQuerySock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
dnsQuerySock.sendto(data, i)
redata, ADDR = dnsQuerySock.recvfrom(1024)
if not data or not dpkt.dns.DNS(data).an:
continue
else:
break
except:
continue
return redata
def work(host, port, bufsize):
address = (host, port)
udpSerSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSerSock.bind(address)
while True:
data, addr = udpSerSock.recvfrom(bufsize)
if not data:
continue
else:
data = proxy(data)
udpSerSock.sendto(data, addr)
udpSerSock.close()
if __name__ == '__main__':
daemon()
work('0.0.0.0', 53, 1024)
阅读(1924) | 评论(0) | 转发(0) |