From cccd6139fc9c2d2ad88172a8f8dfea44ba5ddf59 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 30 Sep 2024 18:06:36 +0800 Subject: [PATCH] Adding latency test for hy2 --- .../kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt | 1 + .../com/v2ray/ang/service/V2RayTestService.kt | 39 ++++++++++++++++--- .../com/v2ray/ang/util/SpeedtestUtil.kt | 13 +++---- .../com/v2ray/ang/util/fmt/Hysteria2Fmt.kt | 3 ++ .../com/v2ray/ang/viewmodel/MainViewModel.kt | 11 +----- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt index c04f71b2..c671541d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/Hysteria2Bean.kt @@ -6,6 +6,7 @@ data class Hysteria2Bean( val lazy: Boolean? = true, val obfs: ObfsBean? = null, val socks5: Socks5Bean? = null, + val http: Socks5Bean? = null, val tls: TlsBean? = null, ) { data class ObfsBean( diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt index b84a4e02..b90efdfd 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayTestService.kt @@ -3,15 +3,18 @@ package com.v2ray.ang.service import android.app.Service import android.content.Intent import android.os.IBinder +import android.util.Log import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_CANCEL import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_SUCCESS -import com.v2ray.ang.dto.ConfigResult +import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.extension.serializable -import com.v2ray.ang.util.JsonUtil import com.v2ray.ang.util.MessageUtil +import com.v2ray.ang.util.MmkvManager +import com.v2ray.ang.util.PluginUtil import com.v2ray.ang.util.SpeedtestUtil import com.v2ray.ang.util.Utils +import com.v2ray.ang.util.V2rayConfigUtil import go.Seq import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job @@ -33,11 +36,10 @@ class V2RayTestService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.getIntExtra("key", 0)) { MSG_MEASURE_CONFIG -> { - val content = intent.serializable("content") ?: "" - val config = JsonUtil.fromJson(content, ConfigResult::class.java) + val guid = intent.serializable("content") ?: "" realTestScope.launch { - val result = SpeedtestUtil.realPing(config.content) - MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(config.guid, result)) + val result = startRealPing(guid) + MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(guid, result)) } } @@ -51,4 +53,29 @@ class V2RayTestService : Service() { override fun onBind(intent: Intent?): IBinder? { return null } + + private fun startRealPing(guid: String): Long { + val retFailure = -1L + + val server = MmkvManager.decodeServerConfig(guid) ?: return retFailure + if (server.getProxyOutbound()?.protocol?.equals(EConfigType.HYSTERIA2.name, true) == true) { + val socksPort = Utils.findFreePort(listOf(0)) + PluginUtil.runPlugin(this, server, "0:${socksPort}") + Thread.sleep(1000L) + + var delay = SpeedtestUtil.testConnection(this, socksPort) + if (delay.first < 0) { + Thread.sleep(10L) + delay = SpeedtestUtil.testConnection(this, socksPort) + } + PluginUtil.stopPlugin() + return delay.first + } else { + val config = V2rayConfigUtil.getV2rayConfig(this, guid) + if (!config.status) { + return retFailure + } + return SpeedtestUtil.realPing(config.content) + } + } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt index 29e27de0..10e793be 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/SpeedtestUtil.kt @@ -98,9 +98,9 @@ object SpeedtestUtil { } } - fun testConnection(context: Context, port: Int): String { - // return V2RayVpnService.measureV2rayDelay() + fun testConnection(context: Context, port: Int): Pair { var result: String + var elapsed = -1L var conn: HttpURLConnection? = null try { @@ -120,7 +120,7 @@ object SpeedtestUtil { val start = SystemClock.elapsedRealtime() val code = conn.responseCode - val elapsed = SystemClock.elapsedRealtime() - start + elapsed = SystemClock.elapsedRealtime() - start if (code == 204 || code == 200 && conn.responseLength == 0L) { result = context.getString(R.string.connection_test_available, elapsed) @@ -134,10 +134,7 @@ object SpeedtestUtil { } } catch (e: IOException) { // network exception - Log.d( - AppConfig.ANG_PACKAGE, - "testConnection IOException: " + Log.getStackTraceString(e) - ) + Log.d(AppConfig.ANG_PACKAGE, "testConnection IOException: " + Log.getStackTraceString(e)) result = context.getString(R.string.connection_test_error, e.message) } catch (e: Exception) { // library exception, eg sumsung @@ -147,7 +144,7 @@ object SpeedtestUtil { conn?.disconnect() } - return result + return Pair(elapsed, result) } fun getLibVersion(): String { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/Hysteria2Fmt.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/Hysteria2Fmt.kt index fd5f468e..d0d7bae7 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/Hysteria2Fmt.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/fmt/Hysteria2Fmt.kt @@ -99,6 +99,9 @@ object Hysteria2Fmt { socks5 = Hysteria2Bean.Socks5Bean( listen = "$LOOPBACK:${socksPort}", ), + http = Hysteria2Bean.Socks5Bean( + listen = "$LOOPBACK:${socksPort}", + ), tls = Hysteria2Bean.TlsBean( sni = tls?.serverName ?: outbound.getServerAddress(), insecure = tls?.allowInsecure diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt index 2a761156..f807970e 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -11,7 +11,6 @@ import android.util.Log import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope - import com.v2ray.ang.AngApplication import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.ANG_PACKAGE @@ -30,7 +29,6 @@ import com.v2ray.ang.util.MessageUtil import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.SpeedtestUtil import com.v2ray.ang.util.Utils -import com.v2ray.ang.util.V2rayConfigUtil import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -213,14 +211,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { getApplication().toast(R.string.connection_test_testing) viewModelScope.launch(Dispatchers.Default) { // without Dispatchers.Default viewModelScope will launch in main thread for (item in serversCopy) { - val config = V2rayConfigUtil.getV2rayConfig(getApplication(), item.guid) - if (config.status) { - MessageUtil.sendMsg2TestService( - getApplication(), - AppConfig.MSG_MEASURE_CONFIG, - JsonUtil.toJson(config) - ) - } + MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG, item.guid) } } }