From 723ab70170d9512d25353e8e12ece3bb0c242ddc Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 16 Apr 2024 10:29:07 +0800 Subject: [PATCH] Remove function migrateLegacy() --- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 32 +- .../com/v2ray/ang/util/AngConfigManager.kt | 429 +++++++++--------- 2 files changed, 240 insertions(+), 221 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index b29657dce..715d31937 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -111,7 +111,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList setupViewModel() copyAssets() - migrateLegacy() + //migrateLegacy() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { RxPermissions(this) @@ -188,21 +188,21 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } } - private fun migrateLegacy() { - lifecycleScope.launch(Dispatchers.IO) { - val result = AngConfigManager.migrateLegacyConfig(this@MainActivity) - if (result != null) { - launch(Dispatchers.Main) { - if (result) { - toast(getString(R.string.migration_success)) - mainViewModel.reloadServerList() - } else { - toast(getString(R.string.migration_fail)) - } - } - } - } - } +// private fun migrateLegacy() { +// lifecycleScope.launch(Dispatchers.IO) { +// val result = AngConfigManager.migrateLegacyConfig(this@MainActivity) +// if (result != null) { +// launch(Dispatchers.Main) { +// if (result) { +// toast(getString(R.string.migration_success)) +// mainViewModel.reloadServerList() +// } else { +// toast(getString(R.string.migration_fail)) +// } +// } +// } +// } +// } fun startV2Ray() { if (mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER).isNullOrEmpty()) { 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 f9ec24b16..fe41a6654 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 @@ -1,35 +1,31 @@ package com.v2ray.ang.util import android.content.Context -import android.content.SharedPreferences import android.graphics.Bitmap import android.text.TextUtils import android.util.Log -import androidx.preference.PreferenceManager import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.JsonPrimitive import com.google.gson.JsonSerializationContext import com.google.gson.JsonSerializer -import com.google.gson.annotations.SerializedName import com.google.gson.reflect.TypeToken import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig -import com.v2ray.ang.AppConfig.ANG_CONFIG -import com.v2ray.ang.AppConfig.PROTOCOL_HTTPS import com.v2ray.ang.AppConfig.PROTOCOL_HTTP +import com.v2ray.ang.AppConfig.PROTOCOL_HTTPS import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_ADDRESS_V4 import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_MTU import com.v2ray.ang.R import com.v2ray.ang.dto.* import com.v2ray.ang.dto.V2rayConfig.Companion.DEFAULT_SECURITY import com.v2ray.ang.dto.V2rayConfig.Companion.TLS -import com.v2ray.ang.util.MmkvManager.KEY_SELECTED_SERVER -import java.net.URI -import java.util.* -import java.lang.reflect.Type import com.v2ray.ang.extension.idnHost import com.v2ray.ang.extension.removeWhiteSpace +import com.v2ray.ang.util.MmkvManager.KEY_SELECTED_SERVER +import java.lang.reflect.Type +import java.net.URI +import java.util.* object AngConfigManager { private val mainStorage by lazy { @@ -55,158 +51,158 @@ object AngConfigManager { /** * Legacy loading config */ - fun migrateLegacyConfig(c: Context): Boolean? { - try { - val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(c) - val context = defaultSharedPreferences.getString(ANG_CONFIG, "") - if (context.isNullOrBlank()) { - return null - } - val angConfig = Gson().fromJson(context, AngConfig::class.java) - for (i in angConfig.vmess.indices) { - upgradeServerVersion(angConfig.vmess[i]) - } - - copyLegacySettings(defaultSharedPreferences) - migrateVmessBean(angConfig, defaultSharedPreferences) - migrateSubItemBean(angConfig) - - defaultSharedPreferences.edit().remove(ANG_CONFIG).apply() - return true - } catch (e: Exception) { - e.printStackTrace() - } - return false - } - - private fun copyLegacySettings(sharedPreferences: SharedPreferences) { - listOf( - AppConfig.PREF_MODE, - AppConfig.PREF_REMOTE_DNS, - AppConfig.PREF_DOMESTIC_DNS, - AppConfig.PREF_LOCAL_DNS_PORT, - AppConfig.PREF_SOCKS_PORT, - AppConfig.PREF_HTTP_PORT, - AppConfig.PREF_LOGLEVEL, - AppConfig.PREF_ROUTING_DOMAIN_STRATEGY, - AppConfig.PREF_ROUTING_MODE, - AppConfig.PREF_V2RAY_ROUTING_AGENT, - AppConfig.PREF_V2RAY_ROUTING_BLOCKED, - AppConfig.PREF_V2RAY_ROUTING_DIRECT, - ).forEach { key -> - settingsStorage?.encode(key, sharedPreferences.getString(key, null)) - } - listOf( - AppConfig.PREF_SPEED_ENABLED, - AppConfig.PREF_PROXY_SHARING, - AppConfig.PREF_LOCAL_DNS_ENABLED, - AppConfig.PREF_ALLOW_INSECURE, - AppConfig.PREF_PREFER_IPV6, - AppConfig.PREF_PER_APP_PROXY, - AppConfig.PREF_BYPASS_APPS, - ).forEach { key -> - settingsStorage?.encode(key, sharedPreferences.getBoolean(key, false)) - } - settingsStorage?.encode( - AppConfig.PREF_SNIFFING_ENABLED, - sharedPreferences.getBoolean(AppConfig.PREF_SNIFFING_ENABLED, true) - ) - settingsStorage?.encode( - AppConfig.PREF_PER_APP_PROXY_SET, - sharedPreferences.getStringSet(AppConfig.PREF_PER_APP_PROXY_SET, setOf()) - ) - } - - private fun migrateVmessBean(angConfig: AngConfig, sharedPreferences: SharedPreferences) { - angConfig.vmess.forEachIndexed { index, vmessBean -> - val type = EConfigType.fromInt(vmessBean.configType) ?: return@forEachIndexed - val config = ServerConfig.create(type) - config.remarks = vmessBean.remarks - config.subscriptionId = vmessBean.subid - if (type == EConfigType.CUSTOM) { - val jsonConfig = sharedPreferences.getString(ANG_CONFIG + vmessBean.guid, "") - val v2rayConfig = try { - Gson().fromJson(jsonConfig, V2rayConfig::class.java) - } catch (e: Exception) { - e.printStackTrace() - return@forEachIndexed - } - config.fullConfig = v2rayConfig - serverRawStorage?.encode(vmessBean.guid, jsonConfig) - } else { - config.outboundBean?.settings?.vnext?.get(0)?.let { vnext -> - vnext.address = vmessBean.address - vnext.port = vmessBean.port - vnext.users[0].id = vmessBean.id - if (config.configType == EConfigType.VMESS) { - vnext.users[0].alterId = vmessBean.alterId - vnext.users[0].security = vmessBean.security - } else if (config.configType == EConfigType.VLESS) { - vnext.users[0].encryption = vmessBean.security - vnext.users[0].flow = vmessBean.flow - } - } - config.outboundBean?.settings?.servers?.get(0)?.let { server -> - server.address = vmessBean.address - server.port = vmessBean.port - if (config.configType == EConfigType.SHADOWSOCKS) { - server.password = vmessBean.id - server.method = vmessBean.security - } else if (config.configType == EConfigType.SOCKS) { - if (TextUtils.isEmpty(vmessBean.security) && TextUtils.isEmpty(vmessBean.id)) { - server.users = null - } else { - val socksUsersBean = - V2rayConfig.OutboundBean.OutSettingsBean.ServersBean.SocksUsersBean() - socksUsersBean.user = vmessBean.security - socksUsersBean.pass = vmessBean.id - server.users = listOf(socksUsersBean) - } - } else if (config.configType == EConfigType.TROJAN) { - server.password = vmessBean.id - } - } - config.outboundBean?.streamSettings?.let { streamSetting -> - val sni = streamSetting.populateTransportSettings( - vmessBean.network, - vmessBean.headerType, - vmessBean.requestHost, - vmessBean.path, - vmessBean.path, - vmessBean.requestHost, - vmessBean.path, - vmessBean.headerType, - vmessBean.path, - vmessBean.requestHost, - ) - val allowInsecure = if (vmessBean.allowInsecure.isBlank()) { - settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false - } else { - vmessBean.allowInsecure.toBoolean() - } - var fingerprint = streamSetting.tlsSettings?.fingerprint - streamSetting.populateTlsSettings( - vmessBean.streamSecurity, allowInsecure, - vmessBean.sni.ifBlank { sni }, fingerprint, null, null, null, null - ) - } - } - val key = MmkvManager.encodeServerConfig(vmessBean.guid, config) - if (index == angConfig.index) { - mainStorage?.encode(KEY_SELECTED_SERVER, key) - } - } - } - - private fun migrateSubItemBean(angConfig: AngConfig) { - angConfig.subItem.forEach { - val subItem = SubscriptionItem() - subItem.remarks = it.remarks - subItem.url = it.url - subItem.enabled = it.enabled - subStorage?.encode(it.id, Gson().toJson(subItem)) - } - } +// fun migrateLegacyConfig(c: Context): Boolean? { +// try { +// val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(c) +// val context = defaultSharedPreferences.getString(ANG_CONFIG, "") +// if (context.isNullOrBlank()) { +// return null +// } +// val angConfig = Gson().fromJson(context, AngConfig::class.java) +// for (i in angConfig.vmess.indices) { +// upgradeServerVersion(angConfig.vmess[i]) +// } +// +// copyLegacySettings(defaultSharedPreferences) +// migrateVmessBean(angConfig, defaultSharedPreferences) +// migrateSubItemBean(angConfig) +// +// defaultSharedPreferences.edit().remove(ANG_CONFIG).apply() +// return true +// } catch (e: Exception) { +// e.printStackTrace() +// } +// return false +// } +// +// private fun copyLegacySettings(sharedPreferences: SharedPreferences) { +// listOf( +// AppConfig.PREF_MODE, +// AppConfig.PREF_REMOTE_DNS, +// AppConfig.PREF_DOMESTIC_DNS, +// AppConfig.PREF_LOCAL_DNS_PORT, +// AppConfig.PREF_SOCKS_PORT, +// AppConfig.PREF_HTTP_PORT, +// AppConfig.PREF_LOGLEVEL, +// AppConfig.PREF_ROUTING_DOMAIN_STRATEGY, +// AppConfig.PREF_ROUTING_MODE, +// AppConfig.PREF_V2RAY_ROUTING_AGENT, +// AppConfig.PREF_V2RAY_ROUTING_BLOCKED, +// AppConfig.PREF_V2RAY_ROUTING_DIRECT, +// ).forEach { key -> +// settingsStorage?.encode(key, sharedPreferences.getString(key, null)) +// } +// listOf( +// AppConfig.PREF_SPEED_ENABLED, +// AppConfig.PREF_PROXY_SHARING, +// AppConfig.PREF_LOCAL_DNS_ENABLED, +// AppConfig.PREF_ALLOW_INSECURE, +// AppConfig.PREF_PREFER_IPV6, +// AppConfig.PREF_PER_APP_PROXY, +// AppConfig.PREF_BYPASS_APPS, +// ).forEach { key -> +// settingsStorage?.encode(key, sharedPreferences.getBoolean(key, false)) +// } +// settingsStorage?.encode( +// AppConfig.PREF_SNIFFING_ENABLED, +// sharedPreferences.getBoolean(AppConfig.PREF_SNIFFING_ENABLED, true) +// ) +// settingsStorage?.encode( +// AppConfig.PREF_PER_APP_PROXY_SET, +// sharedPreferences.getStringSet(AppConfig.PREF_PER_APP_PROXY_SET, setOf()) +// ) +// } +// +// private fun migrateVmessBean(angConfig: AngConfig, sharedPreferences: SharedPreferences) { +// angConfig.vmess.forEachIndexed { index, vmessBean -> +// val type = EConfigType.fromInt(vmessBean.configType) ?: return@forEachIndexed +// val config = ServerConfig.create(type) +// config.remarks = vmessBean.remarks +// config.subscriptionId = vmessBean.subid +// if (type == EConfigType.CUSTOM) { +// val jsonConfig = sharedPreferences.getString(ANG_CONFIG + vmessBean.guid, "") +// val v2rayConfig = try { +// Gson().fromJson(jsonConfig, V2rayConfig::class.java) +// } catch (e: Exception) { +// e.printStackTrace() +// return@forEachIndexed +// } +// config.fullConfig = v2rayConfig +// serverRawStorage?.encode(vmessBean.guid, jsonConfig) +// } else { +// config.outboundBean?.settings?.vnext?.get(0)?.let { vnext -> +// vnext.address = vmessBean.address +// vnext.port = vmessBean.port +// vnext.users[0].id = vmessBean.id +// if (config.configType == EConfigType.VMESS) { +// vnext.users[0].alterId = vmessBean.alterId +// vnext.users[0].security = vmessBean.security +// } else if (config.configType == EConfigType.VLESS) { +// vnext.users[0].encryption = vmessBean.security +// vnext.users[0].flow = vmessBean.flow +// } +// } +// config.outboundBean?.settings?.servers?.get(0)?.let { server -> +// server.address = vmessBean.address +// server.port = vmessBean.port +// if (config.configType == EConfigType.SHADOWSOCKS) { +// server.password = vmessBean.id +// server.method = vmessBean.security +// } else if (config.configType == EConfigType.SOCKS) { +// if (TextUtils.isEmpty(vmessBean.security) && TextUtils.isEmpty(vmessBean.id)) { +// server.users = null +// } else { +// val socksUsersBean = +// V2rayConfig.OutboundBean.OutSettingsBean.ServersBean.SocksUsersBean() +// socksUsersBean.user = vmessBean.security +// socksUsersBean.pass = vmessBean.id +// server.users = listOf(socksUsersBean) +// } +// } else if (config.configType == EConfigType.TROJAN) { +// server.password = vmessBean.id +// } +// } +// config.outboundBean?.streamSettings?.let { streamSetting -> +// val sni = streamSetting.populateTransportSettings( +// vmessBean.network, +// vmessBean.headerType, +// vmessBean.requestHost, +// vmessBean.path, +// vmessBean.path, +// vmessBean.requestHost, +// vmessBean.path, +// vmessBean.headerType, +// vmessBean.path, +// vmessBean.requestHost, +// ) +// val allowInsecure = if (vmessBean.allowInsecure.isBlank()) { +// settingsStorage?.decodeBool(AppConfig.PREF_ALLOW_INSECURE) ?: false +// } else { +// vmessBean.allowInsecure.toBoolean() +// } +// var fingerprint = streamSetting.tlsSettings?.fingerprint +// streamSetting.populateTlsSettings( +// vmessBean.streamSecurity, allowInsecure, +// vmessBean.sni.ifBlank { sni }, fingerprint, null, null, null, null +// ) +// } +// } +// val key = MmkvManager.encodeServerConfig(vmessBean.guid, config) +// if (index == angConfig.index) { +// mainStorage?.encode(KEY_SELECTED_SERVER, key) +// } +// } +// } +// +// private fun migrateSubItemBean(angConfig: AngConfig) { +// angConfig.subItem.forEach { +// val subItem = SubscriptionItem() +// subItem.remarks = it.remarks +// subItem.url = it.url +// subItem.enabled = it.enabled +// subStorage?.encode(it.id, Gson().toJson(subItem)) +// } +// } /** * import config form qrcode or... @@ -599,14 +595,24 @@ object AngConfigManager { if (idx == -1) { queryPairs[Utils.urlDecode(pair)] = ""; } else { - queryPairs[Utils.urlDecode(pair.substring(0, idx))] = Utils.urlDecode(pair.substring(idx + 1)) + queryPairs[Utils.urlDecode(pair.substring(0, idx))] = + Utils.urlDecode(pair.substring(idx + 1)) } } Log.d(AppConfig.ANG_PACKAGE, queryPairs.toString()) var sni: String? = "" if (queryPairs["plugin"] == "obfs-local" && queryPairs["obfs"] == "http") { sni = config.outboundBean?.streamSettings?.populateTransportSettings( - "tcp", "http", queryPairs["obfs-host"], queryPairs["path"], null, null, null, null, null, null + "tcp", + "http", + queryPairs["obfs-host"], + queryPairs["path"], + null, + null, + null, + null, + null, + null ) } else if (queryPairs["plugin"] == "v2ray-plugin") { var network = "ws"; @@ -614,7 +620,16 @@ object AngConfigManager { network = "quic"; } sni = config.outboundBean?.streamSettings?.populateTransportSettings( - network, null, queryPairs["host"], queryPairs["path"], null, null, null, null, null, null + network, + null, + queryPairs["host"], + queryPairs["path"], + null, + null, + null, + null, + null, + null ) } if ("tls" in queryPairs) { @@ -622,9 +637,9 @@ object AngConfigManager { "tls", false, sni ?: "", null, null, null, null, null ) } - + } - + config.outboundBean?.settings?.servers?.get(0)?.let { server -> server.address = uri.idnHost server.port = uri.port @@ -935,38 +950,38 @@ object AngConfigManager { return 0 } - /** - * upgrade - */ - private fun upgradeServerVersion(vmess: AngConfig.VmessBean): Int { - try { - if (vmess.configVersion == 2) { - return 0 - } - - when (vmess.network) { - "ws", "h2" -> { - var path = "" - var host = "" - val lstParameter = vmess.requestHost.split(";") - if (lstParameter.isNotEmpty()) { - path = lstParameter[0].trim() - } - if (lstParameter.size > 1) { - path = lstParameter[0].trim() - host = lstParameter[1].trim() - } - vmess.path = path - vmess.requestHost = host - } - } - vmess.configVersion = 2 - return 0 - } catch (e: Exception) { - e.printStackTrace() - return -1 - } - } +// /** +// * upgrade +// */ +// private fun upgradeServerVersion(vmess: AngConfig.VmessBean): Int { +// try { +// if (vmess.configVersion == 2) { +// return 0 +// } +// +// when (vmess.network) { +// "ws", "h2" -> { +// var path = "" +// var host = "" +// val lstParameter = vmess.requestHost.split(";") +// if (lstParameter.isNotEmpty()) { +// path = lstParameter[0].trim() +// } +// if (lstParameter.size > 1) { +// path = lstParameter[0].trim() +// host = lstParameter[1].trim() +// } +// vmess.path = path +// vmess.requestHost = host +// } +// } +// vmess.configVersion = 2 +// return 0 +// } catch (e: Exception) { +// e.printStackTrace() +// return -1 +// } +// } fun importBatchConfig(servers: String?, subid: String, append: Boolean): Int { try { @@ -1038,15 +1053,19 @@ object AngConfigManager { try { //val gson = GsonBuilder().setPrettyPrinting().create() val gson = GsonBuilder() - .setPrettyPrinting() - .disableHtmlEscaping() - .registerTypeAdapter( // custom serialiser is needed here since JSON by default parse number as Double, core will fail to start - object : TypeToken() {}.type, - JsonSerializer { src: Double?, _: Type?, _: JsonSerializationContext? -> JsonPrimitive(src?.toInt()) } + .setPrettyPrinting() + .disableHtmlEscaping() + .registerTypeAdapter( // custom serialiser is needed here since JSON by default parse number as Double, core will fail to start + object : TypeToken() {}.type, + JsonSerializer { src: Double?, _: Type?, _: JsonSerializationContext? -> + JsonPrimitive( + src?.toInt() ) - .create() + } + ) + .create() val serverList: Array = - Gson().fromJson(server, Array::class.java) + Gson().fromJson(server, Array::class.java) if (serverList.isNotEmpty()) { var count = 0