下面列出了怎么用org.xbill.DNS.TXTRecord的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Query a text record for the given domain from DNS
*
* @param domain the domain to lookup the DNS for a text record
* @return the text record content, if found, null otherwise
*/
@SuppressWarnings ("unchecked")
public String queryText (String domain) {
String rc = null;
Record[] r = query (domain, Type.TXT);
if (r != null) {
rc = "";
for (int n = 0; n < r.length; ++n) {
List <String> answ = ((TXTRecord) r[n]).getStrings ();
for (int m = 0; m < answ.size (); ++m) {
String s = answ.get (m);
if ((s != null) && (s.length () > 0)) {
rc += s;
}
}
}
}
return rc;
}
private List<String> parse(Record record) {
TXTRecord txt = (TXTRecord) record;
List<String> subnets = new ArrayList<>();
@SuppressWarnings("unchecked")
List<String> lines = txt.getStrings();
for (String line : lines) {
String[] parts = StringUtils.split(line, " ");
if (parts.length == 2) {
if (log.isDebugEnabled()) {
log.debug("Add Google resolver IP range: " + parts[0]);
}
subnets.add(parts[0]);
}
}
if (subnets.isEmpty()) {
log.error("No Google resolver addresses found");
}
return subnets;
}
@Override
public Collection<String> findTXTRecords(String hostname) {
TimeMetric timeMetric = metricFactory.timer("findTXTRecords");
List<String> txtR = new ArrayList<>();
Record[] records = lookupNoException(hostname, Type.TXT, "TXT");
try {
if (records != null) {
for (Record record : records) {
TXTRecord txt = (TXTRecord) record;
txtR.add(txt.rdataToString());
}
}
return txtR;
} finally {
timeMetric.stopAndPublish();
}
}
private String concatenateTxtRecordValues(Record[] records) {
if (records == null || records.length == 0)
return null;
StringBuilder builder = new StringBuilder();
for (Record record : records) {
TXTRecord txtRecord = (TXTRecord) record;
if (builder.length() != 0)
builder.append(EOL);
for (Object string : txtRecord.getStrings()) {
if (builder.length() != 0)
builder.append(EOL);
builder.append(string);
}
}
return builder.toString();
}
@Override
protected Boolean doInBackground(String... args) {
//main();
if (args.length != 1) return false;
String name = args[0];
if ((name == null) || (name.isEmpty()))
return false; //pointless trying to lookup nothing
Timber.d("Resolving %s", name);
try {
SimpleResolver sr = new SimpleResolver(DNSSEC_SERVERS[new Random().nextInt(DNSSEC_SERVERS.length)]);
ValidatingResolver vr = new ValidatingResolver(sr);
vr.setTimeout(0, DNS_LOOKUP_TIMEOUT);
vr.loadTrustAnchors(new ByteArrayInputStream(ROOT.getBytes("ASCII")));
Record qr = Record.newRecord(Name.fromConstantString(name + "."), Type.TXT, DClass.IN);
Message response = vr.send(Message.newQuery(qr));
final int rcode = response.getRcode();
if (rcode != Rcode.NOERROR) {
Timber.i("Rcode: %s", Rcode.string(rcode));
for (RRset set : response.getSectionRRsets(Section.ADDITIONAL)) {
if (set.getName().equals(Name.root) && set.getType() == Type.TXT
&& set.getDClass() == ValidatingResolver.VALIDATION_REASON_QCLASS) {
Timber.i("Reason: %s", ((TXTRecord) set.first()).getStrings().get(0));
}
}
return false;
} else {
dnssec = response.getHeader().getFlag(Flags.AD);
for (Record record : response.getSectionArray(Section.ANSWER)) {
if (record.getType() == Type.TXT) {
txts.addAll(((TXTRecord) record).getStrings());
}
}
}
} catch (IOException | IllegalArgumentException ex) {
return false;
}
return true;
}
public static Long getASN(final InetAddress adr)
{
try
{
final Name postfix;
if (adr instanceof Inet6Address)
postfix = Name.fromConstantString("origin6.asn.cymru.com");
else
postfix = Name.fromConstantString("origin.asn.cymru.com");
final Name name = getReverseIPName(adr, postfix);
System.out.println("lookup: " + name);
final Lookup lookup = new Lookup(name, Type.TXT);
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 TXTRecord)
{
final TXTRecord txt = (TXTRecord) record;
@SuppressWarnings("unchecked")
final List<String> strings = txt.getStrings();
if (strings != null && !strings.isEmpty())
{
final String result = strings.get(0);
final String[] parts = result.split(" ?\\| ?");
if (parts != null && parts.length >= 1)
return new Long(parts[0]);
}
}
}
catch (final Exception e)
{
}
return null;
}
public static String getASName(final long asn)
{
try
{
final Name postfix = Name.fromConstantString("asn.cymru.com.");
final Name name = new Name(String.format("AS%d", asn), postfix);
System.out.println("lookup: " + name);
final Lookup lookup = new Lookup(name, Type.TXT);
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 TXTRecord)
{
final TXTRecord txt = (TXTRecord) record;
@SuppressWarnings("unchecked")
final List<String> strings = txt.getStrings();
if (strings != null && !strings.isEmpty())
{
System.out.println(strings);
final String result = strings.get(0);
final String[] parts = result.split(" ?\\| ?");
if (parts != null && parts.length >= 1)
return parts[4];
}
}
}
catch (final Exception e)
{
}
return null;
}
public static String getAScountry(final long asn)
{
try
{
final Name postfix = Name.fromConstantString("asn.cymru.com.");
final Name name = new Name(String.format("AS%d", asn), postfix);
System.out.println("lookup: " + name);
final Lookup lookup = new Lookup(name, Type.TXT);
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 TXTRecord)
{
final TXTRecord txt = (TXTRecord) record;
@SuppressWarnings("unchecked")
final List<String> strings = txt.getStrings();
if (strings != null && !strings.isEmpty())
{
final String result = strings.get(0);
final String[] parts = result.split(" ?\\| ?");
if (parts != null && parts.length >= 1)
return parts[1];
}
}
}
catch (final Exception e)
{
}
return null;
}
/**
* Adapted from https://code.google.com/p/asmack/source/browse/src/custom/org/jivesoftware/smack/util/DNSUtil.java
*
* @param domain
* @return
* @throws TextParseException
*/
@SuppressWarnings("unchecked")
private static String resolveAPITXT(String domain) throws TextParseException {
Lookup lookup = new Lookup(TXT_PREFIX + domain, Type.TXT);
Record recs[] = lookup.run();
if (recs == null) {
throw new RuntimeException("Could not lookup domain.");
}
Map<String, String> stringMap = null;
for (Record rec : recs) {
String rData = rec.rdataToString().replaceAll("\"", "");
List<String> rDataTokens = Arrays.asList(rData.split("\\s+"));
TXTRecord record = new TXTRecord(rec.getName(), rec.getDClass(),
rec.getTTL(), rDataTokens);
List<String> strings = record.getStrings();
if (strings != null && strings.size() > 0) {
stringMap = parseStrings(strings);
break;
}
}
if (stringMap == null) {
throw new RuntimeException("Domain has no TXT records for buddycloud.");
}
String host = stringMap.get("host");
String protocol = stringMap.get("protocol");
String path = stringMap.get("path");
String port = stringMap.get("port");
path = path == null || path.equals("/") ? "" : path;
port = port == null ? "" : port;
return protocol + "://" + host + ":" + port + path;
}
public TXTRecord getTXTRecord(String hostName) {
return null;
}