本文整理汇总了Java中org.xbill.DNS.SimpleResolver类的典型用法代码示例。如果您正苦于以下问题:Java SimpleResolver类的具体用法?Java SimpleResolver怎么用?Java SimpleResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SimpleResolver类属于org.xbill.DNS包,在下文中一共展示了SimpleResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resolve
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
public static String resolve(String host, int addrType) {
try {
Lookup lookup = new Lookup(host, addrType);
SimpleResolver resolver = new SimpleResolver("114.114.114.114");
resolver.setTimeout(5);
lookup.setResolver(resolver);
Record[] result = lookup.run();
if (result == null) return null;
List<Record> records = java.util.Arrays.asList(result);
java.util.Collections.shuffle(records);
for (Record record : records) {
if (addrType == Type.A) {
return ((ARecord) record).getAddress().getHostAddress();
} else if (addrType == Type.AAAA) {
return ((AAAARecord) record).getAddress().getHostAddress();
}
}
} catch (Exception ex) {
return null;
}
return null;
}
示例2: reverseLookupByAddress
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
/**
* reverse lookup an IP address using a specified DNS host and port
*
* @param resolverHost name server hostname or IP address
* @param resolverPort name server port
* @param address the IP address to reverse lookup
* @return a comma separated list of names or an empty string when unable to resolve
*/
public String reverseLookupByAddress(String resolverHost, int resolverPort, InetAddress address) {
try {
SimpleResolver resolver = new SimpleResolver(resolverHost);
resolver.setPort(resolverPort);
Lookup lookup = new Lookup(fromAddress(address), PTR);
Record[] records = lookup.run();
if (records != null) {
List<String> addresses =
of(records)
.filter(it -> it instanceof PTRRecord)
.map(it -> ((PTRRecord) it).getTarget().toString())
.collect(toList());
return collectionToCommaDelimitedString(addresses);
} else {
return "";
}
} catch (UnknownHostException e) {
log.warn("unable to resolve using SRV record " + address, e);
return "";
}
}
示例3: init
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
private void init() {
if (mInitialized) return; else mInitialized = true;
try {
// configure the resolvers, starting with the default ones (based on the current network connection)
Resolver defaultResolver = Lookup.getDefaultResolver();
// use Google's public DNS services
Resolver googleFirstResolver = new SimpleResolver("8.8.8.8");
Resolver googleSecondResolver = new SimpleResolver("8.8.4.4");
// also try using Amazon
Resolver amazonResolver = new SimpleResolver("205.251.198.30");
Lookup.setDefaultResolver(new ExtendedResolver(new Resolver[]{
googleFirstResolver, googleSecondResolver, amazonResolver, defaultResolver }));
} catch (UnknownHostException e) {
Log.w(TAG, "Couldn't initialize custom resolvers");
}
}
示例4: testDnsJavaWithDnsServer
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
@Test
public void testDnsJavaWithDnsServer() throws TextParseException, UnknownHostException {
final Lookup l = new Lookup("example.com", Type.AAAA);
final SimpleResolver resolver = new SimpleResolver("::1");
resolver.setPort(9153);
l.setResolver(resolver);
l.run();
System.out.println("result: " + l.getResult());
final Record[] answers = l.getAnswers();
assertEquals(answers.length, 1);
final Record record = answers[0];
System.err.println(record.getTTL());
if(l.getResult() == Lookup.SUCCESSFUL) {
System.out.println(l.getAnswers()[0].rdataToString());
}
assertTrue(l.getResult() == Lookup.SUCCESSFUL);
}
示例5: reverseDNSLookup
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
public static String reverseDNSLookup(final InetAddress adr)
{
try
{
final Name name = ReverseMap.fromAddress(adr);
final Lookup lookup = new Lookup(name, Type.PTR);
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 PTRRecord)
{
final PTRRecord ptr = (PTRRecord) record;
return ptr.getTarget().toString();
}
}
catch (final Exception e)
{
}
return null;
}
示例6: resolveHostByName
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
/**
* resolves an A record by its name using a specified DNS host and port
*
* @param resolverHost name server hostname or IP address
* @param resolverPort name server port
* @param name the DNS name of the A record - the name to resolve
* @return a comma separated list of IP addresses or an empty string when unable to resolve
*/
public String resolveHostByName(String resolverHost, int resolverPort, String name) {
try {
SimpleResolver resolver = new SimpleResolver(resolverHost);
resolver.setPort(resolverPort);
Lookup lookup = new Lookup(name, A);
Record[] records = lookup.run();
if (records != null) {
List<String> addresses =
of(records)
.filter(it -> it instanceof ARecord)
.map(it -> ((ARecord) it).getAddress().getHostAddress())
.collect(toList());
return collectionToCommaDelimitedString(addresses);
} else {
return "";
}
} catch (UnknownHostException | TextParseException e) {
log.warn("unable to resolve using A record " + name, e);
return "";
}
}
示例7: instantiateResolver
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
/**
* Private helper to instantiate a DNS <code>Resolver</code> by the provided Server.
*
* @param dnsSec <code>true</code> iff DNSSEC is enabled
* @param trustAnchor Public cryptographic to validate against
* @param server Server to use as DNS resolver
*
* @return <code>null</code> in case the <code>Resolver</code> cannot be instantiated
*/
private static Resolver instantiateResolver(boolean dnsSec, String trustAnchor, String server)
{
try {
Resolver resolver = new SimpleResolver(server);
if (!dnsSec) {
return resolver;
}
ValidatingResolver validating = new ValidatingResolver(resolver);
validating.loadTrustAnchors(new ByteArrayInputStream(trustAnchor.getBytes(StandardCharsets.UTF_8)));
return validating;
} catch (IOException e) {
return null;
}
}
示例8: testNoAnswer
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
@Test
@JUnitDNSServer(port=9153, zones={})
public void testNoAnswer() throws Exception {
final Lookup l = new Lookup("example.com", Type.AAAA);
final SimpleResolver resolver = new SimpleResolver("::1");
resolver.setPort(9153);
l.setResolver(resolver);
l.run();
System.out.println("result: " + l.getResult());
final Record[] answers = l.getAnswers();
assertNotNull(answers);
assertEquals(answers.length, 1);
final Record record = answers[0];
System.err.println(record.getTTL());
if(l.getResult() == Lookup.SUCCESSFUL) {
System.out.println(l.getAnswers()[0].rdataToString());
}
assertTrue(l.getResult() == Lookup.SUCCESSFUL);
}
示例9: testNoAnswer
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
@Test
@JUnitDNSServer(port=9153, zones={})
public void testNoAnswer() throws Exception {
final Lookup l = new Lookup("example.com", Type.AAAA);
final SimpleResolver resolver = new SimpleResolver("::1");
resolver.setPort(9153);
l.setResolver(resolver);
l.run();
System.out.println("result: " + l.getResult());
final Record[] answers = l.getAnswers();
assertEquals(answers.length, 1);
final Record record = answers[0];
System.err.println(record.getTTL());
if(l.getResult() == Lookup.SUCCESSFUL) {
System.out.println(l.getAnswers()[0].rdataToString());
}
assertTrue(l.getResult() == Lookup.SUCCESSFUL);
}
示例10: getASN
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
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].split(" ")[0]);
}
}
}
catch (final Exception e)
{
}
return null;
}
示例11: getASName
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
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;
}
示例12: getAScountry
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
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;
}
示例13: process
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
@Override
public void process(Exchange exchange) throws Exception {
String server = exchange.getIn().getHeader(DnsConstants.DNS_SERVER, String.class);
SimpleResolver resolver = new SimpleResolver(server);
int type = Type.value(exchange.getIn().getHeader(DnsConstants.DNS_TYPE, String.class));
if (type == -1) {
// default: if unparsable value given, use A.
type = Type.A;
}
String dclassValue = exchange.getIn().getHeader(DnsConstants.DNS_CLASS, String.class);
if (dclassValue == null) {
dclassValue = "";
}
int dclass = DClass.value(dclassValue);
if (dclass == -1) {
// by default, value is IN.
dclass = DClass.IN;
}
Name name = Name.fromString(exchange.getIn().getHeader(DnsConstants.DNS_NAME, String.class), Name.root);
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = resolver.send(query);
exchange.getIn().setBody(response);
}
示例14: process
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
@Override
public void process(Exchange exchange) throws Exception {
SimpleResolver resolver = new SimpleResolver();
int type = Type.TXT;
Name name = Name.fromString(String.valueOf(exchange.getIn().getHeader(DnsConstants.TERM)) + ".wp.dg.cx", Name.root);
Record rec = Record.newRecord(name, type, DClass.IN);
Message query = Message.newQuery(rec);
Message response = resolver.send(query);
Record[] records = response.getSectionArray(Section.ANSWER);
if (records.length > 0) {
exchange.getIn().setBody(records[0].rdataToString());
} else {
exchange.getIn().setBody(null);
}
}
示例15: resolveTextByName
import org.xbill.DNS.SimpleResolver; //导入依赖的package包/类
/**
* resolves the TXT field for a given name using a specified DNS host and port. This is useful, for example,
* if you want to resolve abusers with: <a href="https://abusix.com/contactdb.html">https://abusix.com/contactdb.html</a>
*
* @param resolverHost name server hostname or IP address
* @param resolverPort name server port
* @param name the DNS name of the TXT record - the name to resolve
* @return the resolved text
*/
public String resolveTextByName(String resolverHost, int resolverPort, String name) {
try {
SimpleResolver resolver = new SimpleResolver(resolverHost);
resolver.setPort(resolverPort);
Lookup lookup = new Lookup(name, TXT);
Record[] records = lookup.run();
if (records != null) {
List<String> addresses =
of(records)
.filter(it -> it instanceof TXTRecord)
.map(it -> collectionToCommaDelimitedString(((TXTRecord) it).getStrings()))
.collect(toList());
return collectionToCommaDelimitedString(addresses);
} else {
return "";
}
} catch (UnknownHostException | TextParseException e) {
log.warn("unable to resolve using TXT record " + name, e);
return "";
}
}