From 93eb9fe3b9454a3389fac265e82953eeec38f006 Mon Sep 17 00:00:00 2001 From: Tamim Hossain <132823494+CodeWithTamim@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:56:04 +0600 Subject: [PATCH] Introduce NetworkType enum to improve network type handling (#3873) * Introduce NetworkType enum to improve network type handling Created a `NetworkType` enum to represent various network types, improving readability and reducing potential errors caused by hardcoded string comparisons. Updated the `getQueryDic` function to utilize this enum. * Refactor to use NetworkType enum in VmessFmt Replaced hardcoded network type strings with the `NetworkType` enum in `VmessFmt` functions. Updated `parse`, `toUri`, and `parseVmessStd` methods to use `NetworkType.fromString`, improving readability and reducing errors caused by typos in network type strings. --- .../kotlin/com/v2ray/ang/dto/NetworkType.kt | 17 +++++++++++++ .../main/kotlin/com/v2ray/ang/fmt/FmtBase.kt | 19 +++++++------- .../main/kotlin/com/v2ray/ang/fmt/VmessFmt.kt | 25 +++++++++++-------- 3 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/NetworkType.kt diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/NetworkType.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/NetworkType.kt new file mode 100644 index 00000000..5e4b5dbe --- /dev/null +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/NetworkType.kt @@ -0,0 +1,17 @@ +package com.v2ray.ang.dto + +enum class NetworkType(val type: String) { + TCP("tcp"), + KCP("kcp"), + WS("ws"), + HTTP_UPGRADE("httpupgrade"), + SPLIT_HTTP("splithttp"), + HTTP("http"), + H2("h2"), + QUIC("quic"), + GRPC("grpc"); + + companion object { + fun fromString(type: String?) = entries.find { it.type == type } ?: TCP + } +} diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/FmtBase.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/FmtBase.kt index 868deb26..a73eb615 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/FmtBase.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/FmtBase.kt @@ -1,6 +1,7 @@ package com.v2ray.ang.fmt import com.v2ray.ang.AppConfig +import com.v2ray.ang.dto.NetworkType import com.v2ray.ang.dto.ProfileItem import com.v2ray.ang.extension.isNotNullEmpty import com.v2ray.ang.util.Utils @@ -31,7 +32,6 @@ open class FmtBase { fun getQueryDic(config: ProfileItem): HashMap { val dicQuery = HashMap() - dicQuery["security"] = config.security?.ifEmpty { "none" }.orEmpty() config.sni.let { if (it.isNotNullEmpty()) dicQuery["sni"] = it.orEmpty() } config.alpn.let { if (it.isNotNullEmpty()) dicQuery["alpn"] = it.orEmpty() } @@ -41,37 +41,38 @@ open class FmtBase { config.spiderX.let { if (it.isNotNullEmpty()) dicQuery["spx"] = it.orEmpty() } config.flow.let { if (it.isNotNullEmpty()) dicQuery["flow"] = it.orEmpty() } - dicQuery["type"] = config.network?.ifEmpty { AppConfig.DEFAULT_NETWORK }.orEmpty() + val networkType = NetworkType.fromString(config.network) + dicQuery["type"] = networkType.type - when (config.network) { - "tcp" -> { + when (networkType) { + NetworkType.TCP -> { dicQuery["headerType"] = config.headerType?.ifEmpty { "none" }.orEmpty() config.host.let { if (it.isNotNullEmpty()) dicQuery["host"] = it.orEmpty() } } - "kcp" -> { + NetworkType.KCP -> { dicQuery["headerType"] = config.headerType?.ifEmpty { "none" }.orEmpty() config.seed.let { if (it.isNotNullEmpty()) dicQuery["seed"] = it.orEmpty() } } - "ws", "httpupgrade", "splithttp" -> { + NetworkType.WS, NetworkType.HTTP_UPGRADE, NetworkType.SPLIT_HTTP -> { config.host.let { if (it.isNotNullEmpty()) dicQuery["host"] = it.orEmpty() } config.path.let { if (it.isNotNullEmpty()) dicQuery["path"] = it.orEmpty() } } - "http", "h2" -> { + NetworkType.HTTP, NetworkType.H2 -> { dicQuery["type"] = "http" config.host.let { if (it.isNotNullEmpty()) dicQuery["host"] = it.orEmpty() } config.path.let { if (it.isNotNullEmpty()) dicQuery["path"] = it.orEmpty() } } - "quic" -> { + NetworkType.QUIC -> { dicQuery["headerType"] = config.headerType?.ifEmpty { "none" }.orEmpty() config.quicSecurity.let { if (it.isNotNullEmpty()) dicQuery["quicSecurity"] = it.orEmpty() } config.quicKey.let { if (it.isNotNullEmpty()) dicQuery["key"] = it.orEmpty() } } - "grpc" -> { + NetworkType.GRPC -> { config.mode.let { if (it.isNotNullEmpty()) dicQuery["mode"] = it.orEmpty() } config.authority.let { if (it.isNotNullEmpty()) dicQuery["authority"] = it.orEmpty() } config.serviceName.let { if (it.isNotNullEmpty()) dicQuery["serviceName"] = it.orEmpty() } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/VmessFmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/VmessFmt.kt index 6767ef5d..00ba2f7e 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/VmessFmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/fmt/VmessFmt.kt @@ -4,6 +4,7 @@ import android.text.TextUtils import android.util.Log import com.v2ray.ang.AppConfig import com.v2ray.ang.dto.EConfigType +import com.v2ray.ang.dto.NetworkType import com.v2ray.ang.dto.ProfileItem import com.v2ray.ang.dto.V2rayConfig.OutboundBean import com.v2ray.ang.dto.VmessQRCode @@ -52,22 +53,24 @@ object VmessFmt : FmtBase() { config.host = vmessQRCode.host config.path = vmessQRCode.path - when (config.network) { - "kcp" -> { + when (NetworkType.fromString(config.network)) { + NetworkType.KCP -> { config.seed = vmessQRCode.path } - "quic" -> { + NetworkType.QUIC -> { config.quicSecurity = vmessQRCode.host config.quicKey = vmessQRCode.path } - "grpc" -> { + NetworkType.GRPC -> { config.mode = vmessQRCode.type config.serviceName = vmessQRCode.path config.authority = vmessQRCode.host } + else -> {} } + config.security = vmessQRCode.tls config.insecure = allowInsecure config.sni = vmessQRCode.sni @@ -90,22 +93,24 @@ object VmessFmt : FmtBase() { vmessQRCode.net = config.network.orEmpty() vmessQRCode.type = config.headerType.orEmpty() - when (config.network) { - "kcp" -> { + when (NetworkType.fromString(config.network)) { + NetworkType.KCP -> { vmessQRCode.path = config.seed.orEmpty() } - "quic" -> { + NetworkType.QUIC -> { vmessQRCode.host = config.quicSecurity.orEmpty() vmessQRCode.path = config.quicKey.orEmpty() } - "grpc" -> { + NetworkType.GRPC -> { vmessQRCode.type = config.mode.orEmpty() vmessQRCode.path = config.serviceName.orEmpty() vmessQRCode.host = config.authority.orEmpty() } + else -> {} } + config.host.let { if (it.isNotNullEmpty()) vmessQRCode.host = it.orEmpty() } config.path.let { if (it.isNotNullEmpty()) vmessQRCode.path = it.orEmpty() } @@ -119,7 +124,7 @@ object VmessFmt : FmtBase() { } fun parseVmessStd(str: String): ProfileItem? { - var allowInsecure = MmkvManager.decodeSettingsBool(AppConfig.PREF_ALLOW_INSECURE, false) + val allowInsecure = MmkvManager.decodeSettingsBool(AppConfig.PREF_ALLOW_INSECURE, false) val config = ProfileItem.create(EConfigType.VMESS) val uri = URI(Utils.fixIllegalUrl(str)) @@ -132,7 +137,7 @@ object VmessFmt : FmtBase() { config.password = uri.userInfo config.method = AppConfig.DEFAULT_SECURITY - config.network = queryParam["type"] ?: "tcp" + config.network = NetworkType.fromString(queryParam["type"]).name config.headerType = queryParam["headerType"] config.host = queryParam["host"] config.path = queryParam["path"]