Skip to content

Instantly share code, notes, and snippets.

@beilly
Created October 16, 2019 08:20
Show Gist options
  • Save beilly/ff3a33d4bae69823b10fd940a4fe3243 to your computer and use it in GitHub Desktop.
Save beilly/ff3a33d4bae69823b10fd940a4fe3243 to your computer and use it in GitHub Desktop.
代理检测
/**
* 是否使用代理(WiFi状态下的,避免被抓包)
*/
fun isWifiProxy(context: Context?): Boolean {
val isIcsOrLater = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
val proxyAddress: String
val proxyPort: Int
if (isIcsOrLater) {
proxyAddress = System.getProperty("http.proxyHost")
val portstr = System.getProperty("http.proxyPort")
proxyPort = Integer.parseInt(portstr ?: "-1")
println("$proxyAddress~")
println("port = $proxyPort")
} else {
proxyAddress = if (null != context) android.net.Proxy.getHost(context) else ""
proxyPort = if (null != context) android.net.Proxy.getPort(context) else -1
Log.e("address = ", "$proxyAddress~")
Log.e("port = ", "$proxyPort~")
}
return !TextUtils.isEmpty(proxyAddress) && proxyPort != -1
}
/**
* 是否正在使用VPN
*/
fun isVpnUsed(): Boolean {
try {
val niList = NetworkInterface.getNetworkInterfaces()
if (niList != null) {
for (intf in Collections.list(niList)) {
if (!intf.isUp || intf.interfaceAddresses.size === 0) {
continue
}
Log.d("-----", "isVpnUsed() NetworkInterface Name: " + intf.getName())
if ("tun0" == intf.name || "ppp0" == intf.name) {
return true // The VPN is up
}
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment