From 7ff1397163989ccfbd397e6d7557861d95ab157b Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 29 Mar 2025 10:43:25 +0800 Subject: [PATCH] Optimize the test true delay function When testing, remove unnecessary configurations such as dns and routing to reduce resource usage, except for custom configurations. --- .../java/com/v2ray/ang/dto/V2rayConfig.kt | 2 +- .../v2ray/ang/handler/V2rayConfigManager.kt | 132 +++++++++++++++--- .../com/v2ray/ang/service/V2RayTestService.kt | 6 +- 3 files changed, 116 insertions(+), 24 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/dto/V2rayConfig.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/V2rayConfig.kt index 14fe43c8..692caf69 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/dto/V2rayConfig.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/V2rayConfig.kt @@ -22,7 +22,7 @@ data class V2rayConfig( var policy: PolicyBean?, val inbounds: ArrayList, var outbounds: ArrayList, - var dns: DnsBean, + var dns: DnsBean? = null, val routing: RoutingBean, val api: Any? = null, val transport: Any? = null, diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt index b5e35807..f4818584 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt @@ -52,6 +52,7 @@ import com.v2ray.ang.util.JsonUtil import com.v2ray.ang.util.Utils object V2rayConfigManager { + private var initConfigCache: String? = null /** * Retrieves the V2ray configuration for the given GUID. @@ -63,16 +64,11 @@ object V2rayConfigManager { fun getV2rayConfig(context: Context, guid: String): ConfigResult { try { val config = MmkvManager.decodeServerConfig(guid) ?: return ConfigResult(false) - if (config.configType == EConfigType.CUSTOM) { - val raw = MmkvManager.decodeServerRaw(guid) ?: return ConfigResult(false) - val domainPort = config.getServerAddressAndPort() - return ConfigResult(true, guid, raw, domainPort) + return if (config.configType == EConfigType.CUSTOM) { + getV2rayCustomConfig(guid, config) + } else { + getV2rayNormalConfig(context, guid, config) } - - val result = getV2rayNonCustomConfig(context, config) - //Log.d(ANG_PACKAGE, result.content) - result.guid = guid - return result } catch (e: Exception) { e.printStackTrace() return ConfigResult(false) @@ -80,13 +76,48 @@ object V2rayConfigManager { } /** - * Retrieves the non-custom V2ray configuration. + * Retrieves the speedtest V2ray configuration for the given GUID. * - * @param context The context in which the function is called. + * @param context The context of the caller. + * @param guid The unique identifier for the V2ray configuration. + * @return A ConfigResult object containing the configuration details or indicating failure. + */ + fun getV2rayConfig4Speedtest(context: Context, guid: String): ConfigResult { + try { + val config = MmkvManager.decodeServerConfig(guid) ?: return ConfigResult(false) + return if (config.configType == EConfigType.CUSTOM) { + getV2rayCustomConfig(guid, config) + } else { + getV2rayNormalConfig4Speedtest(context, guid, config) + } + } catch (e: Exception) { + e.printStackTrace() + return ConfigResult(false) + } + } + + /** + * Retrieves the custom V2ray configuration. + * + * @param guid The unique identifier for the V2ray configuration. * @param config The profile item containing the configuration details. * @return A ConfigResult object containing the result of the configuration retrieval. */ - private fun getV2rayNonCustomConfig(context: Context, config: ProfileItem): ConfigResult { + private fun getV2rayCustomConfig(guid: String, config: ProfileItem): ConfigResult { + val raw = MmkvManager.decodeServerRaw(guid) ?: return ConfigResult(false) + val domainPort = config.getServerAddressAndPort() + return ConfigResult(true, guid, raw, domainPort) + } + + /** + * Retrieves the normal V2ray configuration. + * + * @param context The context in which the function is called. + * @param guid The unique identifier for the V2ray configuration. + * @param config The profile item containing the configuration details. + * @return A ConfigResult object containing the result of the configuration retrieval. + */ + private fun getV2rayNormalConfig(context: Context, guid: String, config: ProfileItem): ConfigResult { val result = ConfigResult(false) val address = config.server ?: return result @@ -97,13 +128,8 @@ object V2rayConfigManager { } } - val assets = Utils.readTextFromAssets(context, "v2ray_config.json") - if (TextUtils.isEmpty(assets)) { - return result - } - val v2rayConfig = JsonUtil.fromJson(assets, V2rayConfig::class.java) ?: return result - v2rayConfig.log.loglevel = - MmkvManager.decodeSettingsString(AppConfig.PREF_LOGLEVEL) ?: "warning" + val v2rayConfig = initV2rayConfig(context) ?: return result + v2rayConfig.log.loglevel = MmkvManager.decodeSettingsString(AppConfig.PREF_LOGLEVEL) ?: "warning" v2rayConfig.remarks = config.remarks inbounds(v2rayConfig) @@ -129,9 +155,75 @@ object V2rayConfigManager { result.status = true result.content = v2rayConfig.toPrettyPrinting() result.domainPort = if (retMore.first) retMore.second else retOut.second + result.guid = guid return result } + /** + * Retrieves the normal V2ray configuration for speedtest. + * + * @param context The context in which the function is called. + * @param guid The unique identifier for the V2ray configuration. + * @param config The profile item containing the configuration details. + * @return A ConfigResult object containing the result of the configuration retrieval. + */ + private fun getV2rayNormalConfig4Speedtest(context: Context, guid: String, config: ProfileItem): ConfigResult { + val result = ConfigResult(false) + + val address = config.server ?: return result + if (!Utils.isIpAddress(address)) { + if (!Utils.isValidUrl(address)) { + Log.d(ANG_PACKAGE, "$address is an invalid ip or domain") + return result + } + } + + val v2rayConfig = initV2rayConfig(context) ?: return result + + val isPlugin = config.configType == EConfigType.HYSTERIA2 + val retOut = outbounds(v2rayConfig, config, isPlugin) ?: return result + val retMore = moreOutbounds(v2rayConfig, config.subscriptionId, isPlugin) + + v2rayConfig.log.loglevel = MmkvManager.decodeSettingsString(AppConfig.PREF_LOGLEVEL) ?: "warning" + v2rayConfig.inbounds.clear() + v2rayConfig.routing.rules.clear() + v2rayConfig.dns = null + v2rayConfig.fakedns = null + v2rayConfig.stats = null + v2rayConfig.policy = null + + v2rayConfig.outbounds.forEach { key -> + key.mux = null + } + + result.status = true + result.content = v2rayConfig.toPrettyPrinting() + result.domainPort = if (retMore.first) retMore.second else retOut.second + result.guid = guid + return result + } + + /** + * Initializes V2ray configuration. + * + * This function loads the V2ray configuration from assets or from a cached value. + * It first attempts to use the cached configuration if available, otherwise reads + * the configuration from the "v2ray_config.json" asset file. + * + * @param context Android context used to access application assets + * @return V2rayConfig object parsed from the JSON configuration, or null if the configuration is empty + */ + + private fun initV2rayConfig(context: Context): V2rayConfig? { + val assets = initConfigCache ?: Utils.readTextFromAssets(context, "v2ray_config.json") + if (TextUtils.isEmpty(assets)) { + return null + } + initConfigCache = assets + val config = JsonUtil.fromJson(assets, V2rayConfig::class.java) + return config + } + private fun inbounds(v2rayConfig: V2rayConfig): Boolean { try { val socksPort = SettingsManager.getSocksPort() @@ -274,7 +366,7 @@ object V2rayConfigManager { val proxyDomain = userRule2Domain(TAG_PROXY) val directDomain = userRule2Domain(TAG_DIRECT) // fakedns with all domains to make it always top priority - v2rayConfig.dns.servers?.add( + v2rayConfig.dns?.servers?.add( 0, V2rayConfig.DnsBean.ServersBean( address = "fakedns", diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt index 193489ba..180dc24b 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt @@ -81,11 +81,11 @@ class V2RayTestService : Service() { val delay = PluginUtil.realPingHy2(this, config) return delay } else { - val config = V2rayConfigManager.getV2rayConfig(this, guid) - if (!config.status) { + val configResult = V2rayConfigManager.getV2rayConfig4Speedtest(this, guid) + if (!configResult.status) { return retFailure } - return SpeedtestManager.realPing(config.content) + return SpeedtestManager.realPing(configResult.content) } } }