Java Code Examples for org.xbill.DNS.Lookup#setResolver()

The following examples show how to use org.xbill.DNS.Lookup#setResolver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleResolverCheck.java    From entrada with GNU General Public License v3.0 7 votes vote down vote up
@Override
protected List<String> fetch() {

  try {
    Resolver resolver = new SimpleResolver();
    // dns resolvers may take a long time to return a response.
    resolver.setTimeout(timeout);
    Lookup l =
        new Lookup(StringUtils.endsWith(hostname, ".") ? hostname : hostname + ".", Type.TXT);
    // always make sure the cache is empty
    l.setCache(new Cache());
    l.setResolver(resolver);
    Record[] records = l.run();
    if (records != null && records.length > 0) {
      return parse(records[0]);
    }
  } catch (Exception e) {
    log.error("Problem while adding Google resolvers, continue without", e);
  }

  log.error("No Google resolver addresses found");
  return Collections.emptyList();
}
 
Example 2
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: DNSResolver.java    From burp-javascript-security-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Follow the CNAME breadcrumb trail and find any which can't resolve
 * @param hostName a string of the hostname, or fqdn, to lookup 
 * @return a set of strings which list the CNAME entries which could not be resolved
 */
public Set<String> getBadCnames(String hostName){
    Set<String> retval = new TreeSet<String>();
    try {
        Lookup thisLookup = new Lookup(hostName, CNAME);
        thisLookup.setResolver(myResolver);
        Record[] results = thisLookup.run();
        if (results != null){
            List<Record> records = Arrays.asList(results);
            for (Record record : records){
                CNAMERecord thisRecord = (CNAMERecord) record;
                String target = thisRecord.getTarget().toString();
                if (hasRecordsOfType(target, CNAME)){
                    // check for more cnames down the tree
                    retval.addAll(getBadCnames(target));
                } else {
                    if (!(hasRecordsOfType(target, A) || hasRecordsOfType(target, AAAA))){
                        // This one doesn't point to anything
                        retval.add(target);
                    }
                }
            }
        }
    }
    catch (TextParseException e){
        System.err.println("[SRI][-] There was an error parsing the name " + hostName);
    }
    return retval;
}
 
Example 4
Source File: Utils.java    From ShadowsocksRR with Apache License 2.0 5 votes vote down vote up
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 || result.length == 0) {
            return null;
        }

        List<Record> records = new ArrayList<>(Arrays.asList(result));
        Collections.shuffle(records);
        for (Record r : records) {
            switch (addrType) {
                case Type.A:
                    return ((ARecord) r).getAddress().getHostAddress();
                case Type.AAAA:
                    return ((AAAARecord) r).getAddress().getHostAddress();
                default:
                    break;
            }
        }
    } catch (Exception e) {
        VayLog.e(TAG, "resolve", e);
        app.track(e);
    }
    return null;
}
 
Example 5
Source File: Utils.java    From Maying with Apache License 2.0 5 votes vote down vote up
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 || result.length == 0) {
            return null;
        }

        List<Record> records = new ArrayList<>(Arrays.asList(result));
        Collections.shuffle(records);
        for (Record r : records) {
            switch (addrType) {
                case Type.A:
                    return ((ARecord) r).getAddress().getHostAddress();
                case Type.AAAA:
                    return ((AAAARecord) r).getAddress().getHostAddress();
                default:
                    break;
            }
        }
    } catch (Exception e) {
        VayLog.e(TAG, "resolve", e);
        ShadowsocksApplication.app.track(e);
    }
    return null;
}
 
Example 6
Source File: DNSJavaService.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up DNS records of the specified type for the specified name.
 * <p/>
 * This method is a public wrapper for the private implementation method
 *
 * @param namestr  the name of the host to be looked up
 * @param type     the type of record desired
 * @param typeDesc the description of the record type, for debugging purpose
 */
protected Record[] lookup(String namestr, int type, String typeDesc) throws TemporaryResolutionException {
    // Name name = null;
    try {
        // name = Name.fromString(namestr, Name.root);
        Lookup l = new Lookup(namestr, type);

        l.setCache(cache);
        l.setResolver(resolver);
        l.setCredibility(dnsCredibility);
        l.setSearchPath(searchPaths);
        Record[] r = l.run();

        try {
            if (l.getResult() == Lookup.TRY_AGAIN) {
                throw new TemporaryResolutionException("DNSService is temporary not reachable");
            } else {
                return r;
            }
        } catch (IllegalStateException ise) {
            // This is okay, because it mimics the original behaviour
            // TODO find out if it's a bug in DNSJava
            LOGGER.warn("Error determining result ", ise);
            throw new TemporaryResolutionException("DNSService is temporary not reachable");
        }

        // return rawDNSLookup(name, false, type, typeDesc);
    } catch (TextParseException tpe) {
        // TODO: Figure out how to handle this correctly.
        LOGGER.error("Couldn't parse name {}", namestr, tpe);
        return null;
    }
}
 
Example 7
Source File: SimpleLookupFactory.java    From dns-java with Apache License 2.0 5 votes vote down vote up
@Override
public Lookup forName(String fqdn) {
  try {
    final Lookup lookup = new Lookup(fqdn, Type.SRV, DClass.IN);
    if (resolver != null) {
      lookup.setResolver(resolver);
    }
    return lookup;
  } catch (TextParseException e) {
    throw new DnsException("unable to create lookup for name: " + fqdn, e);
  }
}
 
Example 8
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: Helperfunctions.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 3 votes vote down vote up
private Lookup testLookup(String thefqdn) throws TextParseException {
  Lookup result = new Lookup(thefqdn, Type.SRV);

  result.setResolver(xbillResolver);

  return result;
}