Fix socksfmt

This commit is contained in:
arm64v8a 2023-06-29 21:39:54 +09:00
parent 3aa428b06f
commit d52ec71bb9
2 changed files with 21 additions and 31 deletions

View File

@ -6,28 +6,11 @@ import io.nekohasekai.sagernet.ktx.unUrlSafe
import io.nekohasekai.sagernet.ktx.urlSafe
import moe.matsuri.nb4a.SingBoxOptions
import moe.matsuri.nb4a.utils.NGUtil
import moe.matsuri.nb4a.utils.Util
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
fun parseSOCKS(link: String): SOCKSBean {
if (!link.substringAfter("://").contains(":")) {
// v2rayN shit format
var url = link.substringAfter("://")
if (url.contains("#")) {
url = url.substringBeforeLast("#")
}
url = url.decodeBase64UrlSafe()
val httpUrl = "http://$url".toHttpUrlOrNull() ?: error("Invalid v2rayN link content: $url")
return SOCKSBean().apply {
serverAddress = httpUrl.host
serverPort = httpUrl.port
username = httpUrl.username.takeIf { it != "null" } ?: ""
password = httpUrl.password.takeIf { it != "null" } ?: ""
if (link.contains("#")) {
name = link.substringAfter("#").unUrlSafe()
}
}
} else {
val url = ("http://" + link.substringAfter("://")).toHttpUrlOrNull()
?: error("Not supported: $link")
@ -41,7 +24,14 @@ fun parseSOCKS(link: String): SOCKSBean {
serverPort = url.port
username = url.username
password = url.password
name = url.fragment
// v2rayN fmt
if (password.isNullOrBlank() && !username.isNullOrBlank()) {
try {
val n = username.decodeBase64UrlSafe()
username = n.substringBefore(":")
password = n.substringAfter(":")
} catch (_: Exception) {
}
}
}
}

View File

@ -72,7 +72,7 @@ object Util {
for (flag in flags) {
try {
ret = Base64.decode(str, flag)
} catch (e: Exception) {
} catch (_: Exception) {
}
if (ret != null) return ret
}