mirror of
https://github.com/MetaCubeX/ClashMetaForAndroid.git
synced 2025-03-30 04:00:18 +03:00
handle ipv6 address's scopeId for dns server
This commit is contained in:
parent
5816076bf6
commit
4e45661e26
1 changed files with 11 additions and 2 deletions
|
@ -7,7 +7,7 @@ import java.net.InetAddress
|
||||||
fun InetAddress.asSocketAddressText(port: Int): String {
|
fun InetAddress.asSocketAddressText(port: Int): String {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Inet6Address ->
|
is Inet6Address ->
|
||||||
"[${numericToTextFormat(this.address)}]:$port"
|
"[${numericToTextFormat(this)}]:$port"
|
||||||
is Inet4Address ->
|
is Inet4Address ->
|
||||||
"${this.hostAddress}:$port"
|
"${this.hostAddress}:$port"
|
||||||
else -> throw IllegalArgumentException("Unsupported Inet type ${this.javaClass}")
|
else -> throw IllegalArgumentException("Unsupported Inet type ${this.javaClass}")
|
||||||
|
@ -16,7 +16,8 @@ fun InetAddress.asSocketAddressText(port: Int): String {
|
||||||
|
|
||||||
private const val INT16SZ = 2
|
private const val INT16SZ = 2
|
||||||
private const val INADDRSZ = 16
|
private const val INADDRSZ = 16
|
||||||
private fun numericToTextFormat(src: ByteArray): String {
|
private fun numericToTextFormat(address: Inet6Address): String {
|
||||||
|
var src = address.getAddress()
|
||||||
val sb = StringBuilder(39)
|
val sb = StringBuilder(39)
|
||||||
for (i in 0 until INADDRSZ / INT16SZ) {
|
for (i in 0 until INADDRSZ / INT16SZ) {
|
||||||
sb.append(
|
sb.append(
|
||||||
|
@ -29,6 +30,14 @@ private fun numericToTextFormat(src: ByteArray): String {
|
||||||
sb.append(":")
|
sb.append(":")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// handle [fe80::1%wlan0] like address from Inet6Address.getHostAddress()
|
||||||
|
// For the Android system, a ScopeId must be carried when initiating a connection to an ipv6 link-local address
|
||||||
|
// Note that the Scope must be returned as an int type, not a string format
|
||||||
|
// Reference: https://github.com/golang/go/issues/68082
|
||||||
|
if (address.getScopeId() > 0) {
|
||||||
|
sb.append("%")
|
||||||
|
sb.append(address.getScopeId())
|
||||||
|
}
|
||||||
return sb.toString()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue