java添加dns缓存记录
是否可以从java向dns缓存添加一条记录?或者我必须使用 JNI 吗?
Is it possible to add a record to the dns cache from java? Or will I have to use the JNI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接配置java来使用代理呢?
或
将这样做。
Why don't you just configure java for using the proxy?
or
will do it.
Java DNS 缓存的实现方式对您的代码应该基本上是透明的。您可以阅读 InetAddress 的 javadoc了解有关如何配置缓存的更多信息。但由于它充当 JVM 范围内的透明代理,因此添加缓存条目的最佳方法就是实际发出指向您想要缓存信息的域的请求。
出于好奇,您的用例是什么?
Java DNS caching is implemented in a manner that should be mostly transparent to your code. You can read the javadoc of InetAddress to learn more about how you can configure the cache. But since it serves as a JVM-wide transparent proxy, the best way you have to add a cache entry would simply be to actually issue a request directed to the domain you want to cache the info for.
Out of curiosity, what's your use case?
假设您正在讨论 Java 应用程序使用的 DNS 缓存,那么在这两种情况下答案都是“否”。
缓存在java.net.InetAddress类中实现;请参阅此处获取源代码。正如您所看到的,缓存是使用私有静态属性实现的,并且涉及的所有类和方法都是私有的或包私有的。简而言之,获取缓存的唯一方法是使用令人讨厌的反射技巧来破坏 Java 访问规则。
由于这是用纯 Java 实现的,因此 JNI 没有帮助。
编辑
不幸的是,上面的链接不再指向OpenJDK代码:-(。
后续
回复:这些评论。
无法从 Java 直接将内容植入系统 DNS 缓存中。事实上,我什至不认为 Java 使用系统 DNS 缓存。
但是,对于您尝试执行的操作,还有更好的替代方案:
让您的用户将其浏览器配置为使用“autoproxy.pac”文件来确定要使用的代理。 IMO,这是最好的选择。
将您想要选择性代理的主机条目放入“/etc/hosts”中,并配置(使用“/etc/host.conf”)您的本地解析器以在与主机对话之前查看“/etc/hosts” DNS 服务器。不幸的是(如缓存中毒),这会用虚假条目“污染”您的 DNS,在使用 HTTP / HTTPS 以外的服务时可能会导致问题。
最后,您可能应该重新考虑对用户完全透明地执行此操作的目标:
如果您这样做是为了实施某些公司安全或互联网访问规则,那么人们可以“绕过”您在此级别实施的任何措施。 (您最好对网络设置防火墙并强制使用代理进行外部访问......或类似的东西。)
如果你只是想实现一个有用的服务,你应该使用 autoproxy.pac 方法这为用户提供了 1) 正在发生的事情的可见性(如果他们愿意查看),以及 2) 选择加入或退出的选项。
如果您正在尝试做其他事情...
Assuming that you are talking about the DNS cache that Java applications use, the answer is No in both cases.
The cache is implemented in the java.net.InetAddress class; refer here for the source code. As you can see, the cache is implemented using private static attributes and all of the classes and methods involved are private or package private. In short, the only way you could get at the cache would be by using nasty reflection tricks to subvert the Java access rules.
Since this is implemented in pure Java, JNI won't help.
EDIT
Unfortunately, the link above no longer points to the OpenJDK code :-(.
FOLLOWUP
Re: these comments.
There is no way to directly plant things in the system DNS cache from Java. Indeed, I don't even think that Java uses the system DNS cache.
But there are better alternatives to what you are trying to do:
Get your users to configure their browsers to use an 'autoproxy.pac' file to determine which proxies to use. IMO, this is the best option.
Put entries for the hosts that you want to selectively proxy into "/etc/hosts" and configure (using "/etc/host.conf") your local resolver to look in "/etc/hosts" before talking to the DNS server. Unfortunately (like cache poisoning) this "pollutes" your DNS with bogus entries, can cause problems when using services other than HTTP / HTTPS.
Finally, you probably should rethink your goal of doing this totally transparently to your users:
If you are doing this to implement some company security or internet access rules, people can "route around" any measures you implement at this level. (You'd be better off firewalling your network and forcing to use a proxy for external access ... or something like that.)
If you are just trying to implement a useful service, you should use the autoproxy.pac approach which gives the users 1) visibility of what is going on (if they care to look), and 2) the option of opting in or out.
If you are trying to do something else ...