Skip to content

Commit 4e45661

Browse files
committed
handle ipv6 address's scopeId for dns server
1 parent 5816076 commit 4e45661

File tree

1 file changed

+11
-2
lines changed
  • service/src/main/java/com/github/kr328/clash/service/util

1 file changed

+11
-2
lines changed

service/src/main/java/com/github/kr328/clash/service/util/Address.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.net.InetAddress
77
fun InetAddress.asSocketAddressText(port: Int): String {
88
return when (this) {
99
is Inet6Address ->
10-
"[${numericToTextFormat(this.address)}]:$port"
10+
"[${numericToTextFormat(this)}]:$port"
1111
is Inet4Address ->
1212
"${this.hostAddress}:$port"
1313
else -> throw IllegalArgumentException("Unsupported Inet type ${this.javaClass}")
@@ -16,7 +16,8 @@ fun InetAddress.asSocketAddressText(port: Int): String {
1616

1717
private const val INT16SZ = 2
1818
private const val INADDRSZ = 16
19-
private fun numericToTextFormat(src: ByteArray): String {
19+
private fun numericToTextFormat(address: Inet6Address): String {
20+
var src = address.getAddress()
2021
val sb = StringBuilder(39)
2122
for (i in 0 until INADDRSZ / INT16SZ) {
2223
sb.append(
@@ -29,6 +30,14 @@ private fun numericToTextFormat(src: ByteArray): String {
2930
sb.append(":")
3031
}
3132
}
33+
// handle [fe80::1%wlan0] like address from Inet6Address.getHostAddress()
34+
// For the Android system, a ScopeId must be carried when initiating a connection to an ipv6 link-local address
35+
// Note that the Scope must be returned as an int type, not a string format
36+
// Reference: https://github.com/golang/go/issues/68082
37+
if (address.getScopeId() > 0) {
38+
sb.append("%")
39+
sb.append(address.getScopeId())
40+
}
3241
return sb.toString()
3342
}
3443

0 commit comments

Comments
 (0)