设为首页收藏本站
查看: 3321|回复: 2

[高级语言] [Java][网络程序开发]《DNS服务简单实现》

[复制链接]
  • TA的每日心情
    奋斗
    5 小时前
  • 签到天数: 723 天

    [LV.9]以坛为家II

    发表于 2022-11-17 14:35:47 | 显示全部楼层 |阅读模式
    项目使用了Netty高性能网络通信框架。
    使用了4.1.X版本

    1. public class Dns {

    2.     public static void main(String[] args) {
    3.         final NioEventLoopGroup group = new NioEventLoopGroup();
    4.         try {
    5.             Bootstrap bootstrap = new Bootstrap();
    6.             bootstrap.group(group).channel(NioDatagramChannel.class)
    7.                     .handler(new ChannelInitializer<NioDatagramChannel>() {
    8.                         @Override
    9.                         protected void initChannel(NioDatagramChannel nioDatagramChannel) throws Exception {
    10.                             nioDatagramChannel.pipeline().addLast(new DatagramDnsQueryDecoder());
    11.                             nioDatagramChannel.pipeline().addLast(new DatagramDnsResponseEncoder());
    12.                             nioDatagramChannel.pipeline().addLast(new DnsHandler());
    13.                         }
    14.                     }).option(ChannelOption.SO_BROADCAST, true);

    15.             ChannelFuture future = bootstrap.bind(53).sync();
    16.             future.channel().closeFuture().sync();
    17.         } catch (InterruptedException e) {
    18.             e.printStackTrace();
    19.         } finally {
    20.             group.shutdownGracefully();
    21.         }
    22.     }
    23. }

    24. class DnsHandler extends SimpleChannelInboundHandler<DatagramDnsQuery> {
    25.     @Override
    26.     public void channelRead0(ChannelHandlerContext ctx, DatagramDnsQuery query) throws UnsupportedEncodingException {
    27.         // 假数据,域名和ip的对应关系应该放到数据库中
    28.         Map<String, byte[]> ipMap = new HashMap<>();
    29.         ipMap.put("www.cnhongker.net.", new byte[] { 74, 121, (byte) 149, (byte)218 });
    30.         DatagramDnsResponse response = new DatagramDnsResponse(query.recipient(), query.sender(), query.id());
    31.         try {
    32.             DefaultDnsQuestion dnsQuestion = query.recordAt(DnsSection.QUESTION);
    33.             response.addRecord(DnsSection.QUESTION, dnsQuestion);
    34.             System.out.println("查询的域名:" + dnsQuestion.name());

    35.             ByteBuf buf = null;
    36.             if (ipMap.containsKey(dnsQuestion.name())) {
    37.                 buf = Unpooled.wrappedBuffer(ipMap.get(dnsQuestion.name()));
    38.             } else {
    39.                 // TODO  对于没有的域名采用迭代方式
    40.                 // buf = Unpooled.wrappedBuffer(new byte[] { 127, 0, 0, 1});
    41.             }
    42.             // TTL设置为10s, 如果短时间内多次请求,客户端会使用本地缓存
    43.             DefaultDnsRawRecord queryAnswer = new DefaultDnsRawRecord(dnsQuestion.name(), DnsRecordType.A, 10, buf);
    44.             response.addRecord(DnsSection.ANSWER, queryAnswer);

    45.         } catch (Exception e) {
    46.             System.out.println("异常了:" + e);
    47.         }finally {
    48.             ctx.writeAndFlush(response);
    49.         }
    50.     }

    51.     @Override
    52.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    53.         cause.printStackTrace();
    54.     }
    55. }
    复制代码


    启动后,就可以使用该服务器作为DNS服务器。
  • TA的每日心情
    奋斗
    昨天 10:58
  • 签到天数: 1970 天

    [LV.Master]伴坛终老

    发表于 2022-11-17 15:01:19 | 显示全部楼层
    总算见 新帖子了
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    红盟社区--红客联盟 

    Processed in 0.059189 second(s), 21 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    备案号:冀ICP备20006029号-1 Powered by HUC © 2001-2021 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表