@@ -6,50 +6,77 @@ import android.util.Log
6
6
import com.neko.v2ray.AppConfig.ANG_PACKAGE
7
7
import com.neko.v2ray.dto.EConfigType
8
8
import com.neko.v2ray.dto.ServerConfig
9
+ import com.neko.v2ray.service.ProcessService
9
10
import com.neko.v2ray.util.fmt.Hysteria2Fmt
10
- import kotlinx.coroutines.CoroutineScope
11
- import kotlinx.coroutines.Dispatchers
12
- import kotlinx.coroutines.launch
13
11
import java.io.File
14
12
15
13
object PluginUtil {
16
14
// private const val HYSTERIA2 = "hysteria2-plugin"
17
15
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
20
18
21
19
// fun initPlugin(name: String): PluginManager.InitResult {
22
20
// return PluginManager.init(name)!!
23
21
// }
24
22
25
23
fun runPlugin (context : Context , config : ServerConfig ? , domainPort : String? ) {
26
- Log .d(packageName , " runPlugin" )
24
+ Log .d(TAG , " runPlugin" )
27
25
28
26
val outbound = config?.getProxyOutbound() ? : return
29
27
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)
31
30
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
35
43
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)
38
49
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()
42
55
43
- runHy2(context, configFile)
56
+ return delay.first
44
57
}
58
+ return retFailure
45
59
}
46
60
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
49
76
}
50
77
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 (
53
80
File (context.applicationInfo.nativeLibraryDir, HYSTERIA2 ).absolutePath,
54
81
// initPlugin(HYSTERIA2).path,
55
82
" --disable-update-check" ,
@@ -59,34 +86,14 @@ object PluginUtil {
59
86
" warn" ,
60
87
" client"
61
88
)
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
- }
82
89
}
83
90
84
91
private fun stopHy2 () {
85
92
try {
86
- Log .d(packageName , " $HYSTERIA2 destroy" )
87
- process?.destroy ()
93
+ Log .d(TAG , " $HYSTERIA2 destroy" )
94
+ procService?.stopProcess ()
88
95
} catch (e: Exception ) {
89
- Log .d(packageName , e.toString())
96
+ Log .d(TAG , e.toString())
90
97
}
91
98
}
92
99
}
0 commit comments