From a7cf8bee28df2eead615613d08a73284cd842b22 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 11 Jul 2024 16:40:25 +0800 Subject: [PATCH] Code clean Add allowInsecure to Share --- .../com/v2ray/ang/util/AppManagerUtil.kt | 8 +- .../kotlin/com/v2ray/ang/util/MmkvManager.kt | 2 +- .../com/v2ray/ang/util/MyContextWrapper.kt | 2 +- .../com/v2ray/ang/util/SpeedtestUtil.kt | 24 +- .../com/v2ray/ang/util/V2rayConfigUtil.kt | 29 ++- .../com/v2ray/ang/util/fmt/ShadowsocksFmt.kt | 6 +- .../com/v2ray/ang/util/fmt/TrojanFmt.kt | 69 +++--- .../kotlin/com/v2ray/ang/util/fmt/VlessFmt.kt | 13 +- .../kotlin/com/v2ray/ang/util/fmt/VmessFmt.kt | 223 ++++++++---------- .../com/v2ray/ang/util/fmt/WireguardFmt.kt | 5 +- 10 files changed, 183 insertions(+), 198 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AppManagerUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AppManagerUtil.kt index 03e38e93..3c77c863 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AppManagerUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AppManagerUtil.kt @@ -7,7 +7,6 @@ import android.content.pm.PackageInfo import android.content.pm.PackageManager import com.v2ray.ang.dto.AppInfo import rx.Observable -import java.util.* object AppManagerUtil { fun loadNetworkAppList(ctx: Context): ArrayList { @@ -31,9 +30,10 @@ object AppManagerUtil { return apps } - fun rxLoadNetworkAppList(ctx: Context): Observable> = Observable.unsafeCreate { - it.onNext(loadNetworkAppList(ctx)) - } + fun rxLoadNetworkAppList(ctx: Context): Observable> = + Observable.unsafeCreate { + it.onNext(loadNetworkAppList(ctx)) + } val PackageInfo.hasInternetPermission: Boolean get() { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt index 024f5c79..e947f7ad 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt @@ -179,7 +179,7 @@ object MmkvManager { } } - fun sortByTestResults( ) { + fun sortByTestResults() { data class ServerDelay(var guid: String, var testDelayMillis: Long) val serverDelays = mutableListOf() diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MyContextWrapper.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MyContextWrapper.kt index cf9bd35e..8c0a4c63 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MyContextWrapper.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MyContextWrapper.kt @@ -7,7 +7,7 @@ import android.content.res.Resources import android.os.Build import android.os.LocaleList import androidx.annotation.RequiresApi -import java.util.* +import java.util.Locale open class MyContextWrapper(base: Context?) : ContextWrapper(base) { companion object { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt index eec61573..03a3f6ba 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt @@ -52,7 +52,8 @@ object SpeedtestUtil { val allText = process.inputStream.bufferedReader().use { it.readText() } if (!TextUtils.isEmpty(allText)) { val tempInfo = allText.substring(allText.indexOf("min/avg/max/mdev") + 19) - val temps = tempInfo.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val temps = + tempInfo.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() if (temps.count() > 0 && temps[0].length < 10) { return temps[0].toFloat().toInt().toString() + "ms" } @@ -70,7 +71,7 @@ object SpeedtestUtil { tcpTestingSockets.add(socket) } val start = System.currentTimeMillis() - socket.connect(InetSocketAddress(url, port),3000) + socket.connect(InetSocketAddress(url, port), 3000) val time = System.currentTimeMillis() - start synchronized(this) { tcpTestingSockets.remove(socket) @@ -105,8 +106,11 @@ object SpeedtestUtil { val url = URL(Utils.getDelayTestUrl()) conn = url.openConnection( - Proxy(Proxy.Type.HTTP, - InetSocketAddress("127.0.0.1", port))) as HttpURLConnection + Proxy( + Proxy.Type.HTTP, + InetSocketAddress("127.0.0.1", port) + ) + ) as HttpURLConnection conn.connectTimeout = 30000 conn.readTimeout = 30000 conn.setRequestProperty("Connection", "close") @@ -120,11 +124,19 @@ object SpeedtestUtil { if (code == 204 || code == 200 && conn.responseLength == 0L) { result = context.getString(R.string.connection_test_available, elapsed) } else { - throw IOException(context.getString(R.string.connection_test_error_status_code, code)) + throw IOException( + context.getString( + R.string.connection_test_error_status_code, + code + ) + ) } } catch (e: IOException) { // network exception - Log.d(AppConfig.ANG_PACKAGE, "testConnection IOException: " + Log.getStackTraceString(e)) + Log.d( + AppConfig.ANG_PACKAGE, + "testConnection IOException: " + Log.getStackTraceString(e) + ) result = context.getString(R.string.connection_test_error, e.message) } catch (e: Exception) { // library exception, eg sumsung diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt index 464ab6e6..2644be2c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt @@ -142,7 +142,8 @@ object V2rayConfigUtil { settingsStorage?.decodeBool(AppConfig.PREF_SNIFFING_ENABLED, true) ?: true v2rayConfig.inbounds[0].sniffing?.enabled = fakedns || sniffAllTlsAndHttp - v2rayConfig.inbounds[0].sniffing?.routeOnly = settingsStorage?.decodeBool(AppConfig.PREF_ROUTE_ONLY_ENABLED, false) + v2rayConfig.inbounds[0].sniffing?.routeOnly = + settingsStorage?.decodeBool(AppConfig.PREF_ROUTE_ONLY_ENABLED, false) if (!sniffAllTlsAndHttp) { v2rayConfig.inbounds[0].sniffing?.destOverride?.clear() } @@ -188,7 +189,7 @@ object V2rayConfigUtil { ) routingUserRule( settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_DIRECT) - ?: "", AppConfig.TAG_DIRECT, v2rayConfig + ?: "", TAG_DIRECT, v2rayConfig ) routingUserRule( settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_BLOCKED) @@ -210,37 +211,38 @@ object V2rayConfigUtil { when (routingMode) { ERoutingMode.BYPASS_LAN.value -> { - routingGeo("ip", "private", AppConfig.TAG_DIRECT, v2rayConfig) + routingGeo("ip", "private", TAG_DIRECT, v2rayConfig) } ERoutingMode.BYPASS_MAINLAND.value -> { - routingGeo("", "cn", AppConfig.TAG_DIRECT, v2rayConfig) - routingGeo("domain", "geolocation-cn", AppConfig.TAG_DIRECT, v2rayConfig) + routingGeo("", "cn", TAG_DIRECT, v2rayConfig) + routingGeo("domain", "geolocation-cn", TAG_DIRECT, v2rayConfig) v2rayConfig.routing.rules.add(0, googleapisRoute) } ERoutingMode.BYPASS_LAN_MAINLAND.value -> { - routingGeo("ip", "private", AppConfig.TAG_DIRECT, v2rayConfig) - routingGeo("", "cn", AppConfig.TAG_DIRECT, v2rayConfig) - routingGeo("domain", "geolocation-cn", AppConfig.TAG_DIRECT, v2rayConfig) + routingGeo("ip", "private", TAG_DIRECT, v2rayConfig) + routingGeo("", "cn", TAG_DIRECT, v2rayConfig) + routingGeo("domain", "geolocation-cn", TAG_DIRECT, v2rayConfig) v2rayConfig.routing.rules.add(0, googleapisRoute) } ERoutingMode.GLOBAL_DIRECT.value -> { val globalDirect = V2rayConfig.RoutingBean.RulesBean( - outboundTag = AppConfig.TAG_DIRECT, + outboundTag = TAG_DIRECT, port = "0-65535" ) v2rayConfig.routing.rules.add(globalDirect) } } - if(routingMode != ERoutingMode.GLOBAL_DIRECT.value) { + if (routingMode != ERoutingMode.GLOBAL_DIRECT.value) { v2rayConfig.routing.rules.add( V2rayConfig.RoutingBean.RulesBean( outboundTag = AppConfig.TAG_PROXY, port = "0-65535" - )) + ) + ) } } catch (e: Exception) { @@ -465,7 +467,7 @@ object V2rayConfigUtil { if (Utils.isPureIpAddress(domesticDns.first())) { v2rayConfig.routing.rules.add( 0, V2rayConfig.RoutingBean.RulesBean( - outboundTag = AppConfig.TAG_DIRECT, + outboundTag = TAG_DIRECT, port = "53", ip = arrayListOf(domesticDns.first()), domain = null @@ -597,7 +599,8 @@ object V2rayConfigUtil { mux = null ) - var packets = settingsStorage?.decodeString(AppConfig.PREF_FRAGMENT_PACKETS) ?: "tlshello" + var packets = + settingsStorage?.decodeString(AppConfig.PREF_FRAGMENT_PACKETS) ?: "tlshello" if (v2rayConfig.outbounds[0].streamSettings?.security == V2rayConfig.REALITY && packets == "tlshello" ) { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/ShadowsocksFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/ShadowsocksFmt.kt index 668264aa..d0710dde 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/ShadowsocksFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/ShadowsocksFmt.kt @@ -96,7 +96,7 @@ object ShadowsocksFmt { for (pair in pairs) { val idx = pair.indexOf("=") if (idx == -1) { - queryPairs[Utils.urlDecode(pair)] = ""; + queryPairs[Utils.urlDecode(pair)] = "" } else { queryPairs[Utils.urlDecode(pair.substring(0, idx))] = Utils.urlDecode(pair.substring(idx + 1)) @@ -118,9 +118,9 @@ object ShadowsocksFmt { null ) } else if (queryPairs["plugin"] == "v2ray-plugin") { - var network = "ws"; + var network = "ws" if (queryPairs["mode"] == "quic") { - network = "quic"; + network = "quic" } sni = config.outboundBean?.streamSettings?.populateTransportSettings( network, diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/TrojanFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/TrojanFmt.kt index 5f50b137..119c47b7 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/TrojanFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/TrojanFmt.kt @@ -20,7 +20,7 @@ object TrojanFmt { } fun parseTrojan(str: String): ServerConfig? { - val allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false + var allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false val config = ServerConfig.create(EConfigType.TROJAN) val uri = URI(Utils.fixIllegalUrl(str)) @@ -28,35 +28,37 @@ object TrojanFmt { var flow = "" var fingerprint = config.outboundBean?.streamSettings?.tlsSettings?.fingerprint - if (uri.rawQuery.isNullOrEmpty()) { - config.outboundBean?.streamSettings?.populateTlsSettings( - V2rayConfig.TLS, allowInsecure, "", - fingerprint, null, null, null, null - ) - } else { - val queryParam = uri.rawQuery.split("&") - .associate { it.split("=").let { (k, v) -> k to Utils.urlDecode(v) } } + if (uri.rawQuery.isNullOrEmpty()) return null - val sni = config.outboundBean?.streamSettings?.populateTransportSettings( - queryParam["type"] ?: "tcp", - queryParam["headerType"], - queryParam["host"], - queryParam["path"], - queryParam["seed"], - queryParam["quicSecurity"], - queryParam["key"], - queryParam["mode"], - queryParam["serviceName"], - queryParam["authority"] - ) - fingerprint = queryParam["fp"] ?: "" - config.outboundBean?.streamSettings?.populateTlsSettings( - queryParam["security"] ?: V2rayConfig.TLS, - allowInsecure, queryParam["sni"] ?: sni?:"", fingerprint, queryParam["alpn"], - null, null, null - ) - flow = queryParam["flow"] ?: "" - } + + val queryParam = uri.rawQuery.split("&") + .associate { it.split("=").let { (k, v) -> k to Utils.urlDecode(v) } } + + val sni = config.outboundBean?.streamSettings?.populateTransportSettings( + queryParam["type"] ?: "tcp", + queryParam["headerType"], + queryParam["host"], + queryParam["path"], + queryParam["seed"], + queryParam["quicSecurity"], + queryParam["key"], + queryParam["mode"], + queryParam["serviceName"], + queryParam["authority"] + ) + fingerprint = queryParam["fp"] ?: "" + allowInsecure = if ((queryParam["allowInsecure"] ?: "") == "1") true else allowInsecure + config.outboundBean?.streamSettings?.populateTlsSettings( + queryParam["security"] ?: V2rayConfig.TLS, + allowInsecure, + queryParam["sni"] ?: sni ?: "", + fingerprint, + queryParam["alpn"], + null, + null, + null + ) + flow = queryParam["flow"] ?: "" config.outboundBean?.settings?.servers?.get(0)?.let { server -> server.address = uri.idnHost @@ -78,7 +80,6 @@ object TrojanFmt { if (!TextUtils.isEmpty(it)) { dicQuery["flow"] = it } - } dicQuery["security"] = streamSetting.security.ifEmpty { "none" } @@ -92,16 +93,16 @@ object TrojanFmt { Utils.removeWhiteSpace(tlsSetting.alpn.joinToString()).orEmpty() } if (!TextUtils.isEmpty(tlsSetting.fingerprint)) { - dicQuery["fp"] = tlsSetting.fingerprint?:"" + dicQuery["fp"] = tlsSetting.fingerprint ?: "" } if (!TextUtils.isEmpty(tlsSetting.publicKey)) { - dicQuery["pbk"] = tlsSetting.publicKey?:"" + dicQuery["pbk"] = tlsSetting.publicKey ?: "" } if (!TextUtils.isEmpty(tlsSetting.shortId)) { - dicQuery["sid"] = tlsSetting.shortId?:"" + dicQuery["sid"] = tlsSetting.shortId ?: "" } if (!TextUtils.isEmpty(tlsSetting.spiderX)) { - dicQuery["spx"] = Utils.urlEncode(tlsSetting.spiderX?:"") + dicQuery["spx"] = Utils.urlEncode(tlsSetting.spiderX ?: "") } } dicQuery["type"] = diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VlessFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VlessFmt.kt index 9c5fb6b3..5e445d3c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VlessFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VlessFmt.kt @@ -20,7 +20,7 @@ object VlessFmt { } fun parseVless(str: String): ServerConfig? { - val allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false + var allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false val config = ServerConfig.create(EConfigType.VLESS) val uri = URI(Utils.fixIllegalUrl(str)) @@ -31,7 +31,7 @@ object VlessFmt { val streamSetting = config.outboundBean?.streamSettings ?: return null config.remarks = Utils.urlDecode(uri.fragment ?: "") - config.outboundBean?.settings?.vnext?.get(0)?.let { vnext -> + config.outboundBean.settings?.vnext?.get(0)?.let { vnext -> vnext.address = uri.idnHost vnext.port = uri.port vnext.users[0].id = uri.userInfo @@ -51,6 +51,7 @@ object VlessFmt { queryParam["serviceName"], queryParam["authority"] ) + allowInsecure = if ((queryParam["allowInsecure"] ?: "") == "1") true else allowInsecure streamSetting.populateTlsSettings( queryParam["security"] ?: "", allowInsecure, @@ -92,16 +93,16 @@ object VlessFmt { Utils.removeWhiteSpace(tlsSetting.alpn.joinToString()).orEmpty() } if (!TextUtils.isEmpty(tlsSetting.fingerprint)) { - dicQuery["fp"] = tlsSetting.fingerprint?:"" + dicQuery["fp"] = tlsSetting.fingerprint ?: "" } if (!TextUtils.isEmpty(tlsSetting.publicKey)) { - dicQuery["pbk"] = tlsSetting.publicKey?:"" + dicQuery["pbk"] = tlsSetting.publicKey ?: "" } if (!TextUtils.isEmpty(tlsSetting.shortId)) { - dicQuery["sid"] = tlsSetting.shortId?:"" + dicQuery["sid"] = tlsSetting.shortId ?: "" } if (!TextUtils.isEmpty(tlsSetting.spiderX)) { - dicQuery["spx"] = Utils.urlEncode(tlsSetting.spiderX?:"") + dicQuery["spx"] = Utils.urlEncode(tlsSetting.spiderX ?: "") } } dicQuery["type"] = diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VmessFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VmessFmt.kt index 83dbe679..55e30b29 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VmessFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/VmessFmt.kt @@ -22,68 +22,64 @@ object VmessFmt { } fun parseVmess(str: String): ServerConfig? { + if (str.indexOf('?') > 0 && str.indexOf('&') > 0) { + return parseVmessStd(str) + } + val allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false val config = ServerConfig.create(EConfigType.VMESS) val streamSetting = config.outboundBean?.streamSettings ?: return null - - if (!tryParseNewVmess(str, config, allowInsecure)) { - if (str.indexOf("?") > 0) { - if (!tryResolveVmess4Kitsunebi(str, config)) { - Log.d(AppConfig.ANG_PACKAGE, "R.string.toast_incorrect_protocol") - return null - } - } else { - var result = str.replace(EConfigType.VMESS.protocolScheme, "") - result = Utils.decode(result) - if (TextUtils.isEmpty(result)) { - Log.d(AppConfig.ANG_PACKAGE, "R.string.toast_decoding_failed") - return null - } - val vmessQRCode = Gson().fromJson(result, VmessQRCode::class.java) - // Although VmessQRCode fields are non null, looks like Gson may still create null fields - if (TextUtils.isEmpty(vmessQRCode.add) || TextUtils.isEmpty(vmessQRCode.port) || TextUtils.isEmpty( - vmessQRCode.id - ) || TextUtils.isEmpty(vmessQRCode.net) - ) { - Log.d(AppConfig.ANG_PACKAGE, "R.string.toast_incorrect_protocol") - return null - } - - config.remarks = vmessQRCode.ps - config.outboundBean?.settings?.vnext?.get(0)?.let { vnext -> - vnext.address = vmessQRCode.add - vnext.port = Utils.parseInt(vmessQRCode.port) - vnext.users[0].id = vmessQRCode.id - vnext.users[0].security = - if (TextUtils.isEmpty(vmessQRCode.scy)) V2rayConfig.DEFAULT_SECURITY else vmessQRCode.scy - vnext.users[0].alterId = Utils.parseInt(vmessQRCode.aid) - } - val sni = streamSetting.populateTransportSettings( - vmessQRCode.net, - vmessQRCode.type, - vmessQRCode.host, - vmessQRCode.path, - vmessQRCode.path, - vmessQRCode.host, - vmessQRCode.path, - vmessQRCode.type, - vmessQRCode.path, - vmessQRCode.host - ) - - val fingerprint = vmessQRCode.fp ?: streamSetting.tlsSettings?.fingerprint - streamSetting.populateTlsSettings( - vmessQRCode.tls, - allowInsecure, - if (TextUtils.isEmpty(vmessQRCode.sni)) sni else vmessQRCode.sni, - fingerprint, - vmessQRCode.alpn, - null, - null, - null - ) - } + var result = str.replace(EConfigType.VMESS.protocolScheme, "") + result = Utils.decode(result) + if (TextUtils.isEmpty(result)) { + Log.d(AppConfig.ANG_PACKAGE, "R.string.toast_decoding_failed") + return null } + val vmessQRCode = Gson().fromJson(result, VmessQRCode::class.java) + // Although VmessQRCode fields are non null, looks like Gson may still create null fields + if (TextUtils.isEmpty(vmessQRCode.add) + || TextUtils.isEmpty(vmessQRCode.port) + || TextUtils.isEmpty(vmessQRCode.id) + || TextUtils.isEmpty(vmessQRCode.net) + ) { + Log.d(AppConfig.ANG_PACKAGE, "R.string.toast_incorrect_protocol") + return null + } + + config.remarks = vmessQRCode.ps + config.outboundBean.settings?.vnext?.get(0)?.let { vnext -> + vnext.address = vmessQRCode.add + vnext.port = Utils.parseInt(vmessQRCode.port) + vnext.users[0].id = vmessQRCode.id + vnext.users[0].security = + if (TextUtils.isEmpty(vmessQRCode.scy)) V2rayConfig.DEFAULT_SECURITY else vmessQRCode.scy + vnext.users[0].alterId = Utils.parseInt(vmessQRCode.aid) + } + val sni = streamSetting.populateTransportSettings( + vmessQRCode.net, + vmessQRCode.type, + vmessQRCode.host, + vmessQRCode.path, + vmessQRCode.path, + vmessQRCode.host, + vmessQRCode.path, + vmessQRCode.type, + vmessQRCode.path, + vmessQRCode.host + ) + + val fingerprint = vmessQRCode.fp + streamSetting.populateTlsSettings( + vmessQRCode.tls, + allowInsecure, + if (TextUtils.isEmpty(vmessQRCode.sni)) sni else vmessQRCode.sni, + fingerprint, + vmessQRCode.alpn, + null, + null, + null + ) + return config } @@ -114,80 +110,51 @@ object VmessFmt { return Utils.encode(json) } - private fun tryParseNewVmess( - uriString: String, config: ServerConfig, allowInsecure: Boolean - ): Boolean { - return runCatching { - val uri = URI(Utils.fixIllegalUrl(uriString)) - check(uri.scheme == "vmess") - val (_, protocol, tlsStr, uuid, alterId) = Regex("(tcp|http|ws|kcp|quic|grpc)(\\+tls)?:([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})").matchEntire( - uri.userInfo - )?.groupValues ?: error("parse user info fail.") - val tls = tlsStr.isNotBlank() - val queryParam = uri.rawQuery.split("&") - .associate { it.split("=").let { (k, v) -> k to Utils.urlDecode(v) } } + fun parseVmessStd(str: String): ServerConfig? { + var allowInsecure = settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false + val config = ServerConfig.create(EConfigType.VMESS) - val streamSetting = config.outboundBean?.streamSettings ?: return false - config.remarks = Utils.urlDecode(uri.fragment ?: "") - config.outboundBean.settings?.vnext?.get(0)?.let { vnext -> - vnext.address = uri.idnHost - vnext.port = uri.port - vnext.users[0].id = uuid - vnext.users[0].security = V2rayConfig.DEFAULT_SECURITY - vnext.users[0].alterId = alterId.toInt() - } - var fingerprint = streamSetting.tlsSettings?.fingerprint - val sni = streamSetting.populateTransportSettings(protocol, - queryParam["type"], - queryParam["host"]?.split("|")?.get(0) ?: "", - queryParam["path"]?.takeIf { it.trim() != "/" } ?: "", - queryParam["seed"], - queryParam["security"], - queryParam["key"], - queryParam["mode"], - queryParam["serviceName"], - queryParam["authority"]) - streamSetting.populateTlsSettings( - if (tls) V2rayConfig.TLS else "", - allowInsecure, - sni, - fingerprint, - null, - null, - null, - null - ) - true - }.getOrElse { false } - } + val uri = URI(Utils.fixIllegalUrl(str)) + if (uri.rawQuery.isNullOrEmpty()) return null + val queryParam = uri.rawQuery.split("&") + .associate { it.split("=").let { (k, v) -> k to Utils.urlDecode(v) } } - private fun tryResolveVmess4Kitsunebi(server: String, config: ServerConfig): Boolean { + val streamSetting = config.outboundBean?.streamSettings ?: return null - var result = server.replace(EConfigType.VMESS.protocolScheme, "") - val indexSplit = result.indexOf("?") - if (indexSplit > 0) { - result = result.substring(0, indexSplit) - } - result = Utils.decode(result) - - val arr1 = result.split('@') - if (arr1.count() != 2) { - return false - } - val arr21 = arr1[0].split(':') - val arr22 = arr1[1].split(':') - if (arr21.count() != 2) { - return false - } - - config.remarks = "Alien" - config.outboundBean?.settings?.vnext?.get(0)?.let { vnext -> - vnext.address = arr22[0] - vnext.port = Utils.parseInt(arr22[1]) - vnext.users[0].id = arr21[1] - vnext.users[0].security = arr21[0] + config.remarks = Utils.urlDecode(uri.fragment ?: "") + config.outboundBean.settings?.vnext?.get(0)?.let { vnext -> + vnext.address = uri.idnHost + vnext.port = uri.port + vnext.users[0].id = uri.userInfo + vnext.users[0].security = V2rayConfig.DEFAULT_SECURITY vnext.users[0].alterId = 0 } - return true + + val sni = streamSetting.populateTransportSettings( + queryParam["type"] ?: "tcp", + queryParam["headerType"], + queryParam["host"], + queryParam["path"], + queryParam["seed"], + queryParam["quicSecurity"], + queryParam["key"], + queryParam["mode"], + queryParam["serviceName"], + queryParam["authority"] + ) + + allowInsecure = if ((queryParam["allowInsecure"] ?: "") == "1") true else allowInsecure + streamSetting.populateTlsSettings( + queryParam["security"] ?: "", + allowInsecure, + queryParam["sni"] ?: sni, + queryParam["fp"] ?: "", + queryParam["alpn"], + null, + null, + null + ) + + return config } } \ No newline at end of file diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/WireguardFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/WireguardFmt.kt index 9c0b2bba..84efeb0e 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/WireguardFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/WireguardFmt.kt @@ -25,14 +25,15 @@ object WireguardFmt { ?: AppConfig.WIREGUARD_LOCAL_ADDRESS_V4).removeWhiteSpace() .split(",") wireguard.peers?.get(0)?.publicKey = queryParam["publickey"] ?: "" - wireguard.peers?.get(0)?.endpoint = Utils.getIpv6Address(uri.idnHost) + ":${uri.port}" + wireguard.peers?.get(0)?.endpoint = + Utils.getIpv6Address(uri.idnHost) + ":${uri.port}" wireguard.mtu = Utils.parseInt(queryParam["mtu"] ?: AppConfig.WIREGUARD_LOCAL_MTU) wireguard.reserved = (queryParam["reserved"] ?: "0,0,0").removeWhiteSpace().split(",") .map { it.toInt() } } return config - }else { + } else { return null } }