如何配置主机名解析以在Java中使用自定义DNS服务器?

时间:2012-07-25 10:23:02

标签: java networking dns

默认情况下,

java.net.InetAddress使用本地计算机的默认主机名解析程序解析主机名:

  

主机名到IP地址解析是通过使用本地计算机配置信息和网络命名服务(如域名系统(DNS)和网络信息服务(NIS))的组合来完成的。正在使用的特定命名服务默认情况下本地计算机配置一个。对于任何主机名,将返回其对应的IP地址。 [source]

如何在不修改本地计算机的默认主机名解析程序的情况下配置此行为?

例如,无论如何都要配置java.net.InetAddress,以便通过OpenDNS(208.67.222.222,208.67.220.220)或Google Public DNS(2001:4860:4860 :: 8888,2001:4860)解析主机名:4860 :: 8844)

或者是明确创建DNS数据包请求的唯一解决方案,通过java.net.DatagramSocketjava.net.Socket将其发送到服务器,并解析响应?

3 个答案:

答案 0 :(得分:5)

对于最多8个Java版本,这里是我编写的代码,用于在Java中硬编码foo系统名称DNS解析,以便传递测试用例。它的优点是将您的特定entrie附加到默认的Java运行时DNS解析。

我建议不要在生产中运行。使用强制反射访问和Java运行时非公共实现类!

private static final String FOO_IP = "10.10.8.111";

/** Fake "foo" DNS resolution */
@SuppressWarnings("restriction")
public static class MyHostNameService implements sun.net.spi.nameservice.NameService {
    @Override
    public InetAddress[] lookupAllHostAddr(String paramString) throws UnknownHostException {
        if ("foo".equals(paramString) || "foo.domain.tld".equals(paramString)) {
            final byte[] arrayOfByte = sun.net.util.IPAddressUtil.textToNumericFormatV4(FOO_IP);
            final InetAddress address = InetAddress.getByAddress(paramString, arrayOfByte);
            return new InetAddress[] { address };
        } else {
            throw new UnknownHostException();
        }
    }
    @Override
    public String getHostByAddr(byte[] paramArrayOfByte) throws UnknownHostException {
        throw new UnknownHostException();
    }
}

static {
    // Force to load fake hostname resolution for tests to pass
    try {
        List<sun.net.spi.nameservice.NameService> nameServices =
            (List<sun.net.spi.nameservice.NameService>)
            org.apache.commons.lang3.reflect.FieldUtils.readStaticField(InetAddress.class, "nameServices", true);
        nameServices.add(new MyHostNameService());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

希望这会有所帮助,但同样要小心使用!

答案 1 :(得分:3)

Java 提供了一个新的系统参数 jdk.net.hosts.file,用于添加自定义 DNS 记录,如 source code

 * The HostsFileNameService provides host address mapping
 * by reading the entries in a hosts file, which is specified by
 * {@code jdk.net.hosts.file} system property
 *
 * <p>The file format is that which corresponds with the /etc/hosts file
 * IP Address host alias list.
 *
 * <p>When the file lookup is enabled it replaces the default NameService
 * implementation
 *
 * @since 9
 */
private static final class HostsFileNameService implements NameService {

示例:我们可以使用 JVM Option 启动 Java 应用程序

  • -Djdk.net.hosts.file=/path/to/alternative/hosts_file

hosts_file 内容可能在哪里:

127.0.0.1    myserver-a.local
10.0.5.10    myserver-b.local
192.168.0.1  myserver-c.local

它将根据 hosts_file

执行 DNS 技巧

答案 2 :(得分:1)

使用以下接口并允许访问java.net。*可以使用自己的DNS提供程序与JDK8和JDK9。新的Provider通过安装 &#34; INameService.install(new MyNameService());&#34;

public interface INameService extends InvocationHandler {
    public static void install(final INameService dns) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
        final Class<?> inetAddressClass = InetAddress.class;
        Object neu;
        Field nameServiceField;
        try {
            final Class<?> iface = Class.forName("java.net.InetAddress$NameService");
            nameServiceField = inetAddressClass.getDeclaredField("nameService");
            neu = Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] { iface }, dns);
        } catch(final ClassNotFoundException|NoSuchFieldException e) {
            nameServiceField = inetAddressClass.getDeclaredField("nameServices");
            final Class<?> iface = Class.forName("sun.net.spi.nameservice.NameService");
            neu = Arrays.asList(Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] { iface }, dns));
        }
        nameServiceField.setAccessible(true);
        nameServiceField.set(inetAddressClass, neu);
    }

    /**
     * Lookup a host mapping by name. Retrieve the IP addresses associated with a host
     *
     * @param host the specified hostname
     * @return array of IP addresses for the requested host
     * @throws UnknownHostException  if no IP address for the {@code host} could be found
     */
    InetAddress[] lookupAllHostAddr(final String host) throws UnknownHostException;

    /**
     * Lookup the host corresponding to the IP address provided
     *
     * @param addr byte array representing an IP address
     * @return {@code String} representing the host name mapping
     * @throws UnknownHostException
     *             if no host found for the specified IP address
     */
    String getHostByAddr(final byte[] addr) throws UnknownHostException;

    @Override default public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
        switch(method.getName()) {
        case "lookupAllHostAddr": return lookupAllHostAddr((String)args[0]);
        case "getHostByAddr"    : return getHostByAddr    ((byte[])args[0]);
        default                 :
            final StringBuilder o = new StringBuilder();
            o.append(method.getReturnType().getCanonicalName()+" "+method.getName()+"(");
            final Class<?>[] ps = method.getParameterTypes();
            for(int i=0;i<ps.length;++i) {
                if(i>0) o.append(", ");
                o.append(ps[i].getCanonicalName()).append(" p").append(i);
            }
            o.append(")");
            throw new UnsupportedOperationException(o.toString());
        }
    }
}