Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Adding latency test for hy2
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 30, 2024
1 parent 35b3f0d commit 1e88dd9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions app/src/main/kotlin/com/neko/v2ray/dto/Hysteria2Bean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
39 changes: 33 additions & 6 deletions app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package com.neko.v2ray.service
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_CANCEL
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_SUCCESS
import com.neko.v2ray.dto.ConfigResult
import com.neko.v2ray.dto.EConfigType
import com.neko.v2ray.extension.serializable
import com.neko.v2ray.util.JsonUtil
import com.neko.v2ray.util.MessageUtil
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.PluginUtil
import com.neko.v2ray.util.SpeedtestUtil
import com.neko.v2ray.util.Utils
import com.neko.v2ray.util.V2rayConfigUtil
import go.Seq
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand All @@ -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<String>("content") ?: ""
val config = JsonUtil.fromJson(content, ConfigResult::class.java)
val guid = intent.serializable<String>("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))
}
}

Expand All @@ -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)
}
}
}
13 changes: 5 additions & 8 deletions app/src/main/kotlin/com/neko/v2ray/util/SpeedtestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ object SpeedtestUtil {
}
}

fun testConnection(context: Context, port: Int): String {
// return V2RayVpnService.measureV2rayDelay()
fun testConnection(context: Context, port: Int): Pair<Long, String> {
var result: String
var elapsed = -1L
var conn: HttpURLConnection? = null

try {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -147,7 +144,7 @@ object SpeedtestUtil {
conn?.disconnect()
}

return result
return Pair(elapsed, result)
}

fun getLibVersion(): String {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/util/fmt/Hysteria2Fmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.neko.v2ray.util.MessageUtil
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.SpeedtestUtil
import com.neko.v2ray.util.Utils
import com.neko.v2ray.util.V2rayConfigUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -213,14 +212,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
getApplication<AngApplication>().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)
}
}
}
Expand Down

0 comments on commit 1e88dd9

Please sign in to comment.