1. 程式人生 > >獲取郵箱的DNS和MX 工具類

獲取郵箱的DNS和MX 工具類

maven ble javax override exc factor host provide getattr

1.導入Maven DNS 包:

<dependency>
    <groupId>dnsjava</groupId>
    <artifactId>dnsjava</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId
> <version>2.0</version> </dependency>

2.DNS工具類:DNSUtil.java

import com.google.common.collect.Lists;
import org.xbill.DNS.*;
import java.util.ArrayList;
import java.util.List;
public class DNSUtil {
    class DNSType{
        public static final int MX=1;
        
public static final int A=2; } public static List<String> dnsALookup(final String adr) { List<String> list= Lists.newArrayList(); try { final Lookup lookup = new Lookup(adr, Type.A); lookup.setResolver(new SimpleResolver()); lookup.setCache(
null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) { if (record instanceof ARecord) { final ARecord ptr = (ARecord) record; list.add(ptr.getAddress().getHostAddress().toString()); } } }catch (Exception e){ e.printStackTrace(); } return list; } public static List<String> mxByDomain(String host){ try { DnsThread dnsThread=new DnsThread(host,DNSType.MX); dnsThread.start(); dnsThread.join(5000); List<String> dnsList=dnsThread.getDnsList(); return dnsList; } catch (Exception e) { return new ArrayList(); } } public static List<String> reverseDNSLookup(final String adr) { List<String> list= Lists.newArrayList(); try { final Lookup lookup = new Lookup(adr, Type.MX); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) { if (record instanceof MXRecord) { final MXRecord ptr = (MXRecord) record; list.add(ptr.getTarget().toString()); } } }catch (Exception e){ e.printStackTrace(); } return list; } public static class DnsThread extends Thread { private List<String> dnsList = new ArrayList(); private String host = ""; private int type=0; DnsThread(String host,int type) { this.host = host; this.type=type; } @Override public void run() { lookup(host); } public void lookup(String host) { try { if(type==DNSType.MX) this.dnsList = reverseDNSLookup(host); if(type==DNSType.A) this.dnsList = dnsALookup(host); } catch (Exception e) { } } public List<String> getDnsList() { return dnsList; } public void setDnsList(List<String> dnsList) { this.dnsList = dnsList; } } public static void main(String[] args) { System.out.println(DNSUtil.dnsALookup("qq.com")); System.out.println(DNSUtil.mxByDomain("qq.com")); } }

2.DNS工具類(改版):DNSUtils.java

package com.lyn.util;

