-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
api/src/main/java/com/muedsa/tvbox/api/plugin/TvBoxContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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状态 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |