Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Nov 19, 2024
1 parent e2c1081 commit 5695c17
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 51 deletions.
51 changes: 32 additions & 19 deletions V2rayNG/app/src/main/java/com/v2ray/ang/dto/V2rayConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,21 @@ data class V2rayConfig(
}

fun populateTransportSettings(
transport: String, headerType: String?, host: String?, path: String?, seed: String?,
quicSecurity: String?, key: String?, mode: String?, serviceName: String?,
transport: String,
headerType: String?,
host: String?,
path: String?,
seed: String?,
quicSecurity: String?,
key: String?,
mode: String?,
serviceName: String?,
authority: String?
): String? {
var sni: String? = null
network = transport
when (network) {
"tcp" -> {
NetworkType.TCP.type -> {
val tcpSetting = TcpSettingsBean()
if (headerType == AppConfig.HEADER_TYPE_HTTP) {
tcpSetting.header.type = AppConfig.HEADER_TYPE_HTTP
Expand All @@ -369,7 +376,7 @@ data class V2rayConfig(
tcpSettings = tcpSetting
}

"kcp" -> {
NetworkType.KCP.type -> {
val kcpsetting = KcpSettingsBean()
kcpsetting.header.type = headerType ?: "none"
if (seed.isNullOrEmpty()) {
Expand All @@ -380,32 +387,32 @@ data class V2rayConfig(
kcpSettings = kcpsetting
}

"ws" -> {
NetworkType.WS.type -> {
val wssetting = WsSettingsBean()
wssetting.headers.Host = host.orEmpty()
sni = wssetting.headers.Host
wssetting.path = path ?: "/"
wsSettings = wssetting
}

"httpupgrade" -> {
NetworkType.HTTP_UPGRADE.type -> {
val httpupgradeSetting = HttpupgradeSettingsBean()
httpupgradeSetting.host = host.orEmpty()
sni = httpupgradeSetting.host
httpupgradeSetting.path = path ?: "/"
httpupgradeSettings = httpupgradeSetting
}

"splithttp","xhttp" -> {
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> {
val xhttpSetting = XhttpSettingsBean()
xhttpSetting.host = host.orEmpty()
sni = xhttpSetting.host
xhttpSetting.path = path ?: "/"
xhttpSettings = xhttpSetting
}

"h2", "http" -> {
network = "h2"
NetworkType.H2.type, NetworkType.HTTP.type -> {
network = NetworkType.H2.type
val h2Setting = HttpSettingsBean()
h2Setting.host = host.orEmpty().split(",").map { it.trim() }.filter { it.isNotEmpty() }
sni = h2Setting.host.getOrNull(0) ?: sni
Expand All @@ -421,7 +428,7 @@ data class V2rayConfig(
// quicSettings = quicsetting
// }

"grpc" -> {
NetworkType.GRPC.type -> {
val grpcSetting = GrpcSettingsBean()
grpcSetting.multiMode = mode == "multi"
grpcSetting.serviceName = serviceName.orEmpty()
Expand All @@ -436,8 +443,14 @@ data class V2rayConfig(
}

fun populateTlsSettings(
streamSecurity: String, allowInsecure: Boolean, sni: String?, fingerprint: String?, alpns: String?,
publicKey: String?, shortId: String?, spiderX: String?
streamSecurity: String,
allowInsecure: Boolean,
sni: String?,
fingerprint: String?,
alpns: String?,
publicKey: String?,
shortId: String?,
spiderX: String?
) {
security = streamSecurity
val tlsSetting = TlsSettingsBean(
Expand Down Expand Up @@ -545,7 +558,7 @@ data class V2rayConfig(
) {
val transport = streamSettings?.network ?: return null
return when (transport) {
"tcp" -> {
NetworkType.TCP.type -> {
val tcpSetting = streamSettings?.tcpSettings ?: return null
listOf(
tcpSetting.header.type,
Expand All @@ -554,7 +567,7 @@ data class V2rayConfig(
)
}

"kcp" -> {
NetworkType.KCP.type -> {
val kcpSetting = streamSettings?.kcpSettings ?: return null
listOf(
kcpSetting.header.type,
Expand All @@ -563,7 +576,7 @@ data class V2rayConfig(
)
}

"ws" -> {
NetworkType.WS.type -> {
val wsSetting = streamSettings?.wsSettings ?: return null
listOf(
"",
Expand All @@ -572,7 +585,7 @@ data class V2rayConfig(
)
}

"httpupgrade" -> {
NetworkType.HTTP_UPGRADE.type -> {
val httpupgradeSetting = streamSettings?.httpupgradeSettings ?: return null
listOf(
"",
Expand All @@ -581,7 +594,7 @@ data class V2rayConfig(
)
}

"splithttp" ,"xhttp"-> {
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> {
val xhttpSettings = streamSettings?.xhttpSettings ?: return null
listOf(
"",
Expand All @@ -590,7 +603,7 @@ data class V2rayConfig(
)
}

"h2" -> {
NetworkType.H2.type -> {
val h2Setting = streamSettings?.httpSettings ?: return null
listOf(
"",
Expand All @@ -608,7 +621,7 @@ data class V2rayConfig(
// )
// }

"grpc" -> {
NetworkType.GRPC.type -> {
val grpcSetting = streamSettings?.grpcSettings ?: return null
listOf(
if (grpcSetting.multiMode == true) "multi" else "gun",
Expand Down
5 changes: 3 additions & 2 deletions V2rayNG/app/src/main/java/com/v2ray/ang/fmt/FmtBase.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand Down Expand Up @@ -31,7 +30,9 @@ open class FmtBase {
}

fun getItemFormQuery(config: ProfileItem, queryParam: Map<String, String>, allowInsecure: Boolean) {
config.network = queryParam["type"] ?: "tcp"
config.network = queryParam["type"] ?: NetworkType.TCP.type
//TODO
if (config.network == NetworkType.SPLIT_HTTP.type) config.network = NetworkType.XHTTP.type
config.headerType = queryParam["headerType"]
config.host = queryParam["host"]
config.path = queryParam["path"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.v2ray.ang.fmt

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.extension.idnHost
Expand Down Expand Up @@ -38,7 +39,7 @@ object ShadowsocksFmt : FmtBase() {
val queryParam = getQueryParam(uri)

if (queryParam["plugin"] == "obfs-local" && queryParam["obfs"] == "http") {
config.network = "tcp"
config.network = NetworkType.TCP.type
config.headerType = "http"
config.host = queryParam["obfs-host"]
config.path = queryParam["path"]
Expand Down
3 changes: 2 additions & 1 deletion V2rayNG/app/src/main/java/com/v2ray/ang/fmt/TrojanFmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.v2ray.ang.fmt

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.extension.idnHost
Expand All @@ -22,7 +23,7 @@ object TrojanFmt : FmtBase() {
config.password = uri.userInfo

if (uri.rawQuery.isNullOrEmpty()) {
config.network = "tcp"
config.network = NetworkType.TCP.type
config.security = AppConfig.TLS
config.insecure = allowInsecure
} else {
Expand Down
2 changes: 1 addition & 1 deletion V2rayNG/app/src/main/java/com/v2ray/ang/fmt/VmessFmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object VmessFmt : FmtBase() {
config.password = vmessQRCode.id
config.method = if (TextUtils.isEmpty(vmessQRCode.scy)) AppConfig.DEFAULT_SECURITY else vmessQRCode.scy

config.network = vmessQRCode.net ?: "tcp"
config.network = vmessQRCode.net ?: NetworkType.TCP.type
config.headerType = vmessQRCode.type
config.host = vmessQRCode.host
config.path = vmessQRCode.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tencent.mmkv.MMKV
import com.v2ray.ang.AppConfig
import com.v2ray.ang.AppConfig.ANG_PACKAGE
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.dto.NetworkType
import com.v2ray.ang.dto.ProfileItem
import com.v2ray.ang.dto.ServerConfig
import com.v2ray.ang.handler.MmkvManager.decodeServerConfig
Expand Down Expand Up @@ -72,7 +73,7 @@ object MigrateManager {
config.password = outbound.getPassword()
config.flow = outbound?.settings?.vnext?.first()?.users?.first()?.flow ?: outbound?.settings?.servers?.first()?.flow

config.network = outbound?.streamSettings?.network ?: "tcp"
config.network = outbound?.streamSettings?.network ?: NetworkType.TCP.type
outbound.getTransportSettingDetails()?.let { transportDetails ->
config.headerType = transportDetails[0].orEmpty()
config.host = transportDetails[1].orEmpty()
Expand Down
53 changes: 27 additions & 26 deletions V2rayNG/app/src/main/java/com/v2ray/ang/ui/ServerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_ADDRESS_V6
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_MTU
import com.v2ray.ang.R
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.dto.NetworkType
import com.v2ray.ang.dto.ProfileItem
import com.v2ray.ang.extension.toast
import com.v2ray.ang.handler.MmkvManager
Expand Down Expand Up @@ -158,16 +159,16 @@ class ServerActivity : BaseActivity() {
sp_header_type?.adapter = adapter
sp_header_type_title?.text =
when (networks[position]) {
"grpc" -> getString(R.string.server_lab_mode_type)
"xhttp" -> getString(R.string.server_lab_xhttp_mode)
NetworkType.GRPC.type -> getString(R.string.server_lab_mode_type)
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> getString(R.string.server_lab_xhttp_mode)
else -> getString(R.string.server_lab_head_type)
}.orEmpty()
sp_header_type?.setSelection(
Utils.arrayFind(
types,
when (networks[position]) {
"grpc" -> config?.mode
"xhttp" -> config?.xhttpMode
NetworkType.GRPC.type -> config?.mode
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> config?.xhttpMode
else -> config?.headerType
}.orEmpty()
)
Expand All @@ -176,29 +177,29 @@ class ServerActivity : BaseActivity() {
et_request_host?.text = Utils.getEditable(
when (networks[position]) {
//"quic" -> config?.quicSecurity
"grpc" -> config?.authority
NetworkType.GRPC.type -> config?.authority
else -> config?.host
}.orEmpty()
)
et_path?.text = Utils.getEditable(
when (networks[position]) {
"kcp" -> config?.seed
NetworkType.KCP.type -> config?.seed
//"quic" -> config?.quicKey
"grpc" -> config?.serviceName
NetworkType.GRPC.type -> config?.serviceName
else -> config?.path
}.orEmpty()
)

tv_request_host?.text = Utils.getEditable(
getString(
when (networks[position]) {
"tcp" -> R.string.server_lab_request_host_http
"ws" -> R.string.server_lab_request_host_ws
"httpupgrade" -> R.string.server_lab_request_host_httpupgrade
"splithttp", "xhttp" -> R.string.server_lab_request_host_xhttp
"h2" -> R.string.server_lab_request_host_h2
NetworkType.TCP.type -> R.string.server_lab_request_host_http
NetworkType.WS.type -> R.string.server_lab_request_host_ws
NetworkType.HTTP_UPGRADE.type -> R.string.server_lab_request_host_httpupgrade
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> R.string.server_lab_request_host_xhttp
NetworkType.H2.type -> R.string.server_lab_request_host_h2
//"quic" -> R.string.server_lab_request_host_quic
"grpc" -> R.string.server_lab_request_host_grpc
NetworkType.GRPC.type -> R.string.server_lab_request_host_grpc
else -> R.string.server_lab_request_host
}
)
Expand All @@ -207,27 +208,27 @@ class ServerActivity : BaseActivity() {
tv_path?.text = Utils.getEditable(
getString(
when (networks[position]) {
"kcp" -> R.string.server_lab_path_kcp
"ws" -> R.string.server_lab_path_ws
"httpupgrade" -> R.string.server_lab_path_httpupgrade
"splithttp", "xhttp" -> R.string.server_lab_path_xhttp
"h2" -> R.string.server_lab_path_h2
NetworkType.KCP.type -> R.string.server_lab_path_kcp
NetworkType.WS.type -> R.string.server_lab_path_ws
NetworkType.HTTP_UPGRADE.type -> R.string.server_lab_path_httpupgrade
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> R.string.server_lab_path_xhttp
NetworkType.H2.type -> R.string.server_lab_path_h2
//"quic" -> R.string.server_lab_path_quic
"grpc" -> R.string.server_lab_path_grpc
NetworkType.GRPC.type -> R.string.server_lab_path_grpc
else -> R.string.server_lab_path
}
)
)
et_extra?.text = Utils.getEditable(
when (networks[position]) {
"splithttp", "xhttp" -> JsonUtil.toJsonPretty(JsonUtil.parseString(config?.xhttpExtra))
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> JsonUtil.toJsonPretty(JsonUtil.parseString(config?.xhttpExtra))
else -> null
}.orEmpty()
)

layout_extra?.visibility =
when (networks[position]) {
"splithttp", "xhttp" -> View.VISIBLE
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> View.VISIBLE
else -> View.GONE
}
}
Expand Down Expand Up @@ -475,7 +476,7 @@ class ServerActivity : BaseActivity() {
if (config.subscriptionId.isEmpty() && !subscriptionId.isNullOrEmpty()) {
config.subscriptionId = subscriptionId.orEmpty()
}
Log.d(ANG_PACKAGE, JsonUtil.toJsonPretty(config)?:"")
Log.d(ANG_PACKAGE, JsonUtil.toJsonPretty(config) ?: "")
MmkvManager.encodeServerConfig(editGuid, config)
toast(R.string.toast_success)
finish()
Expand Down Expand Up @@ -564,19 +565,19 @@ class ServerActivity : BaseActivity() {

private fun transportTypes(network: String?): Array<out String> {
return when (network) {
"tcp" -> {
NetworkType.TCP.type -> {
tcpTypes
}

"kcp" -> {
NetworkType.KCP.type -> {
kcpAndQuicTypes
}

"grpc" -> {
NetworkType.GRPC.type -> {
grpcModes
}

"xhttp" -> {
NetworkType.SPLIT_HTTP.type, NetworkType.XHTTP.type -> {
xhttpMode
}

Expand Down

0 comments on commit 5695c17

Please sign in to comment.