View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.dns.dot;
17  
18  import io.netty5.bootstrap.Bootstrap;
19  import io.netty5.buffer.BufferUtil;
20  import io.netty5.channel.Channel;
21  import io.netty5.channel.ChannelHandlerContext;
22  import io.netty5.channel.ChannelInitializer;
23  import io.netty5.channel.ChannelPipeline;
24  import io.netty5.channel.EventLoopGroup;
25  import io.netty5.channel.MultithreadEventLoopGroup;
26  import io.netty5.channel.SimpleChannelInboundHandler;
27  import io.netty5.channel.nio.NioHandler;
28  import io.netty5.channel.socket.SocketChannel;
29  import io.netty5.channel.socket.nio.NioSocketChannel;
30  import io.netty5.handler.codec.dns.DefaultDnsQuery;
31  import io.netty5.handler.codec.dns.DefaultDnsQuestion;
32  import io.netty5.handler.codec.dns.DefaultDnsResponse;
33  import io.netty5.handler.codec.dns.DnsOpCode;
34  import io.netty5.handler.codec.dns.DnsQuery;
35  import io.netty5.handler.codec.dns.DnsQuestion;
36  import io.netty5.handler.codec.dns.DnsRawRecord;
37  import io.netty5.handler.codec.dns.DnsRecord;
38  import io.netty5.handler.codec.dns.DnsRecordType;
39  import io.netty5.handler.codec.dns.DnsSection;
40  import io.netty5.handler.codec.dns.TcpDnsQueryEncoder;
41  import io.netty5.handler.codec.dns.TcpDnsResponseDecoder;
42  import io.netty5.handler.ssl.SslContext;
43  import io.netty5.handler.ssl.SslContextBuilder;
44  import io.netty5.util.NetUtil;
45  
46  import java.util.Random;
47  import java.util.concurrent.TimeUnit;
48  
49  public final class DoTClient {
50      private static final String QUERY_DOMAIN = "www.example.com";
51      private static final int DNS_SERVER_PORT = 853;
52      private static final String DNS_SERVER_HOST = "8.8.8.8";
53  
54      private DoTClient() {
55      }
56  
57      private static void handleQueryResp(DefaultDnsResponse msg) {
58          if (msg.count(DnsSection.QUESTION) > 0) {
59              DnsQuestion question = msg.recordAt(DnsSection.QUESTION, 0);
60              System.out.printf("name: %s%n", question.name());
61          }
62          for (int i = 0, count = msg.count(DnsSection.ANSWER); i < count; i++) {
63              DnsRecord record = msg.recordAt(DnsSection.ANSWER, i);
64              if (record.type() == DnsRecordType.A) {
65                  // Just print the IP after query
66                  DnsRawRecord raw = (DnsRawRecord) record;
67                  System.out.println(NetUtil.bytesToIpAddress(BufferUtil.getBytes(raw.content())));
68              }
69          }
70      }
71  
72      public static void main(String[] args) throws Exception {
73          EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
74          try {
75              final SslContext sslContext = SslContextBuilder.forClient()
76                                                             .protocols("TLSv1.3", "TLSv1.2")
77                                                             .build();
78  
79              Bootstrap b = new Bootstrap();
80              b.group(group)
81               .channel(NioSocketChannel.class)
82               .handler(new ChannelInitializer<SocketChannel>() {
83                   @Override
84                   protected void initChannel(SocketChannel ch) {
85                       ChannelPipeline p = ch.pipeline();
86                       p.addLast(sslContext.newHandler(ch.bufferAllocator(), DNS_SERVER_HOST, DNS_SERVER_PORT))
87                        .addLast(new TcpDnsQueryEncoder())
88                        .addLast(new TcpDnsResponseDecoder())
89                        .addLast(new SimpleChannelInboundHandler<DefaultDnsResponse>() {
90                            @Override
91                            protected void messageReceived(
92                                    ChannelHandlerContext ctx, DefaultDnsResponse msg) {
93                                try {
94                                    handleQueryResp(msg);
95                                } finally {
96                                    ctx.close();
97                                }
98                            }
99                        });
100                  }
101              });
102             final Channel ch = b.connect(DNS_SERVER_HOST, DNS_SERVER_PORT).asStage().get();
103 
104             int randomID = new Random().nextInt(60000 - 1000) + 1000;
105             DnsQuery query = new DefaultDnsQuery(randomID, DnsOpCode.QUERY)
106                     .setRecord(DnsSection.QUESTION, new DefaultDnsQuestion(QUERY_DOMAIN, DnsRecordType.A));
107             ch.writeAndFlush(query).asStage().sync();
108             boolean success = ch.closeFuture().asStage().await(10, TimeUnit.SECONDS);
109             if (!success) {
110                 System.err.println("dns query timeout!");
111                 ch.close().asStage().sync();
112             }
113         } finally {
114             group.shutdownGracefully();
115         }
116     }
117 }