|
|
@ -9,29 +9,35 @@ import io.netty.handler.codec.dns.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
|
|
|
|
public class SimpleDnsServer {
|
|
|
|
public class SimpleDnsServer {
|
|
|
|
|
|
|
|
|
|
|
|
private final EventLoopGroup group = new NioEventLoopGroup();
|
|
|
|
private final EventLoopGroup group = new NioEventLoopGroup();
|
|
|
|
private final Channel channel;
|
|
|
|
private final Channel channel;
|
|
|
|
private String ip = "127.0.0.1";
|
|
|
|
private String ip = "127.0.0.1";
|
|
|
|
private final int port = 55;
|
|
|
|
private final int port;
|
|
|
|
|
|
|
|
|
|
|
|
public SimpleDnsServer() throws InterruptedException {
|
|
|
|
public SimpleDnsServer() throws InterruptedException {
|
|
|
|
Bootstrap bootstrap = new Bootstrap();
|
|
|
|
this(ThreadLocalRandom.current().nextInt(50, 1000));
|
|
|
|
bootstrap.group(group)
|
|
|
|
}
|
|
|
|
.channel(NioDatagramChannel.class)
|
|
|
|
|
|
|
|
.handler(new ChannelInitializer<>() {
|
|
|
|
public SimpleDnsServer(int port) throws InterruptedException {
|
|
|
|
@Override
|
|
|
|
Bootstrap bootstrap = new Bootstrap();
|
|
|
|
protected void initChannel(Channel ch) throws Exception {
|
|
|
|
bootstrap.group(group)
|
|
|
|
ch.pipeline().addLast(new DatagramDnsQueryDecoder());
|
|
|
|
.channel(NioDatagramChannel.class)
|
|
|
|
ch.pipeline().addLast(new DatagramDnsResponseEncoder());
|
|
|
|
.handler(new ChannelInitializer<>() {
|
|
|
|
ch.pipeline().addLast(new DnsMessageHandler());
|
|
|
|
@Override
|
|
|
|
}
|
|
|
|
protected void initChannel(Channel ch) throws Exception {
|
|
|
|
});
|
|
|
|
ch.pipeline().addLast(new DatagramDnsQueryDecoder());
|
|
|
|
|
|
|
|
ch.pipeline().addLast(new DatagramDnsResponseEncoder());
|
|
|
|
|
|
|
|
ch.pipeline().addLast(new DnsMessageHandler());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
ChannelFuture future = bootstrap.bind(port).sync();
|
|
|
|
this.port = port;
|
|
|
|
channel = future.channel();
|
|
|
|
ChannelFuture future = bootstrap.bind(port).sync();
|
|
|
|
|
|
|
|
channel = future.channel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public InetSocketAddress getAddr() {
|
|
|
|
public InetSocketAddress getAddr() {
|
|
|
|