Skip to content

Commit d4cc105

Browse files
2dustAnGgIt886
authored andcommitted
fix:Fixed latency test
1 parent c5b0e4c commit d4cc105

File tree

3 files changed

+97
-55
lines changed

3 files changed

+97
-55
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.neko.v2ray.service
2+
3+
import android.content.Context
4+
import android.util.Log
5+
import com.neko.v2ray.AppConfig.ANG_PACKAGE
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.launch
9+
10+
class ProcessService {
11+
private val TAG = ANG_PACKAGE
12+
private lateinit var process: Process
13+
14+
fun runProcess(context: Context, cmd: MutableList<String>) {
15+
Log.d(TAG, cmd.toString())
16+
17+
try {
18+
val proBuilder = ProcessBuilder(cmd)
19+
proBuilder.redirectErrorStream(true)
20+
process = proBuilder
21+
.directory(context.filesDir)
22+
.start()
23+
24+
CoroutineScope(Dispatchers.IO).launch {
25+
Thread.sleep(50L)
26+
Log.d(TAG, "runProcess check")
27+
process.waitFor()
28+
Log.d(TAG, "runProcess exited")
29+
}
30+
Log.d(TAG, process.toString())
31+
32+
} catch (e: Exception) {
33+
Log.d(TAG, e.toString())
34+
}
35+
}
36+
37+
fun stopProcess() {
38+
try {
39+
Log.d(TAG, "runProcess destroy")
40+
process?.destroy()
41+
} catch (e: Exception) {
42+
Log.d(TAG, e.toString())
43+
}
44+
}
45+
}

