- Import Java.io.BufferedReader;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.io.InputStreamReader;
- Import Java.lang.reflect.Field;
- Import Java.lang.reflect.Method;
- Import java.net.HttpURLConnection;
- Import java.net.InetAddress;
- Import java.net.MalformedURLException;
- Import Java.net.URL;
- Import java.net.UnknownHostException;
- Import Java.util.HashMap;
- Import java.util.List;
- Import Java.util.Map;
- Import Sun.net.spi.nameservice.NameService;
- public class Javadnscachetest {
- /**
- * Tested, two ways can be in Windows environment, Linux environment to be tested
- *
- * @param args
- * @throws Exception
- */
- public static void Main (string[] args) throws Exception {
- /* 1. Dynamic Replacement Addresscache */
- Changednswithaddresscache ();
- /* 1. Dynamic Agent Nameservice */
- Changednswithnameservice ();
- }
- public static void Changednswithnameservice () throws Exception {
- /* 1. Get reflexive class */
- class<?> addressclass = Inetaddress.class;
- /* 2. Get addresscache field */
- try {
- Field Nameservicefield = Addressclass.getdeclaredfield ("Nameservice");//For JRockit or IBM JDK
- Nameservicefield.setaccessible (TRUE);
- Sun.net.spi.nameservice.NameService Nameservice = (nameservice) nameservicefield.get (null);
- Nameservicefield.set (NULL, New Nameserviceproxy (Nameservice));
- Nameservicefield.setaccessible (FALSE);
- } catch (Nosuchfieldexception e) {
- Field Nameservicesfield = Addressclass.getdeclaredfield ("nameservices");//For OPENJDK
- Nameservicesfield.setaccessible (TRUE);
- List<sun.net.spi.nameservice.nameservice> nameservices = (list<sun.net.spi.nameservice.nameservice>) Nameservicesfield.get (NULL);
- if (nameservices! = null && nameservices.size () > 0) {
- /* Replace as proxy instance */
- Nameservices.set (0, New Nameserviceproxy (Nameservices.get (0)));
- } else {
- Could it be empty? Pending test
- }
- Nameservicesfield.setaccessible (FALSE);
- }
- Gethttpconent ("www.baidu.com");
- }
- public static void Changednswithcddresscache () throws Exception {
- /* 1. Get reflexive class */
- class<?> addressclass = Inetaddress.class;
- /* 2. Get addresscache field */
- Field Addresscachefield = Addressclass.getdeclaredfield ("Addresscache");
- /* 3. Get Addresscache */
- Addresscachefield.setaccessible (TRUE);
- Object Addresscache = Addresscachefield.get (null);
- Addresscachefield.setaccessible (FALSE);
- /* 4. Get the Reflection class for Addresscache */
- class<?> Addresscacheclass = Addresscache.getclass ();
- /* 5. Get the Put method for Addresscache */
- Method Putmethod = Addresscacheclass.getdeclaredmethod ("Put", String.class, Inetaddress[].class);
- /* 5. Modify Addresscache Change wwww.baidu.com to specify any IP */
- Putmethod.setaccessible (TRUE);
- Putmethod.invoke (Addresscache, "www.baidu.com", new inetaddress[] {inetaddress.getbyaddress (new byte[] {, (byte) 239, (Byte) 210, 26});
- Putmethod.setaccessible (FALSE);
- /* 6. Test to see if it is connected */
- Gethttpconent ("www.baidu.com");
- }
- private static void Gethttpconent (String host) throws Malformedurlexception, IOException {
- HttpURLConnection conn = (httpurlconnection) New URL ("http:/" + host). OpenConnection ();
- try {
- Conn.setconnecttimeout (3000);//Reduce connection time for easy testing
- Conn.setdefaultusecaches (FALSE);
- Conn.setdoinput (TRUE);
- Conn.setrequestmethod ("GET");
- Conn.connect ();
- int code = Conn.getresponsecode ();
- System.out.format ("rest[%d]\n", code);
- InputStream in = null;
- in = Conn.geterrorstream ();//If not 2xx, the output is obtained by Errorstream.
- if (in = = null) {
- in = Conn.getinputstream ();
- }
- if (in = null) {
- BufferedReader reader = new BufferedReader (new InputStreamReader (in));
- for (String line = null; (line = Reader.readline ()) = null;) {
- System.out.println (line);
- }
- }
- } finally {
- IF (conn! = null) {
- Conn.disconnect ();
- }
- }
- }
- @SuppressWarnings ("restriction")
- public static class Nameserviceproxy implements Sun.net.spi.nameservice.NameService {
- Final Sun.net.spi.nameservice.NameService Nameservice;
- Final map<string, inetaddress[]> mapping = new hashmap<string, inetaddress[]> ();
- {
- try {
- Mapping.put ("www.baidu.com", new inetaddress[] {inetaddress.getbyaddress (new byte[] {, (byte) 239, (byte) 210, 26}) });
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- }
- }//Instance Initialization table
- Public Nameserviceproxy (Sun.net.spi.nameservice.NameService nameservice) {
- This.nameservice = Nameservice;
- }
- @Override
- Public String gethostbyaddr (byte[] addr) throws Unknownhostexception {
- return this.nameService.getHostByAddr (addr);
- }
- @Override
- Public inetaddress[] LOOKUPALLHOSTADDR (String host) throws Unknownhostexception {
- if (Mapping.containskey (host)) {
- return Mapping.get (host);
- } else {
- return This.nameService.lookupAllHostAddr (host);
- }
- }
- }
- }
Java dynamic replacement for DNS in inetaddress simple analysis 2