Skip to content

Commit

Permalink
feat: IPv6状态检测和仅IPv4的okhttp3 Dns实现
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 14, 2024
1 parent a4dbc22 commit 7b7927e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api/src/main/java/com/muedsa/tvbox/api/plugin/TvBoxContext.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.muedsa.tvbox.api.plugin

import com.muedsa.tvbox.api.store.IPluginPerfStore
import com.muedsa.tvbox.tool.NetworkUtils

data class TvBoxContext(
val screenWidth: Int, // TV屏幕宽度dp
val screenHeight: Int, // TV屏幕高度dp
val debug: Boolean, // debug模式
val store: IPluginPerfStore // 插件PrefStore
val screenWidth: Int, // TV屏幕宽度dp
val screenHeight: Int, // TV屏幕高度dp
val debug: Boolean, // debug模式
val store: IPluginPerfStore, // 插件PrefStore
val iPv6Status: NetworkUtils.IPv6Status // IPv6状态
)
4 changes: 4 additions & 0 deletions api/src/main/java/com/muedsa/tvbox/tool/HttpTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const val ChromeUserAgent =
fun createOkHttpClient(
debug: Boolean = false,
cookieJar: CookieJar = CookieJar.NO_COOKIES,
onlyIpv4: Boolean = true,
builderApply: OkHttpClient.Builder.() -> Unit = {}
): OkHttpClient =
OkHttpClient.Builder()
Expand All @@ -32,6 +33,9 @@ fun createOkHttpClient(
addNetworkInterceptor(HttpLoggingInterceptor()
.also { it.level = HttpLoggingInterceptor.Level.BODY })
}
if (onlyIpv4) {
dns(OnlyIPv4Dns)
}
builderApply()
}
.build()
Expand Down
64 changes: 64 additions & 0 deletions api/src/main/java/com/muedsa/tvbox/tool/IPv6Checker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.muedsa.tvbox.tool

import java.net.Inet6Address
import java.net.InetSocketAddress
import java.net.NetworkInterface
import java.net.Socket

object NetworkUtils {

private val HOSTS = listOf(
// Google DNS
"2001:4860:4860::8888" to 53,
// 阿里DNS
"2400:3200::1" to 53,
// 百度DNS
"2400:da00::6666" to 53,
// CNNIC DNS
"2001:dc7:1000::1" to 53
)

// 检查是否有可用的IPv6地址
fun hasIPv6Support(): Boolean = try {
NetworkInterface.getNetworkInterfaces()
.asSequence()
.flatMap { it.inetAddresses.asSequence() }
.any { it is Inet6Address && !it.isLoopbackAddress }
} catch (_: Exception) {
false
}

// 尝试创建IPv6 Socket
fun canConnectIPv6(): Boolean {
return HOSTS.any { (host, port) ->
try {
Socket().use { socket ->
socket.connect(InetSocketAddress(host, port), 1000)
true
}
} catch (_: Exception) {
false
}
}
}

// 综合检测方法
fun checkIPv6Support(): IPv6Status {
val hasIPv6Interface = hasIPv6Support()
val systemIPv6Enabled = !System.getProperty("java.net.preferIPv4Stack", "false").toBoolean()

return when {
!systemIPv6Enabled -> IPv6Status.DISABLED_BY_SYSTEM
!hasIPv6Interface -> IPv6Status.NO_IPV6_INTERFACE
!canConnectIPv6() -> IPv6Status.NO_IPV6_CONNECTION
else -> IPv6Status.SUPPORTED
}
}

enum class IPv6Status {
SUPPORTED, // IPv6完全支持
NO_IPV6_INTERFACE, // 没有IPv6网络接口
NO_IPV6_CONNECTION, // 有接口但无法连接
DISABLED_BY_SYSTEM // 系统禁用IPv6
}
}
16 changes: 16 additions & 0 deletions api/src/main/java/com/muedsa/tvbox/tool/OnlyIPv4Dns.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.muedsa.tvbox.tool

import okhttp3.Dns
import java.net.Inet4Address
import java.net.InetAddress
import java.net.UnknownHostException

object OnlyIPv4Dns : Dns {
override fun lookup(hostname: String): List<InetAddress> = InetAddress.getAllByName(hostname)
.filter { it is Inet4Address }
.also { addresses ->
if (addresses.isEmpty()) {
throw UnknownHostException("No IPv4 address for $hostname")
}
}
}

0 comments on commit 7b7927e

Please sign in to comment.