@@ -9,7 +9,8 @@ import io.ktor.http.Url
99import kotlinx.serialization.Serializable
1010import kotlinx.serialization.json.buildJsonObject
1111import kotlinx.serialization.json.put
12- import kotlin.io.encoding.Base64
12+ import utils.decodeFlexibleBase64OrNull
13+ import utils.encodeBase64
1314
1415@Serializable
1516data class Shadowsocks (
@@ -44,7 +45,7 @@ data class Shadowsocks(
4445 this .port = url.port.toString()
4546 if (url.port == 0 ) {
4647 val full =
47- ProxyServer .base64.decode( url.host).decodeToString()
48+ url.host.decodeProxyUrlBase64( ).decodeToString()
4849 val infoAndServer = full.split(' @' )
4950 if (infoAndServer.size == 2 ) {
5051 val methodAndPassword = infoAndServer[0 ].split(' :' )
@@ -60,7 +61,7 @@ data class Shadowsocks(
6061 }
6162 } else {
6263 val info = url.user?.let {
63- ProxyServer .base64.decode(it ).decodeToString()
64+ it.decodeProxyUrlBase64( ).decodeToString()
6465 } ? : throw IllegalArgumentException (" Bad Shadowsocks url" )
6566 val pos = info.indexOfFirst { it == ' :' }
6667 if (pos > - 1 ) {
@@ -76,8 +77,7 @@ data class Shadowsocks(
7677 protocol = URLProtocol .createOrDefault(ProxyServerConstants .PROTOCOL_SS )
7778 host = this @Shadowsocks.server
7879 this @Shadowsocks.port.toIntOrNull()?.let { port = it }
79- user = ProxyServer .base64
80- .encode(" ${this @Shadowsocks.method} :${this @Shadowsocks.password} " .toByteArray())
80+ user = " ${this @Shadowsocks.method} :${this @Shadowsocks.password} " .encodeToByteArray().encodeProxyUrlBase64()
8181 fragment = this @Shadowsocks.remarks
8282 }.build().toString()
8383 }
@@ -148,16 +148,14 @@ private fun normalizeShadowsocks2022Key(key: String, validLengths: Set<Int>): St
148148 validLengths.joinToString(" or " ),
149149 )
150150 }
151- return Base64 . Default .encode(decodedKey )
151+ return decodedKey.encodeBase64( )
152152}
153153
154154private fun decodeShadowsocks2022Key (key : String ): ByteArray? {
155155 if (key.isBlank()) {
156156 return null
157157 }
158- return Shadowsocks2022KeyDecoders .firstNotNullOfOrNull { decoder ->
159- runCatching { decoder.decode(key) }.getOrNull()
160- }
158+ return key.decodeFlexibleBase64OrNull()
161159}
162160
163161private val Shadowsocks2022KeyLengths = mapOf (
@@ -166,9 +164,3 @@ private val Shadowsocks2022KeyLengths = mapOf(
166164 " 2022-blake3-chacha20-poly1305" to setOf (32 ),
167165)
168166
169- private val Shadowsocks2022KeyDecoders = listOf (
170- Base64 .Default ,
171- Base64 .Default .withPadding(Base64 .PaddingOption .ABSENT_OPTIONAL ),
172- Base64 .UrlSafe ,
173- Base64 .UrlSafe .withPadding(Base64 .PaddingOption .ABSENT_OPTIONAL ),
174- )
0 commit comments