免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1399 | 回复: 0
打印 上一主题 下一主题

模拟dns优选,处理dns的响应时间 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-07-11 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-14 09:52 |只看该作者 |倒序浏览
主要用于选择更合适的dns解析服务器。可以自定义配置需要测试的dns地址。
如果你们的宽带是电信联通或移动可以去查询它们所在城市的dns地址也可以加入测试。
dnstest.ini
  1. #要进行解析的域名
  2. test www.baidu.com
  3. test www.oschina.com
  4. test tv.sohu.com

  5. #可用的dns。
  6. dns 8.8.8.8
  7. dns 8.8.4.4
  8. dns 114.114.114.114
  9. dns 114.114.115.115
  10. dns 223.5.5.5
  11. dns 223.6.6.6
  12. dns 1.2.4.8
  13. dns 210.2.4.8
  14. dns 208.67.222.222
  15. dns 208.67.220.220
  16. dns 101.226.4.6
  17. dns 123.125.82.6
  18. dns 123.125.83.6
复制代码
处理线程
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7. import java.net.SocketException;
  8. import java.net.UnknownHostException;
  9. import java.nio.ByteBuffer;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;

  13. public class DNSSpeed implements Runnable {
  14.     private final InetAddress dns;
  15.     private final String dd;

  16.     public DNSSpeed(String dns, String dd) throws UnknownHostException {
  17.         this.dns = InetAddress.getByName(dns);
  18.         this.dd = dd;
  19.     }

  20.     public void run() {
  21.         byte[] data = toDnsBytes(dd);
  22.         DatagramSocket dispatcher = null;
  23.         DatagramPacket pack = null;
  24.         boolean isSuccess = false;
  25.         long start = System.currentTimeMillis();
  26.         try {
  27.             pack = new DatagramPacket(data, data.length, dns, 53);
  28.             dispatcher = createDatagramSocket();
  29.             dispatcher.setSoTimeout(1000);
  30.             dispatcher.send(pack);
  31.             data = new byte[512];
  32.             pack = new DatagramPacket(data, data.length);
  33.             dispatcher.receive(pack);
  34.             if (pack.getLength() > 12) {//简单验证返回包
  35.                 isSuccess = true;
  36.             }
  37.         } catch (Exception e) {
  38.         } finally {
  39.             if (null != dispatcher) {
  40.                 dispatcher.close();
  41.             }
  42.             String info = "%-15s\t%-20s\t%s\t%4d\t";
  43.             info = String.format(info, dns.getHostAddress(), dd, isSuccess, (System.currentTimeMillis() - start));
  44.             System.out.println(info);
  45.         }
  46.     }

  47.     /**
  48.      * 组dns查询包
  49.      * 2015年7月14日
  50.      * @param dd
  51.      * @return
  52.      */
  53.     protected static final byte[] toDnsBytes(String dd) {
  54.         ByteBuffer b = ByteBuffer.allocateDirect(512);
  55.         short id = (short) (3 + r.nextInt(1000));
  56.         b.putShort(id);// id;
  57.         b.put((byte) 0x01);
  58.         b.put((byte) 0x00);
  59.         b.putShort((short) (0x01 & 0xFFFF));
  60.         b.putShort((short) (0x00 & 0xFFFF));
  61.         b.putShort((short) (0x00 & 0xFFFF));
  62.         b.putShort((short) (0x00 & 0xFFFF));
  63.         String[] ss = dd.split("\\.");
  64.         for (int i = 0; i < ss.length; i++) {
  65.             byte[] bb = ss[i].getBytes();
  66.             b.put((byte) (bb.length & 0xFF));
  67.             b.put(bb);
  68.         }
  69.         b.put((byte) 0x00);
  70.         b.putShort((short) 0x01);
  71.         b.putShort((short) 0x01);
  72.         b.flip();
  73.         byte[] bb = new byte[b.remaining()];
  74.         b.get(bb);
  75.         return bb;
  76.     }

  77.     private static Random r = new Random();

  78.     /**
  79.      * 随机取一个 50000开始的端口
  80.      * 2015年7月14日
  81.      * @return
  82.      */
  83.     private static synchronized DatagramSocket createDatagramSocket() {
  84.         DatagramSocket result = null;
  85.         while (null == result) {
  86.             try {
  87.                 int port = 50000 + r.nextInt(10000);
  88.                 result = new DatagramSocket(port);
  89.             } catch (SocketException e) {
  90.                 result = null;
  91.             } finally {
  92.             }
  93.         }
  94.         return result;
  95.     }

  96.     public static void main(String[] args) throws UnknownHostException {
  97.         List<String> config = loadFile("dnstest.ini");
  98.         List<String> dns = getConfig(config, "dns");
  99.         List<String> test = getConfig(config, "test");
  100.         for (String s : dns) {
  101.             for (String t : test) {
  102.                 new DNSSpeed(s, t).run();
  103.             }
  104.         }
  105.     }

  106.     /**
  107.      * 取key. 配置以 key  value格式
  108.      * 2015年7月14日
  109.      * @param config
  110.      * @param key
  111.      * @return
  112.      */
  113.     protected static final List<String> getConfig(List<String> config, String key) {
  114.         List<String> result = new ArrayList<String>();
  115.         for (String ss : config) {
  116.             ss = ss.trim();
  117.             if (ss.startsWith(key)) {
  118.                 result.add(ss.substring(key.length()).trim());
  119.             }
  120.         }
  121.         return result;
  122.     }

  123.     /**
  124.      * 加载配置文件转list
  125.      * 2015年7月14日
  126.      * @param path
  127.      * @return
  128.      */
  129.     protected static final List<String> loadFile(String path) {
  130.         List<String> result = new ArrayList<String>();
  131.         BufferedReader in = null;
  132.         try {
  133.             in = new BufferedReader(new FileReader(path));
  134.             String s = null;
  135.             while (null != (s = in.readLine())) {
  136.                 s = s.trim();
  137.                 if (!s.startsWith("#") && !s.isEmpty()) {//过滤掉空行及#号开头的配置
  138.                     result.add(s);
  139.                 }
  140.             }
  141.         } catch (Exception e) {
  142.             e.printStackTrace();
  143.         } finally {
  144.             if (null != in) {
  145.                 try {
  146.                     in.close();
  147.                 } catch (IOException e) {
  148.                 }
  149.             }
  150.         }
  151.         return result;
  152.     }
  153. }
