Android下tun0网络的检测方法
2023-02-19 12:21:24 时间
1. 判断是否包含tun0、tun1
public void isDeviceInTun() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (name.equals("tun0") || name.equals("tun1")) {
Log.i("TAG", "isDeviceInVPN current device is in VPN.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
2. 获取tun0的IP地址
/**
* 获取指定网卡ip地址
*
* @return
*/
public static String getLocalIP(String ethType) {
String hostIp = null;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
if (ni.getName().equals(ethType)) {
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
if (!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
}
} catch (SocketException e) {
Log.i("IPUtil", "SocketException");
e.printStackTrace();
}
return hostIp;
}
相关文章
- 软件测试|App自动化之Appium工作原理
- 【TUICallKit】小程序全局监听(主包方案)
- Android 12 蓝牙适配 Java版
- 封装 axios 拦截器实现用户无感刷新 access_token
- 在Android 应用程序开发期间减少Android应用程序大小的九种方法
- Go 语言为什么建议 append 追加新元素使用原切片变量接收返回值?
- iOS 16.1重磅来袭!全机型更新拍照、灵动岛和电池百分比显示
- 以羊了个羊为例,浅谈小程序抓包与响应报文篡改
- Python 搭建云服务和小程序“云办公”
- 「合体」20个月,卷出安卓最强野兽X90!这颗国产「V2」,又把发哥带飞了
- 我真不想学 happens - before 了!
- Go语言Append缺陷引发的深度拷贝讨论
- 微信小程序位置 API
- 获取用户授权的手机号【微信小程序】
- 小程序客服会话
- EasyWechat 4.x 微信小程序企业付款到零钱
- EasyWechat 4.x 微信小程序订阅消息
- EasyWechat 3.x 小程序客服消息自动回复
- 得物App在后台悄悄删除手机里的视频?反转了,兄弟们!
- 这几个 app 用得上