我将我的 Windows 机器的 DNS 服务器配置为 127.0.0.1 并在本地主机上创建了一个基本的 python 服务器:
from socket import *
serverPort = 53
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('127.0.0.1', serverPort))
print "The server is ready to receive on port: {}".format(serverPort)
while 1:
try:
message, clientAddress = serverSocket.recvfrom(512)
except:
continue
print clientAddress, message
modifiedMessage = "127.0.0.1"
serverSocket.sendto(modifiedMessage, clientAddress)
PS :我知道 DNS 是一个二进制协议(protocol),发送 ASCII 文本不会有任何好处,但我不是要制作解析器,我是试图透明地了解前者的工作原理。
当我启动服务器时,我对以下输出感到很满意:
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 50065), '\xb5\xfc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 52718), '\xc7\x15\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x05tiles\x08services\x07mozilla\x03com\x00\x00\x01\x00\x01')
但与我预期的不同,我仍然能够打开网站。 Wireshark 告诉我,我正在连接到 8.8.8.8(IDK 如何?)。
我尝试从我的机器上清除 DNS 缓存,nada。 我错过了什么?
PPS: 如果我删除 try/catch 子句,我会收到此错误(几秒后 程序的执行):
error: [Errno 10054] An existing connection was forcibly closed by the remote host
请您参考如下方法:
您可能已将 Googles 8.8.8.8 配置为后备 DNS 服务器。
并且由于您正在破坏 DNS 答案,因此接收这些损坏答案的任何人都将退回到辅助服务器。典型 UN*X 机器上 DNS 查询的整个路径非常复杂,整个系统通常非常健壮。
