在上一篇中,使用scrapy修改源IP发送请求的最后我们提到由于hosts文件不支持正则,会导致我们的随机域名DNS查询失败。使用DNS代理服务器可以解决这个问题,
下面是我用gevent写的小工具,很简单。我们只拦截匹配的A记录,然后发送DNS Response,如果不匹配,那么我们服务器就是一个DNS代理,转发请求。
# -*- coding=utf-8 -*-
import struct
from cStringIO import StringIO
from collections import namedtuple
from gevent import socket
from gevent.server import DatagramServer
Hex = lambda x : '0x{0:04x}'.format(x) # Hex(256) => "0x0100"
QueryResult = namedtuple("DnsQuery",
"transactionID,flags,questions,answerRrs \
authorityRrs,additionalRrs,qname,qtype,qclass"
)
LOCALDNS = ("114.114.114.114",53)
Hosts = {
"*.ttt.com":"173.194.127.144", # google ip
}
def preg_match(preg,real):
"""
only support '*'
>>>preg_match("www.*.test*.com","www.python.test.com")
True
>>>preg_match("www.

本文介绍了为了解决hosts文件不支持正则导致DNS查询失败的问题,如何利用gevent编写一个简单的DNS代理服务器。该服务器能拦截匹配的A记录并返回DNS响应,未匹配则作为普通DNS代理转发请求。代码已上传至GitHub,邀请读者参与扩展和完善。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



