If you want to modify DNS resolving, you are out of luck with java.
The SPI NameServiceDefinition was silently removed in Java 9+.
Even with this definition, it was impossible to just extend the default NameService
implementation.
If you want to modify the behaviour of the existing InetAddress::NameService
implementation, this agent will let you do this via ugly reflection and will break in later java versions as well.
To use this agent, there are three steps to this:
-
Compilation or Download
-
Mount the agent with default configuration
-
Configure or extend the agent
You can compile this agent using ./mvnw package
.
All you need is java11+ for compilation.
You can use the file ./agent/target/nameserviceagent-agent-1.0.0-SNAPSHOT.jar
.
Alternative: Download the nameserviceagent-agent.jar from the releases.
You can mount the agent using java -javaagent:nameserviceagent-agent.jar <yourtool>
.
Currently these options are available:
key | type | default value | description |
---|---|---|---|
|
System Property ( |
|
Switch the implementation of the replaced/injected naming service. |
|
System Property ( |
|
Timeout for the DefaultSequentialReachableNameService when a remote host is considered unavailable. |
The default configuration will use the default nameService (e.g. system dns or host file[4]). However, while the default implementation will just use the 1st IP returned, this implementation will in fact check all the IP addresses.
This DNS NameResolver implementation will always resolve to loopback (either 127.0.0.1
or ::1
).
This configuration can be activated like this:
java
-javaagent:${localRepositoryPath}/io/github/bmhm/nameserviceagent/nameserviceagent-agent/@project.version@/nameserviceagent-agent-@project.version@.jar
-Dnameserviceagent.implementation=io.github.bmhm.nameserviceagent.agent.nameservice.AlwaysLocalhostLoopbackNameService
If you want to write your own DNS Name Resolver, just extend AbstractProxyNameService
and make sure you also have the public constructor with the NameService interface.
Provide the full class name via the system property nameserviceagent.implementation
.
This agent is not intended for production code. It will probably fail for more recent java releases which do not allow acces via reflection.