复制代码
结果数据,主要看最后一位是解析时间
  1. 8.8.8.8         www.baidu.com           true      12   
  2. 8.8.8.8         www.oschina.com         true       4   
  3. 8.8.8.8         tv.sohu.com             true       7   
  4. 8.8.4.4         www.baidu.com           true       7   
  5. 8.8.4.4         www.oschina.com         true       8   
  6. 8.8.4.4         tv.sohu.com             true       6   
  7. 114.114.114.114 www.baidu.com           true       5   
  8. 114.114.114.114 www.oschina.com         true       6   
  9. 114.114.114.114 tv.sohu.com             true       8   
  10. 114.114.115.115 www.baidu.com           true       5   
  11. 114.114.115.115 www.oschina.com         true       6   
  12. 114.114.115.115 tv.sohu.com             true       5   
  13. 223.5.5.5       www.baidu.com           true      41   
  14. 223.5.5.5       www.oschina.com         true      44   
  15. 223.5.5.5       tv.sohu.com             true      42   
  16. 223.6.6.6       www.baidu.com           true      40   
  17. 223.6.6.6       www.oschina.com         true      45   
  18. 223.6.6.6       tv.sohu.com             true      46   
  19. 1.2.4.8         www.baidu.com           true       8   
  20. 1.2.4.8         www.oschina.com         true       6   
  21. 1.2.4.8         tv.sohu.com             true      13   
  22. 210.2.4.8       www.baidu.com           true       6   
  23. 210.2.4.8       www.oschina.com         true       8   
  24. 210.2.4.8       tv.sohu.com             true     187   
  25. 208.67.222.222  www.baidu.com           true       5   
  26. 208.67.222.222  www.oschina.com         true       6   
  27. 208.67.222.222  tv.sohu.com             true       4   
  28. 208.67.220.220  www.baidu.com           true       5   
  29. 208.67.220.220  www.oschina.com         true       7   
  30. 208.67.220.220  tv.sohu.com             true      10   
  31. 101.226.4.6     www.baidu.com           true       5   
  32. 101.226.4.6     www.oschina.com         true       5   
  33. 101.226.4.6     tv.sohu.com             true       6   
  34. 123.125.82.6    www.baidu.com           true       5   
  35. 123.125.82.6    www.oschina.com         true       7   
  36. 123.125.82.6    tv.sohu.com             true       5   
  37. 123.125.83.6    www.baidu.com           true       5   
  38. 123.125.83.6    www.oschina.com         true       5   
  39. 123.125.83.6    tv.sohu.com             true       6
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP