From 3144817795fe855cc633fd160d2c12afef11ddc4 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 20 Oct 2022 16:37:41 +0800 Subject: [PATCH] add tls alpn --- .../kotlin/com/v2ray/ang/dto/V2rayConfig.kt | 5 +++-- .../kotlin/com/v2ray/ang/dto/VmessQRCode.kt | 3 ++- .../kotlin/com/v2ray/ang/ui/ServerActivity.kt | 11 +++++++++- .../com/v2ray/ang/util/AngConfigManager.kt | 16 +++++++++----- .../main/kotlin/com/v2ray/ang/util/Utils.kt | 4 ++++ .../app/src/main/res/layout/tls_layout.xml | 22 ++++++++++++++++++- V2rayNG/app/src/main/res/values/arrays.xml | 7 ++++++ V2rayNG/app/src/main/res/values/strings.xml | 3 ++- 8 files changed, 59 insertions(+), 12 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt index fe5815c5..a5fa7859 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt @@ -259,12 +259,13 @@ data class V2rayConfig( return sni } - fun populateTlsSettings(streamSecurity: String, allowInsecure: Boolean, sni: String, fingerprint: String?) { + fun populateTlsSettings(streamSecurity: String, allowInsecure: Boolean, sni: String, fingerprint: String?, alpns: String?) { security = streamSecurity val tlsSetting = TlsSettingsBean( allowInsecure = allowInsecure, serverName = sni, - fingerprint = fingerprint + fingerprint = fingerprint, + alpn = if (alpns.isNullOrEmpty()) null else alpns.split(",").map { it.trim() }.filter { it.isNotEmpty() } ) if (security == TLS) { tlsSettings = tlsSetting diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/VmessQRCode.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/VmessQRCode.kt index 90d58a5a..48f3a341 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/VmessQRCode.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/VmessQRCode.kt @@ -12,4 +12,5 @@ data class VmessQRCode(var v: String = "", var host: String = "", var path: String = "", var tls: String = "", - var sni: String = "") \ No newline at end of file + var sni: String = "", + var alpn: String = "") \ No newline at end of file diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt index 3c5768de..73696caf 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerActivity.kt @@ -70,6 +70,9 @@ class ServerActivity : BaseActivity() { private val uTlsItems: Array by lazy { resources.getStringArray(R.array.streamsecurity_utls) } + private val alpns: Array by lazy { + resources.getStringArray(R.array.streamsecurity_alpn) + } // Kotlin synthetics was used, but since it is removed in 1.8. We switch to old manual approach. // We don't use AndroidViewBinding because, it is better to share similar logics for different // protocols. Use findViewById manually ensures the xml are de-coupled with the activity logic. @@ -90,6 +93,7 @@ class ServerActivity : BaseActivity() { private val sp_header_type_title: TextView? by lazy { findViewById(R.id.sp_header_type_title) } private val et_request_host: EditText? by lazy { findViewById(R.id.et_request_host) } private val et_path: EditText? by lazy { findViewById(R.id.et_path) } + private val sp_stream_alpn: Spinner? by lazy { findViewById(R.id.sp_stream_alpn) } //uTLS override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -178,6 +182,10 @@ class ServerActivity : BaseActivity() { val utlsIndex = Utils.arrayFind(uTlsItems, tlsSetting.fingerprint) sp_stream_fingerprint?.setSelection(utlsIndex) } + tlsSetting.alpn?.let { + val alpnIndex = Utils.arrayFind(alpns, Utils.removeWhiteSpace(tlsSetting.alpn.joinToString())!!) + sp_stream_alpn?.setSelection(alpnIndex) + } } } @@ -320,6 +328,7 @@ class ServerActivity : BaseActivity() { val allowInsecureField = sp_allow_insecure?.selectedItemPosition ?: return val streamSecurity = sp_stream_security?.selectedItemPosition ?: return var utlsIndex = sp_stream_fingerprint?.selectedItemPosition ?: return + var alpnIndex = sp_stream_alpn?.selectedItemPosition ?: return var sni = streamSetting.populateTransportSettings( transport = networks[network], @@ -341,7 +350,7 @@ class ServerActivity : BaseActivity() { allowinsecures[allowInsecureField].toBoolean() } - streamSetting.populateTlsSettings(streamSecuritys[streamSecurity], allowInsecure, sni, uTlsItems[utlsIndex]) + streamSetting.populateTlsSettings(streamSecuritys[streamSecurity], allowInsecure, sni, uTlsItems[utlsIndex], alpns[alpnIndex]) } private fun transportTypes(network: String?): Array { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt index 43bff542..b93ac35f 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt @@ -145,7 +145,7 @@ object AngConfigManager { } var fingerprint = streamSetting.tlsSettings?.fingerprint streamSetting.populateTlsSettings(vmessBean.streamSecurity, allowInsecure, - vmessBean.sni.ifBlank { sni }, fingerprint) + vmessBean.sni.ifBlank { sni }, fingerprint, null) } } val key = MmkvManager.encodeServerConfig(vmessBean.guid, config) @@ -223,7 +223,7 @@ object AngConfigManager { streamSetting.populateTlsSettings(vmessQRCode.tls, allowInsecure, - if (TextUtils.isEmpty(vmessQRCode.sni)) sni else vmessQRCode.sni, fingerprint) + if (TextUtils.isEmpty(vmessQRCode.sni)) sni else vmessQRCode.sni, fingerprint, vmessQRCode.alpn) } } } else if (str.startsWith(EConfigType.SHADOWSOCKS.protocolScheme)) { @@ -306,11 +306,11 @@ object AngConfigManager { 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"]) - config.outboundBean?.streamSettings?.populateTlsSettings(queryParam["security"] ?: TLS, allowInsecure, queryParam["sni"] ?: sni!!, fingerprint) + config.outboundBean?.streamSettings?.populateTlsSettings(queryParam["security"] ?: TLS, allowInsecure, queryParam["sni"] ?: sni!!, fingerprint, queryParam["alpn"]) flow = queryParam["flow"] ?: "" } else { - config.outboundBean?.streamSettings?.populateTlsSettings(TLS, allowInsecure, "", fingerprint) + config.outboundBean?.streamSettings?.populateTlsSettings(TLS, allowInsecure, "", fingerprint, null) } config.outboundBean?.settings?.servers?.get(0)?.let { server -> @@ -339,7 +339,7 @@ object AngConfigManager { val sni = streamSetting.populateTransportSettings(queryParam["type"] ?: "tcp", queryParam["headerType"], queryParam["host"], queryParam["path"], queryParam["seed"], queryParam["quicSecurity"], queryParam["key"], queryParam["mode"], queryParam["serviceName"]) - streamSetting.populateTlsSettings(queryParam["security"] ?: "", allowInsecure, queryParam["sni"] ?: sni, fingerprint) + streamSetting.populateTlsSettings(queryParam["security"] ?: "", allowInsecure, queryParam["sni"] ?: sni, fingerprint, queryParam["alpn"]) } if (config == null){ return R.string.toast_incorrect_protocol @@ -384,7 +384,7 @@ object AngConfigManager { queryParam["host"]?.split("|")?.get(0) ?: "", queryParam["path"]?.takeIf { it.trim() != "/" } ?: "", queryParam["seed"], queryParam["security"], queryParam["key"], queryParam["mode"], queryParam["serviceName"]) - streamSetting.populateTlsSettings(if (tls) TLS else "", allowInsecure, sni, fingerprint) + streamSetting.populateTlsSettings(if (tls) TLS else "", allowInsecure, sni, fingerprint, null) true }.getOrElse { false } } @@ -477,6 +477,7 @@ object AngConfigManager { vmessQRCode.net = streamSetting.network vmessQRCode.tls = streamSetting.security vmessQRCode.sni = streamSetting.tlsSettings?.serverName.orEmpty() + vmessQRCode.alpn = Utils.removeWhiteSpace(streamSetting.tlsSettings?.alpn?.joinToString()).orEmpty() outbound.getTransportSettingDetails()?.let { transportDetails -> vmessQRCode.type = transportDetails[0] vmessQRCode.host = transportDetails[1] @@ -531,6 +532,9 @@ object AngConfigManager { if (!TextUtils.isEmpty(tlsSetting.serverName)) { dicQuery["sni"] = tlsSetting.serverName } + if (!tlsSetting.alpn.isNullOrEmpty() && tlsSetting.alpn.isNotEmpty()) { + dicQuery["alpn"] = Utils.removeWhiteSpace(tlsSetting.alpn.joinToString()).orEmpty() + } } dicQuery["type"] = streamSetting.network.ifEmpty { V2rayConfig.DEFAULT_NETWORK } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index 469e82a4..977a2a9d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -424,5 +424,9 @@ object Utils { .replace(" ","%20") .replace("|","%7C") } + + fun removeWhiteSpace(str: String?): String? { + return str?.replace(" ", "") + } } diff --git a/V2rayNG/app/src/main/res/layout/tls_layout.xml b/V2rayNG/app/src/main/res/layout/tls_layout.xml index 9596b5ff..2f1f31f7 100644 --- a/V2rayNG/app/src/main/res/layout/tls_layout.xml +++ b/V2rayNG/app/src/main/res/layout/tls_layout.xml @@ -64,13 +64,33 @@ android:layout_width="match_parent" android:layout_height="@dimen/edit_height" android:entries="@array/streamsecurity_utls" - android:nextFocusDown="@+id/sp_allow_insecure" /> + android:nextFocusDown="@+id/sp_stream_alpn" /> + + + + + + + randomized + + + h2 + http/1.1 + h2,http/1.1 + + true diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 258d275a..4f780629 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -49,7 +49,8 @@ request host(host/ws host/h2 host)/QUIC security path(ws path/h2 path)/QUIC key/kcp seed/gRPC serviceName tls - uTLS + uTLS + alpn allowInsecure SNI address