import org.xbill.DNS.*;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class DNSUtils{
    public static final Long TIMEOUT=3000l;
    class DNSType{
        public static final int MX=1;
        public static final int A=2;
    }

    public static List<String> mxByDomain(String host){
        try {
            long a=System.currentTimeMillis();
            DnsThread dnsThread=new DnsThread(host,DNSType.MX);
            dnsThread.start();
            dnsThread.join(TIMEOUT);
            long b=System.currentTimeMillis();
            List<String> dnsList=dnsThread.getDnsList();
            System.out.println(host+"耗時"+Double.valueOf((b-a)/1000)+"s");
            return dnsList;
        } catch (Exception e) {
           return new ArrayList<>();
        }
    }

    public static List<String> aByDomain(String host){
        try {
            long a=System.currentTimeMillis();
            DnsThread dnsThread=new DnsThread(host,DNSType.A);
            dnsThread.start();
            dnsThread.join(TIMEOUT);
            long b=System.currentTimeMillis();
            List<String> dnsList=dnsThread.getDnsList();
//            System.out.println(host+"DNS檢測耗時"+Double.valueOf((b-a)/1000)+"s");
            return dnsList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public static List<String> reverseDNSLookup(final String adr) throws Exception
    {
        List<String> resultList=searchDNSList(adr);
        if(resultList.size()>0){
            return resultList;
        }

        List<String> list= new ArrayList<>();
        final Lookup lookup = new Lookup(adr, Type.MX);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL)
            for (final Record record : records) {
                if (record instanceof MXRecord) {
                    final MXRecord ptr = (MXRecord) record;
                    list.add(ptr.getTarget().toString());
                }
            }
        return list;
    }

    /**

     * @param address 查詢地址

     * @return

     */

    public static List<String> searchDNSList(String address) {
        InitialDirContext context = null;
        String serverAddr="202.106.0.20";
        List<String> resultList = new ArrayList<String>();
        try {
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put("java.naming.factory.initial",
                    "com.sun.jndi.dns.DnsContextFactory");
            env.put("java.naming.provider.url", "dns://" + serverAddr + "/");
            env.put("com.sun.jndi.ldap.read.timeout", "5000");
            context = new InitialDirContext(env);
            String dns_attrs[] = { "MX" };
            Attributes attrs = context.getAttributes(address, dns_attrs);
            Attribute attr = attrs.get("MX");
            if (attr != null) {
                for (int i = 0; i < attr.size(); i++) {
                    String str=(String) attr.get(i);
                    if(str.indexOf(" ")!=-1)
                    resultList.add(str.split(" ")[1]);
                    else
                     resultList.add(str);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(context!=null){
                try {
                    context.close();
                } catch (NamingException e) {
                }
            }
        }
        return resultList;
    }

    public static List<String> dnsALookup(final String adr) throws Exception
    {
        List<String> list= new ArrayList<>();
        final Lookup lookup = new Lookup(adr, Type.A);
        lookup.setResolver(new SimpleResolver());
        lookup.setCache(null);
        final Record[] records = lookup.run();
        if (lookup.getResult() == Lookup.SUCCESSFUL) {
            for (final Record record : records) {
                if (record instanceof ARecord) {
                    final ARecord ptr = (ARecord) record;
                   list.add(ptr.getAddress().getHostAddress().toString());
                }
            }
        }
        return list;
    }

    public static class DnsThread extends Thread {
        private List<String> dnsList = new ArrayList<>();
        private String host = "";
        private int type=0;
         DnsThread(String host,int type) {
            this.host = host;
            this.type=type;
        }

        @Override
        public void run() {
            lookup(host);
        }

        public void lookup(String host) {
            try {
                if(type==DNSType.MX)
                    this.dnsList = reverseDNSLookup(host);
                if(type==DNSType.A)
                    this.dnsList = dnsALookup(host);
            } catch (Exception e) {
            }
        }

        public List<String> getDnsList() {
            return dnsList;
        }

        public void setDnsList(List<String> dnsList) {
            this.dnsList = dnsList;
        }
    }

 
    public static void main(String[] args){
          String[] domain=new String[]{"gmail.com","xinnet.com","wo.cn","138.com"};
          for(String ss: domain) {
              List<String> list = DNSUtils.mxByDomain(ss);
              System.out.println("得到Mx length:"+list.size());
              System.out.println("得到Mx:"+ JSONUtils.obj2json(list));
          }

//        long a=System.currentTimeMillis();
//         System.out.println(DNSUtils.mxByDomain("gmail.com"));
//        long b=System.currentTimeMillis();
//        System.out.println(b-a);
//        String domain=DNSUtils.mxByDomain("gmail.com").get(0);
//        System.out.println(DNSUtils.mxByDomain("xinnet.com"));
//        long c=System.currentTimeMillis();
//        System.out.println(c-b);
//        System.out.println(DNSUtils.aByDomain(domain));
//        long d=System.currentTimeMillis();
//        System.out.println(d-c);
//        long a=System.currentTimeMillis();
//        try {
//            System.out.println(DNSUtils.reverseDNSLookup("189.cn"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(System.currentTimeMillis()-a);
//        try {
//           List ip= DNSUtils.mxByDomain("qq");
//            System.out.println(ip);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}

獲取郵箱的DNS和MX 工具類