Java DNS cache viewer

前端 未结 4 1481
离开以前
离开以前 2020-12-13 14:46

Is there a way to view/dump DNS cached used by java.net api?

4条回答
  •  醉酒成梦
    2020-12-13 15:14

    Here is a script to print the positive and negative DNS address cache.

    import java.lang.reflect.Field;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    public class DNSCache {
      public static void main(String[] args) throws Exception {
        InetAddress.getByName("stackoverflow.com");
        InetAddress.getByName("www.google.com");
        InetAddress.getByName("www.yahoo.com");
        InetAddress.getByName("www.example.com");
        try {
            InetAddress.getByName("nowhere.example.com");
        } catch (UnknownHostException e) {
    
        }
    
        String addressCache = "addressCache";
        System.out.println(addressCache);
        printDNSCache(addressCache);
        String negativeCache = "negativeCache";
        System.out.println(negativeCache);
        printDNSCache(negativeCache);
      }
      private static void printDNSCache(String cacheName) throws Exception {
        Class klass = InetAddress.class;
        Field acf = klass.getDeclaredField(cacheName);
        acf.setAccessible(true);
        Object addressCache = acf.get(null);
        Class cacheKlass = addressCache.getClass();
        Field cf = cacheKlass.getDeclaredField("cache");
        cf.setAccessible(true);
        Map cache = (Map) cf.get(addressCache);
        for (Map.Entry hi : cache.entrySet()) {
            Object cacheEntry = hi.getValue();
            Class cacheEntryKlass = cacheEntry.getClass();
            Field expf = cacheEntryKlass.getDeclaredField("expiration");
            expf.setAccessible(true);
            long expires = (Long) expf.get(cacheEntry);
    
            Field af = cacheEntryKlass.getDeclaredField("address");
            af.setAccessible(true);
            InetAddress[] addresses = (InetAddress[]) af.get(cacheEntry);
            List ads = new ArrayList(addresses.length);
            for (InetAddress address : addresses) {
                ads.add(address.getHostAddress());
            }
    
            System.out.println(hi.getKey() + " "+new Date(expires) +" " +ads);
        }
      }
    }
    

提交回复
热议问题