具有netty的DNS服务器,挖掘正确,但浏览器无法连接到Web

时间:2019-07-18 13:29:42

标签: dns netty

我尝试用netty编写一个简单的DNS服务器。当我使用dig命令时,它将返回正确的结果,但是当我将DNS服务器更改为我的服务器并使用浏览器访问域时,它将失败。我认为浏览器无法从DNS服务器获取IP地址。

我的IP为const a = [1,2] const b = [3,4] const c = [5,6] a.push(...b, ...c) console.log(a) // return [1,2,3,4,5,6] 。我在端口80上打开了一个Web服务器,并尝试使用我的浏览器访问192.168.43.245(返回aaa.1.tqtqtq.aaa的测试域),但无法连接。

192.168.43.245

这是我的挖掘结果

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);

我尝试在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 上使用浏览器连接,DNS服务器向浏览器返回了一条消息,但浏览器连接错误。

当我使用nslookup时,它给了我这个:

aaa.1.tqtqtq.aaa

我的代码可能有错误,但我不知道。

0 个答案:

没有答案