【发布时间】:2018-03-19 00:08:25
【问题描述】:
我一直在尝试使用 netty 编写自定义 DNS 服务器。我已经使用DatagramDnsQueryDecoder 解析传入的 DNSQuery UDP 数据包,但我不知道如何发送响应来解析域名。我从处理程序收到了DatagramDnsQuery 对象,但找不到初始化DatagramDnsResponse 并添加测试DNS 记录并通过DatagramDnsResponseEncoder 将其发送回客户端的方法。
这是我到目前为止所做的事情
public class DNSListen {
public static void main(String[] args) {
final NioEventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel nioDatagramChannel) throws Exception {
nioDatagramChannel.pipeline().addLast(new DatagramDnsQueryDecoder());
nioDatagramChannel.pipeline().addLast(new DatagramDnsResponseEncoder());
nioDatagramChannel.pipeline().addLast(new DNSMessageHandler());
}
})
.option(ChannelOption.SO_BROADCAST, true);
ChannelFuture future = bootstrap.bind(53).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
这里是 DNSMessages 的处理程序
public class DNSMessageHandler extends SimpleChannelInboundHandler<DatagramDnsQuery> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramDnsQuery dnsMessage) throws Exception {
System.out.println(dnsMessage.content());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
}
当我运行此程序并将系统 DNS 设置为 127.0.0.1 时,我得到了多个 DNS 请求的输出 但我找不到发送虚拟 DNS 响应的方法,以便为请求的域解析 IP 地址。 (我认为我应该初始化一个DatagramDnsResponse 对象并将其写回给用户,但这样对吗?如果是这样,我如何使用虚拟IP 作为解析的IP 对其进行初始化) 我是不是走错了路,有人可以指导我走正确的路吗?谢谢。
【问题讨论】:
-
我已经添加了我到目前为止所做的事情,并进一步解释了。谢谢。
标签: java dns netty nameservers