Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package foo;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.CNAMERecord;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.NSRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.Type;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
import com.google.common.net.InternetDomainName;
public class DNSTest {
private static Joiner joiner = Joiner.on(",").skipNulls();
public static void main(String[] args) throws IOException {
List<String> domains = Files.readLines(new File("C:\\workspace\\Test\\top500.domains.09.16.csv"), Charsets.UTF_8);
domains.remove(0); //remove header row
System.out.println("domain,ttl,aaaa-ttl,cname-ttl,miltiple-ttls,ns-root,ns-ttl,a-count,aaaa-count,a-records,aaaa-records,ns-records,cname");
Set<Exception> errors = new HashSet<>();
for (String domainRecord : domains) {
String[] details = domainRecord.split(",");
String domain = details[1].replace("/", "").replace("\"", "");
Set<Long> aTtls = new HashSet<>();
Set<Long> aaaaTtls = new HashSet<>();
Set<Long> nsTtls = new HashSet<>();
Long cnameTtl = null;
Set<String> aAddresses = new HashSet<>();
Set<String> aaaaAddresses = new HashSet<>();
Set<String> nsAddresses = new HashSet<>();
String cnameAddress = "";
try {
Record[] cnames = new Lookup(domain, Type.NS).run();
if (cnames != null) {
for (Record record : cnames) {
NSRecord rec = (NSRecord) record;
nsTtls.add(rec.getTTL());
nsAddresses.add(rec.getTarget().toString());
}
}
} catch (Exception ex) {
errors.add(ex);
}
Resolver resolver;
try {
resolver = new ExtendedResolver(nsAddresses.toArray(new String[0]));
} catch (Exception ex) {
resolver = new SimpleResolver();
}
try {
Lookup lookup = new Lookup(domain, Type.A);
lookup.setResolver(resolver);
Record[] rs = lookup.run();
if (rs != null) {
for (Record record : rs) {
ARecord rec = (ARecord) record;
aTtls.add(rec.getTTL());
aAddresses.add(rec.getAddress().getHostAddress());
}
}
} catch (Exception ex) {
errors.add(ex);
}
try {
Lookup lookup = new Lookup(domain, Type.AAAA);
lookup.setResolver(resolver);
Record[] ipv6 = lookup.run();
if (ipv6 != null) {
for (Record record : ipv6) {
AAAARecord rec = (AAAARecord) record;
aaaaTtls.add(rec.getTTL());
aaaaAddresses.add(rec.getAddress().getHostAddress());
}
}
} catch (Exception ex) {
errors.add(ex);
}
try {
Lookup lookup = new Lookup(domain, Type.CNAME);
lookup.setResolver(resolver);
Record[] cnames = lookup.run();
if (cnames != null) {
for (Record record : cnames) {
CNAMERecord rec = (CNAMERecord) record;
cnameTtl = rec.getTTL();
cnameAddress = rec.getTarget().toString();
}
}
} catch (Exception ex) {
errors.add(ex);
}
List<String> line = new ArrayList<>();
line.add(domain);
line.add(getTtl(aTtls));
line.add(getTtl(aaaaTtls));
line.add(cnameTtl != null ? cnameTtl.toString() : "");
line.add(String.valueOf(hasMultipleTtls(aTtls, aaaaTtls)));
line.add(getNSRoot(nsAddresses, errors));
line.add(getTtl(nsTtls));
line.add(String.valueOf(aAddresses.size()));
line.add(String.valueOf(aaaaAddresses.size()));
line.add("\"" + joiner.join(aAddresses) + "\"");
line.add("\"" + joiner.join(aaaaAddresses) + "\"");
line.add("\"" + joiner.join(nsAddresses) + "\"");
line.add(cnameAddress);
System.out.println(joiner.join(line));
}
System.out.println("-------");
for (Exception ex : errors) {
ex.printStackTrace();
}
}
private static String getNSRoot(Set<String> nsAddresses, Set<Exception> errors) {
if (nsAddresses.isEmpty()) {
return "";
}
try {
return InternetDomainName.from(nsAddresses.iterator().next()).topPrivateDomain().name();
} catch (Exception ex) {
errors.add(ex);
return nsAddresses.iterator().next();
}
}
private static boolean hasMultipleTtls(Set<Long> aTtls, Set<Long> aaaaTtls) {
if (aTtls.size() > 1 || aaaaTtls.size() > 1) {
return true;
}
if (!aTtls.isEmpty() && !aaaaTtls.isEmpty()) {
if (aTtls.iterator().next().longValue() != aaaaTtls.iterator().next().longValue()) {
return true;
}
}
return false;
}
private static String getTtl(Set<Long> ttls) {
if (ttls.isEmpty()) {
return "";
}
return ttls.iterator().next().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment