Java dynamic replacement for DNS in inetaddress simple analysis 2

Source: Internet
Author: User

  1. Import Java.io.BufferedReader;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import Java.io.InputStreamReader;
  5. Import Java.lang.reflect.Field;
  6. Import Java.lang.reflect.Method;
  7. Import java.net.HttpURLConnection;
  8. Import java.net.InetAddress;
  9. Import java.net.MalformedURLException;
  10. Import Java.net.URL;
  11. Import java.net.UnknownHostException;
  12. Import Java.util.HashMap;
  13. Import java.util.List;
  14. Import Java.util.Map;
  15. Import Sun.net.spi.nameservice.NameService;
  16. public class Javadnscachetest {
  17. /**
  18. * Tested, two ways can be in Windows environment, Linux environment to be tested
  19. *
  20. * @param args
  21. * @throws Exception
  22. */
  23. public static void Main (string[] args) throws Exception {
  24. /* 1. Dynamic Replacement Addresscache */
  25. Changednswithaddresscache ();
  26. /* 1. Dynamic Agent Nameservice */
  27. Changednswithnameservice ();
  28. }
  29. public static void Changednswithnameservice () throws Exception {
  30. /* 1. Get reflexive class */
  31. class<?> addressclass = Inetaddress.class;
  32. /* 2. Get addresscache field */
  33. try {
  34. Field Nameservicefield = Addressclass.getdeclaredfield ("Nameservice");//For JRockit or IBM JDK
  35. Nameservicefield.setaccessible (TRUE);
  36. Sun.net.spi.nameservice.NameService Nameservice = (nameservice) nameservicefield.get (null);
  37. Nameservicefield.set (NULL, New Nameserviceproxy (Nameservice));
  38. Nameservicefield.setaccessible (FALSE);
  39. } catch (Nosuchfieldexception e) {
  40. Field Nameservicesfield = Addressclass.getdeclaredfield ("nameservices");//For OPENJDK
  41. Nameservicesfield.setaccessible (TRUE);
  42. List<sun.net.spi.nameservice.nameservice> nameservices = (list<sun.net.spi.nameservice.nameservice>) Nameservicesfield.get (NULL);
  43. if (nameservices! = null && nameservices.size () > 0) {
  44. /* Replace as proxy instance */
  45. Nameservices.set (0, New Nameserviceproxy (Nameservices.get (0)));
  46. } else {
  47. Could it be empty? Pending test
  48. }
  49. Nameservicesfield.setaccessible (FALSE);
  50. }
  51. Gethttpconent ("www.baidu.com");
  52. }
  53. public static void Changednswithcddresscache () throws Exception {
  54. /* 1. Get reflexive class */
  55. class<?> addressclass = Inetaddress.class;
  56. /* 2. Get addresscache field */
  57. Field Addresscachefield = Addressclass.getdeclaredfield ("Addresscache");
  58. /* 3. Get Addresscache */
  59. Addresscachefield.setaccessible (TRUE);
  60. Object Addresscache = Addresscachefield.get (null);
  61. Addresscachefield.setaccessible (FALSE);
  62. /* 4. Get the Reflection class for Addresscache */
  63. class<?> Addresscacheclass = Addresscache.getclass ();
  64. /* 5. Get the Put method for Addresscache */
  65. Method Putmethod = Addresscacheclass.getdeclaredmethod ("Put", String.class, Inetaddress[].class);
  66. /* 5. Modify Addresscache Change wwww.baidu.com to specify any IP */
  67. Putmethod.setaccessible (TRUE);
  68. Putmethod.invoke (Addresscache, "www.baidu.com", new inetaddress[] {inetaddress.getbyaddress (new byte[] {, (byte) 239, (Byte) 210, 26});
  69. Putmethod.setaccessible (FALSE);
  70. /* 6. Test to see if it is connected */
  71. Gethttpconent ("www.baidu.com");
  72. }
  73. private static void Gethttpconent (String host) throws Malformedurlexception, IOException {
  74. HttpURLConnection conn = (httpurlconnection) New URL ("http:/" + host). OpenConnection ();
  75. try {
  76. Conn.setconnecttimeout (3000);//Reduce connection time for easy testing
  77. Conn.setdefaultusecaches (FALSE);
  78. Conn.setdoinput (TRUE);
  79. Conn.setrequestmethod ("GET");
  80. Conn.connect ();
  81. int code = Conn.getresponsecode ();
  82. System.out.format ("rest[%d]\n", code);
  83. InputStream in = null;
  84. in = Conn.geterrorstream ();//If not 2xx, the output is obtained by Errorstream.
  85. if (in = = null) {
  86. in = Conn.getinputstream ();
  87. }
  88. if (in = null) {
  89. BufferedReader reader = new BufferedReader (new InputStreamReader (in));
  90. for (String line = null; (line = Reader.readline ()) = null;) {
  91. System.out.println (line);
  92. }
  93. }
  94. } finally {
  95. IF (conn! = null) {
  96. Conn.disconnect ();
  97. }
  98. }
  99. }
  100. @SuppressWarnings ("restriction")
  101. public static class Nameserviceproxy implements Sun.net.spi.nameservice.NameService {
  102. Final Sun.net.spi.nameservice.NameService Nameservice;
  103. Final map<string, inetaddress[]> mapping = new hashmap<string, inetaddress[]> ();
  104. {
  105. try {
  106. Mapping.put ("www.baidu.com", new inetaddress[] {inetaddress.getbyaddress (new byte[] {, (byte) 239, (byte) 210, 26}) });
  107. } catch (Unknownhostexception e) {
  108. E.printstacktrace ();
  109. }
  110. }//Instance Initialization table
  111. Public Nameserviceproxy (Sun.net.spi.nameservice.NameService nameservice) {
  112. This.nameservice = Nameservice;
  113. }
  114. @Override
  115. Public String gethostbyaddr (byte[] addr) throws Unknownhostexception {
  116. return this.nameService.getHostByAddr (addr);
  117. }
  118. @Override
  119. Public inetaddress[] LOOKUPALLHOSTADDR (String host) throws Unknownhostexception {
  120. if (Mapping.containskey (host)) {
  121. return Mapping.get (host);
  122. } else {
  123. return This.nameService.lookupAllHostAddr (host);
  124. }
  125. }
  126. }
  127. }

Java dynamic replacement for DNS in inetaddress simple analysis 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.