1- package click.vpnclient.engine.flutter.vpnclient_engine_flutter
1+ package click.vpnclient.engine.flutter
22
33import android.content.Context
4+ import android.util.Log
5+ import go.Seq
46import io.flutter.embedding.engine.plugins.FlutterPlugin
57import io.flutter.plugin.common.MethodCall
68import io.flutter.plugin.common.MethodChannel
79import io.flutter.plugin.common.MethodChannel.MethodCallHandler
810import io.flutter.plugin.common.MethodChannel.Result
9- import go.Seq
10- import android.util.Log
11-
12-
13-
14-
15-
16-
1711
1812/* *
1913 * VpnclientEngineFlutterPlugin
2014 * This class handles the communication between Flutter and native Android code
2115 * for managing VPN connections.
2216 */
23- class VpnclientEngineFlutterPlugin : FlutterPlugin , MethodCallHandler {
24- // / The MethodChannel that will handle the communication between Flutter and native Android
17+ class VpnclientEngineFlutterPlugin :
18+ FlutterPlugin ,
19+ MethodCallHandler {
20+ // / The MethodChannel that will handle the communication between Flutter and native Android
2521 private lateinit var channel: MethodChannel
2622 private lateinit var context: Context
27- private val TAG = " VpnclientEngineFlutterPlugin"
2823
2924 private var singboxCore: SingBoxCore ? = null
3025
@@ -34,7 +29,10 @@ class VpnclientEngineFlutterPlugin : FlutterPlugin, MethodCallHandler {
3429 context = flutterPluginBinding.applicationContext
3530 }
3631
37- override fun onMethodCall (call : MethodCall , result : Result ) {
32+ override fun onMethodCall (
33+ call : MethodCall ,
34+ result : Result ,
35+ ) {
3836 when (call.method) {
3937 " startVPN" -> startVPN(call, result)
4038 " stopVPN" -> stopVPN(call, result)
@@ -52,7 +50,7 @@ class VpnclientEngineFlutterPlugin : FlutterPlugin, MethodCallHandler {
5250 try {
5351 singboxCore = SingBoxCore (config)
5452 } catch (e: Exception ) {
55- Log .e(TAG , " Failed to initialize sing-box" , e)
53+ Log .e(packageName , " Failed to initialize sing-box" , e)
5654 }
5755 }
5856
@@ -61,14 +59,17 @@ class VpnclientEngineFlutterPlugin : FlutterPlugin, MethodCallHandler {
6159 * @param call MethodCall containing the configuration.
6260 * @param result Result to send the success or error back to Flutter.
6361 */
64- private fun startVPN (call : MethodCall , result : Result ) {
62+ private fun startVPN (
63+ call : MethodCall ,
64+ result : Result ,
65+ ) {
6566 val config = call.argument<String >(" config" ) ? : return result.error(" NO_CONFIG" , " Missing config" , null )
6667 try {
6768 initSingbox(config)
6869 singboxCore?.start()
6970 result.success(true )
7071 } catch (e: Exception ) {
71- Log .e(TAG , " Failed to start sing-box" , e)
72+ Log .e(packageName , " Failed to start sing-box" , e)
7273 result.error(" START_ERROR" , " Failed to start sing-box" , e.message)
7374 }
7475 }
@@ -77,30 +78,37 @@ class VpnclientEngineFlutterPlugin : FlutterPlugin, MethodCallHandler {
7778 * Stop the VPN connection.
7879 * @param result Result to send the success back to Flutter.
7980 */
80- private fun stopVPN (call : MethodCall , result : Result ) {
81+ private fun stopVPN (
82+ call : MethodCall ,
83+ result : Result ,
84+ ) {
8185 try {
8286 singboxCore?.stop()
8387 singboxCore = null // Release sing-box instance after stopping
8488 result.success(true )
8589 } catch (e: Exception ) {
86- Log .e(TAG , " Failed to stop sing-box" , e)
90+ Log .e(packageName , " Failed to stop sing-box" , e)
8791 result.error(" STOP_ERROR" , " Failed to stop sing-box" , e.message)
8892 }
8993 }
9094
91- private fun getStatus (call : MethodCall , result : Result ) {
95+ private fun getStatus (
96+ call : MethodCall ,
97+ result : Result ,
98+ ) {
9299 try {
93100 val status = singboxCore?.getStatus() ? : " stopped"
94101 result.success(status)
95102 } catch (e: Exception ) {
96- Log .e(TAG , " Failed to get sing-box status" , e)
103+ Log .e(packageName , " Failed to get sing-box status" , e)
97104 result.error(
98105 " STATUS_ERROR" ,
99106 " Failed to get sing-box status" ,
100- e.message
107+ e.message,
101108 )
102109 }
103110 }
111+
104112 private fun getPlatformVersion (result : Result ) {
105113 result.success(" Android ${android.os.Build .VERSION .RELEASE } " )
106114 }
@@ -110,19 +118,17 @@ class VpnclientEngineFlutterPlugin : FlutterPlugin, MethodCallHandler {
110118 }
111119}
112120
113-
114-
115- class SingBoxCore (config : String ) {
116- private val TAG = " SingBoxCore"
117-
121+ class SingBoxCore (
122+ config : String ,
123+ ) {
118124 init {
119125 try {
120126 // Initialize the Go runtime
121127 Seq .setContext(null )
122128 // Load the sing-box configuration from the provided string
123129 Singbox .setConfig(config)
124130 } catch (e: Exception ) {
125- Log .e(TAG , " Error initializing SingBox: ${e.message} " )
131+ Log .e(packageName , " Error initializing SingBox: ${e.message} " )
126132 throw e
127133 }
128134 }
@@ -139,7 +145,7 @@ class SingBoxCore(config: String) {
139145 throw Exception (" Error starting SingBox: $err " )
140146 }
141147 } catch (e: Exception ) {
142- Log .e(TAG , " Error starting SingBox: ${e.message} " )
148+ Log .e(packageName , " Error starting SingBox: ${e.message} " )
143149 throw e
144150 }
145151 }
@@ -153,12 +159,10 @@ class SingBoxCore(config: String) {
153159 // Stop the SingBox core
154160 Singbox .stop()
155161 } catch (e: Exception ) {
156- Log .e(TAG , " Error stopping SingBox: ${e.message} " )
162+ Log .e(packageName , " Error stopping SingBox: ${e.message} " )
157163 throw e
158164 }
159165 }
160166
161- fun getStatus (): String {
162- return Singbox .getStatus()
163- }
167+ fun getStatus (): String = Singbox .getStatus()
164168}
0 commit comments