我把虚拟机(win)的 dns 设置成我自己的 dns server 192.168.43.245,然后程序会把 aaa.1.tqtqtq.aaa 这个域名的 ip 解析成 192.168.43.245 ,192.168.43.245 的 80 端口上开了一个 web 服务,这样我认为访问 aaa.1.tqtqtq.aaa 的时候就会访问 192.168.43.245 的 web 服务。
这是本机 dig 的结果
dig aaa.1.tqtqtq.aaa @127.0.0.1
; <<>> DiG 9.10.6 <<>> aaa.1.tqtqtq.aaa @127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57548
;; flags: qr; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;aaa.1.tqtqtq.aaa. IN A
;; ANSWER SECTION:
aaa.1.tqtqtq.aaa. 0 IN A 192.168.43.245
;; Query time: 5 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Jul 18 21:24:32 CST 2019
;; MSG SIZE rcvd: 66
然后用浏览器访问 aaa.1.tqtqtq.aaa 是访问不通的,比如在 ie 下返回的错误信息是
发生临时 DNS 错误。请尝试刷新页面。
错误代码: INET_E_RESOURCE_NOT_FOUND
直接访问 http://192.168.43.245/login 是访问的到的,证明 53 端口和 80 端口虚拟机都访问不到,dns 解析出了问题
在虚拟机 nslookup 的结果也有些不正常
DNS request timed out.
timeout was 2 seconds.
服务器: UnKnown
Address: 192.168.43.245
非权威应答:
名称: aaaa.1.tqtqtq.aaa
Addresses: 192.168.43.245
192.168.43.245
以下是 DNSHandler 的代码,没有找到问题出现在哪里,大家帮忙分析一下吧
public void channelRead0(ChannelHandlerContext ctx, DatagramDnsQuery query){
ByteBuf answerIP;
int logID;
DatagramDnsResponse response = new DatagramDnsResponse(query.recipient(), query.sender(), query.id());
DefaultDnsQuestion dnsQuestion = query.recordAt(DnsSection.QUESTION);
String connectIP = query.sender().getHostName();
String domainRegex = "\\.\\d+\\."+domain.replace(".","\\.")+"\\.$";
String subDomain = dnsQuestion.name().replaceAll(domainRegex,"");
String[] hd = dnsQuestion.name().replace('.'+domain+'.',"").split("\\.");
try{
logID = Integer.parseInt(hd[hd.length-1]);
}catch (NumberFormatException n){
return;
}
String ipRegex = "^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$";
String rebindRegex = "^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$";
if(Pattern.compile(rebindRegex).matcher(subDomain).matches()) {
String[] s = subDomain.split("\\.");
byte[] ip = new byte[4];
byte[] rebindIP = new byte[4];
for (int i = 0; i < s.length; i++) {
if (i < 4) {
ip[i] = (byte) Integer.parseInt(s[i]);
} else {
rebindIP[i - 4] = (byte) Integer.parseInt(s[i]);
}
}
if (questionDomainMap.containsKey(dnsQuestion.name())) {
answerIP = Unpooled.wrappedBuffer(questionDomainMap.get(dnsQuestion.name()));
questionDomainMap.remove(dnsQuestion.name());
} else {
answerIP = Unpooled.wrappedBuffer(ip);
questionDomainMap.put(dnsQuestion.name(), rebindIP);
}
}else if(Pattern.compile(ipRegex).matcher(subDomain).matches()){
String[] s = subDomain.split("\\.");
byte[] ip = new byte[s.length];
for(int i=0; i < s.length; i++){
ip[i] = (byte)Integer.parseInt(s[i]);
}
answerIP = Unpooled.wrappedBuffer(ip);
}else{
answerIP = Unpooled.wrappedBuffer(new byte[] {(byte) 192, (byte) 168, 43, (byte)245});
System.out.println(dnsQuestion.name());
System.out.println(1);
}
DnsLog dnslog = new DnsLog(UUID.randomUUID().toString(),dnsQuestion.name().substring(0,dnsQuestion.name().length()-1),new Timestamp(System.currentTimeMillis()),connectIP,dnsQuestion.type().toString(),logID);
dnsLogService.addDnsLog(dnslog);
response.addRecord(DnsSection.QUESTION, dnsQuestion);
DefaultDnsRawRecord queryAnswer = new DefaultDnsRawRecord(dnsQuestion.name(), DnsRecordType.A, 0, answerIP);
response.addRecord(DnsSection.ANSWER, queryAnswer);
ctx.writeAndFlush(response);
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.