app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.neko.v2ray.service
33
import android.app.Service
44
import android.content.Intent
55
import android.os.IBinder
6-
import android.util.Log
76
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG
87
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_CANCEL
98
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_SUCCESS
@@ -59,17 +58,8 @@ class V2RayTestService : Service() {
5958

6059
val server = MmkvManager.decodeServerConfig(guid) ?: return retFailure
6160
if (server.getProxyOutbound()?.protocol?.equals(EConfigType.HYSTERIA2.name, true) == true) {
62-
val socksPort = Utils.findFreePort(listOf(0))
63-
PluginUtil.runPlugin(this, server, "0:${socksPort}")
64-
Thread.sleep(1000L)
65-
66-
var delay = SpeedtestUtil.testConnection(this, socksPort)
67-
if (delay.first < 0) {
68-
Thread.sleep(10L)
69-
delay = SpeedtestUtil.testConnection(this, socksPort)
70-
}
71-
PluginUtil.stopPlugin()
72-
return delay.first
61+
val delay = PluginUtil.realPingHy2(this, server)
62+
return delay
7363
} else {
7464
val config = V2rayConfigUtil.getV2rayConfig(this, guid)
7565
if (!config.status) {

app/src/main/kotlin/com/neko/v2ray/util/PluginUtil.kt

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,77 @@ import android.util.Log
66
import com.neko.v2ray.AppConfig.ANG_PACKAGE
77
import com.neko.v2ray.dto.EConfigType
88
import com.neko.v2ray.dto.ServerConfig
9+
import com.neko.v2ray.service.ProcessService
910
import com.neko.v2ray.util.fmt.Hysteria2Fmt
10-
import kotlinx.coroutines.CoroutineScope
11-
import kotlinx.coroutines.Dispatchers
12-
import kotlinx.coroutines.launch
1311
import java.io.File
1412

1513
object PluginUtil {
1614
//private const val HYSTERIA2 = "hysteria2-plugin"
1715
private const val HYSTERIA2 = "libhysteria2.so"
18-
private const val packageName = ANG_PACKAGE
19-
private lateinit var process: Process
16+
private const val TAG = ANG_PACKAGE
17+
private lateinit var procService: ProcessService
2018

2119
// fun initPlugin(name: String): PluginManager.InitResult {
2220
// return PluginManager.init(name)!!
2321
// }
2422

2523
fun runPlugin(context: Context, config: ServerConfig?, domainPort: String?) {
26-
Log.d(packageName, "runPlugin")
24+
Log.d(TAG, "runPlugin")
2725

2826
val outbound = config?.getProxyOutbound() ?: return
2927
if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) {
30-
Log.d(packageName, "runPlugin $HYSTERIA2")
28+
val configFile = genConfigHy2(context, config, domainPort) ?: return
29+
val cmd = genCmdHy2(context, configFile)
3130

32-
val socksPort = domainPort?.split(":")?.last()
33-
.let { if (it.isNullOrEmpty()) return else it.toInt() }
34-
val hy2Config = Hysteria2Fmt.toNativeConfig(config, socksPort) ?: return
31+
procService = ProcessService()
32+
procService.runProcess(context, cmd)
33+
}
34+
}
35+
36+
fun stopPlugin() {
37+
stopHy2()
38+
}
39+
40+
fun realPingHy2(context: Context, config: ServerConfig?): Long {
41+
Log.d(TAG, "realPingHy2")
42+
val retFailure = -1L
3543

36-
val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json")
37-
Log.d(packageName, "runPlugin ${configFile.absolutePath}")
44+
val outbound = config?.getProxyOutbound() ?: return retFailure
45+
if (outbound.protocol.equals(EConfigType.HYSTERIA2.name, true)) {
46+
val socksPort = Utils.findFreePort(listOf(0))
47+
val configFile = genConfigHy2(context, config, "0:${socksPort}") ?: return retFailure
48+
val cmd = genCmdHy2(context, configFile)
3849

39-
configFile.parentFile?.mkdirs()
40-
configFile.writeText(JsonUtil.toJson(hy2Config))
41-
Log.d(packageName, JsonUtil.toJson(hy2Config))
50+
val proc = ProcessService()
51+
proc.runProcess(context, cmd)
52+
Thread.sleep(1000L)
53+
val delay = SpeedtestUtil.testConnection(context, socksPort)
54+
proc.stopProcess()
4255

43-
runHy2(context, configFile)
56+
return delay.first
4457
}
58+
return retFailure
4559
}
4660

47-
fun stopPlugin() {
48-
stopHy2()
61+
private fun genConfigHy2(context: Context, config: ServerConfig, domainPort: String?): File? {
62+
Log.d(TAG, "runPlugin $HYSTERIA2")
63+
64+
val socksPort = domainPort?.split(":")?.last()
65+
.let { if (it.isNullOrEmpty()) return null else it.toInt() }
66+
val hy2Config = Hysteria2Fmt.toNativeConfig(config, socksPort) ?: return null
67+
68+
val configFile = File(context.noBackupFilesDir, "hy2_${SystemClock.elapsedRealtime()}.json")
69+
Log.d(TAG, "runPlugin ${configFile.absolutePath}")
70+
71+
configFile.parentFile?.mkdirs()
72+
configFile.writeText(JsonUtil.toJson(hy2Config))
73+
Log.d(TAG, JsonUtil.toJson(hy2Config))
74+
75+
return configFile
4976
}
5077

51-
private fun runHy2(context: Context, configFile: File) {
52-
val cmd = mutableListOf(
78+
private fun genCmdHy2(context: Context, configFile: File): MutableList<String> {
79+
return mutableListOf(
5380
File(context.applicationInfo.nativeLibraryDir, HYSTERIA2).absolutePath,
5481
//initPlugin(HYSTERIA2).path,
5582
"--disable-update-check",
@@ -59,34 +86,14 @@ object PluginUtil {
5986
"warn",
6087
"client"
6188
)
62-
Log.d(packageName, cmd.toString())
63-
64-
try {
65-
val proBuilder = ProcessBuilder(cmd)
66-
proBuilder.redirectErrorStream(true)
67-
process = proBuilder
68-
.directory(context.filesDir)
69-
.start()
70-
71-
CoroutineScope(Dispatchers.IO).launch {
72-
Thread.sleep(500L)
73-
Log.d(packageName, "$HYSTERIA2 check")
74-
process.waitFor()
75-
Log.d(packageName, "$HYSTERIA2 exited")
76-
}
77-
Log.d(packageName, process.toString())
78-
79-
} catch (e: Exception) {
80-
Log.d(packageName, e.toString())
81-
}
8289
}
8390

8491
private fun stopHy2() {
8592
try {
86-
Log.d(packageName, "$HYSTERIA2 destroy")
87-
process?.destroy()
93+
Log.d(TAG, "$HYSTERIA2 destroy")
94+
procService?.stopProcess()
8895
} catch (e: Exception) {
89-
Log.d(packageName, e.toString())
96+
Log.d(TAG, e.toString())
9097
}
9198
}
9299
}

0 commit comments

Comments
 (0)