mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 22:50:05 +08:00
Fix socksfmt
This commit is contained in:
parent
3aa428b06f
commit
d52ec71bb9
@ -6,42 +6,32 @@ import io.nekohasekai.sagernet.ktx.unUrlSafe
|
|||||||
import io.nekohasekai.sagernet.ktx.urlSafe
|
import io.nekohasekai.sagernet.ktx.urlSafe
|
||||||
import moe.matsuri.nb4a.SingBoxOptions
|
import moe.matsuri.nb4a.SingBoxOptions
|
||||||
import moe.matsuri.nb4a.utils.NGUtil
|
import moe.matsuri.nb4a.utils.NGUtil
|
||||||
|
import moe.matsuri.nb4a.utils.Util
|
||||||
import okhttp3.HttpUrl
|
import okhttp3.HttpUrl
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
|
||||||
fun parseSOCKS(link: String): SOCKSBean {
|
fun parseSOCKS(link: String): SOCKSBean {
|
||||||
if (!link.substringAfter("://").contains(":")) {
|
val url = ("http://" + link.substringAfter("://")).toHttpUrlOrNull()
|
||||||
// v2rayN shit format
|
?: error("Not supported: $link")
|
||||||
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")
|
|
||||||
|
|
||||||
return SOCKSBean().apply {
|
return SOCKSBean().apply {
|
||||||
protocol = when {
|
protocol = when {
|
||||||
link.startsWith("socks4://") -> SOCKSBean.PROTOCOL_SOCKS4
|
link.startsWith("socks4://") -> SOCKSBean.PROTOCOL_SOCKS4
|
||||||
link.startsWith("socks4a://") -> SOCKSBean.PROTOCOL_SOCKS4A
|
link.startsWith("socks4a://") -> SOCKSBean.PROTOCOL_SOCKS4A
|
||||||
else -> SOCKSBean.PROTOCOL_SOCKS5
|
else -> SOCKSBean.PROTOCOL_SOCKS5
|
||||||
|
}
|
||||||
|
serverAddress = url.host
|
||||||
|
serverPort = url.port
|
||||||
|
username = url.username
|
||||||
|
password = url.password
|
||||||
|
// v2rayN fmt
|
||||||
|
if (password.isNullOrBlank() && !username.isNullOrBlank()) {
|
||||||
|
try {
|
||||||
|
val n = username.decodeBase64UrlSafe()
|
||||||
|
username = n.substringBefore(":")
|
||||||
|
password = n.substringAfter(":")
|
||||||
|
} catch (_: Exception) {
|
||||||
}
|
}
|
||||||
serverAddress = url.host
|
|
||||||
serverPort = url.port
|
|
||||||
username = url.username
|
|
||||||
password = url.password
|
|
||||||
name = url.fragment
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -72,7 +72,7 @@ object Util {
|
|||||||
for (flag in flags) {
|
for (flag in flags) {
|
||||||
try {
|
try {
|
||||||
ret = Base64.decode(str, flag)
|
ret = Base64.decode(str, flag)
|
||||||
} catch (e: Exception) {
|
} catch (_: Exception) {
|
||||||
}
|
}
|
||||||
if (ret != null) return ret
|
if (ret != null) return ret
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user