首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
Java动态替换InetAddress中DNS的做法简单分析2
时间:
2016-09-07 14:40:54
阅读:
457
评论:
0
收藏:
0
[点我收藏+]
标签:
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 {
/**
* 经测试,二种方式在Windows环境都可以, Linux环境待测
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
/* 1. 动态替换AddressCache */
// changeDNSWithAddressCache();
/* 1. 动态代理NameService */
changeDNSWithNameService();
}
public static void changeDNSWithNameService() throws Exception {
/* 1. 获取反身类 */
Class<?> addressClass = InetAddress.class;
/* 2. 获取addressCache字段 */
try {
Field nameServiceField = addressClass.getDeclaredField("nameService");// 对于Jrockit或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");// 对于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) {
/* 置换为代理实例 */
nameServices.set(0, new NameServiceProxy(nameServices.get(0)));
} else {
// 可能为空吗? 待测
}
nameServicesField.setAccessible(false);
}
getHttpConent("www.baidu.com");
}
public static void changeDNSWithCddressCache() throws Exception {
/* 1. 获取反身类 */
Class<?> addressClass = InetAddress.class;
/* 2. 获取addressCache字段 */
Field addressCacheField = addressClass.getDeclaredField("addressCache");
/* 3. 获取addressCache */
addressCacheField.setAccessible(true);
Object addressCache = addressCacheField.get(null);
addressCacheField.setAccessible(false);
/* 4. 获取addressCache的反射类 */
Class<?> addressCacheClass = addressCache.getClass();
/* 5. 获取addressCache的put方法 */
Method putMethod = addressCacheClass.getDeclaredMethod("put", String.class, InetAddress[].class);
/* 5. 修改addressCache将wwww.baidu.com换成指定任意IP */
putMethod.setAccessible(true);
putMethod.invoke(addressCache, "www.baidu.com", new InetAddress[] { InetAddress.getByAddress(new byte[] { 115, (byte) 239, (byte) 210, 26 }) });
putMethod.setAccessible(false);
/* 6.测试,看看是否连通 */
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);// 减少连接时间,方便测试
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();// 如果非2xx,则由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[] { 115, (byte) 239, (byte) 210, 26 }) });
} catch (UnknownHostException e) {
e.printStackTrace();
}
}// 实例初始化表
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动态替换InetAddress中DNS的做法简单分析2
标签:
原文地址:http://www.cnblogs.com/zolo/p/5849244.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!