diff --git a/android/app/src/main/kotlin/com/network/proxy/MainActivity.kt b/android/app/src/main/kotlin/com/network/proxy/MainActivity.kt index ab28936..0c81009 100644 --- a/android/app/src/main/kotlin/com/network/proxy/MainActivity.kt +++ b/android/app/src/main/kotlin/com/network/proxy/MainActivity.kt @@ -2,7 +2,6 @@ package com.network.proxy import android.content.Intent import android.content.res.Configuration -import android.net.VpnService import android.os.Bundle import com.network.proxy.plugin.AppLifecyclePlugin import com.network.proxy.plugin.PictureInPicturePlugin @@ -16,7 +15,6 @@ class MainActivity : FlutterActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - prepareVpn() } override fun configureFlutterEngine(flutterEngine: FlutterEngine) { @@ -46,18 +44,20 @@ class MainActivity : FlutterActivity() { flutterEngine.plugins.add(lifecycleChannel) } - /** - * 准备vpn
- * 设备可能弹出连接vpn提示 - */ - private fun prepareVpn() { - val intent = VpnService.prepare(this@MainActivity) - if (intent != null) { - startActivityForResult(intent, VpnServicePlugin.REQUEST_CODE) - } - } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == VpnServicePlugin.REQUEST_CODE) { + if (resultCode == RESULT_OK) { + activity.startService(ProxyVpnService.startVpnIntent(activity)) + return + } + + val alertDialog = Intent(applicationContext, VpnAlertDialog::class.java) + .setAction("com.network.proxy.ProxyVpnService") + alertDialog.flags = Intent.FLAG_ACTIVITY_NEW_TASK + startActivity(alertDialog) + return + } + super.onActivityResult(requestCode, resultCode, data) } diff --git a/android/app/src/main/kotlin/com/network/proxy/ProxyVpnService.kt b/android/app/src/main/kotlin/com/network/proxy/ProxyVpnService.kt index 8b5f990..67d814d 100644 --- a/android/app/src/main/kotlin/com/network/proxy/ProxyVpnService.kt +++ b/android/app/src/main/kotlin/com/network/proxy/ProxyVpnService.kt @@ -10,6 +10,7 @@ import android.net.ProxyInfo import android.net.VpnService import android.os.Build import android.os.ParcelFileDescriptor +import android.util.Log import androidx.core.app.NotificationCompat import com.network.proxy.vpn.socket.ProtectSocket import com.network.proxy.vpn.socket.ProtectSocketHolder @@ -21,10 +22,6 @@ import com.network.proxy.vpn.socket.ProtectSocketHolder class ProxyVpnService : VpnService(), ProtectSocket { private var vpnInterface: ParcelFileDescriptor? = null - private var host: String? = null - private var port: Int = 0 - private var allowApps: List? = null - companion object { const val MAX_PACKET_LEN = 1500 @@ -45,6 +42,10 @@ class ProxyVpnService : VpnService(), ProtectSocket { var isRunning = false + var host: String? = null + var port: Int = 0 + var allowApps: ArrayList? = null + fun stopVpnIntent(context: Context): Intent { return Intent(context, ProxyVpnService::class.java).also { it.action = ACTION_DISCONNECT @@ -53,9 +54,9 @@ class ProxyVpnService : VpnService(), ProtectSocket { fun startVpnIntent( context: Context, - proxyHost: String? = null, - proxyPort: Int? = null, - allowApps: ArrayList? = null + proxyHost: String? = host, + proxyPort: Int? = port, + allowApps: ArrayList? = this.allowApps ): Intent { return Intent(context, ProxyVpnService::class.java).also { it.putExtra(ProxyHost, proxyHost) @@ -76,9 +77,9 @@ class ProxyVpnService : VpnService(), ProtectSocket { START_NOT_STICKY } else { connect( - intent.getStringExtra(ProxyHost) ?: this.host!!, - intent.getIntExtra(ProxyPort, this.port), - intent.getStringArrayListExtra(AllowApps) ?: this.allowApps + intent.getStringExtra(ProxyHost) ?: host!!, + intent.getIntExtra(ProxyPort, port), + intent.getStringArrayListExtra(AllowApps) ?: allowApps ) START_STICKY } @@ -93,10 +94,12 @@ class ProxyVpnService : VpnService(), ProtectSocket { isRunning = false } - private fun connect(proxyHost: String, proxyPort: Int, allowPackages: List?) { - this.host = proxyHost - this.port = proxyPort - this.allowApps = allowPackages + private fun connect(proxyHost: String, proxyPort: Int, allowPackages: ArrayList?) { + Log.i("ProxyVpnService", "startVpn $host:$port $allowApps") + + host = proxyHost + port = proxyPort + allowApps = allowPackages vpnInterface = createVpnInterface(proxyHost, proxyPort, allowPackages) if (vpnInterface == null) { val alertDialog = Intent(applicationContext, VpnAlertDialog::class.java) diff --git a/android/app/src/main/kotlin/com/network/proxy/plugin/VpnServicePlugin.kt b/android/app/src/main/kotlin/com/network/proxy/plugin/VpnServicePlugin.kt index c7a6e86..253c732 100644 --- a/android/app/src/main/kotlin/com/network/proxy/plugin/VpnServicePlugin.kt +++ b/android/app/src/main/kotlin/com/network/proxy/plugin/VpnServicePlugin.kt @@ -1,6 +1,6 @@ package com.network.proxy.plugin -import android.util.Log +import android.net.VpnService import com.network.proxy.ProxyVpnService import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.MethodChannel @@ -18,11 +18,16 @@ class VpnServicePlugin : AndroidFlutterPlugin() { "isRunning" -> { result.success(ProxyVpnService.isRunning) } + "startVpn" -> { val host = call.argument("proxyHost") val port = call.argument("proxyPort") val allowApps = call.argument>("allowApps") - startVpn(host!!, port!!, allowApps) + val prepareVpn = prepareVpn(host!!, port!!, allowApps) + if (prepareVpn) { + startVpn(host, port, allowApps) + } + result.success(prepareVpn) } "stopVpn" -> { @@ -45,11 +50,26 @@ class VpnServicePlugin : AndroidFlutterPlugin() { } } + /** + * 准备vpn
+ * 设备可能弹出连接vpn提示 + */ + private fun prepareVpn(host: String, port: Int, allowApps: ArrayList?): Boolean { + val intent = VpnService.prepare(activity) + if (intent != null) { + ProxyVpnService.host = host + ProxyVpnService.port = port + ProxyVpnService.allowApps = allowApps + activity.startActivityForResult(intent, REQUEST_CODE) + return false + } + return true + } + /** * 启动vpn服务 */ private fun startVpn(host: String, port: Int, allowApps: ArrayList?) { - Log.i("com.network.proxy", "startVpn $host:$port $allowApps") val intent = ProxyVpnService.startVpnIntent(activity, host, port, allowApps) activity.startService(intent) } diff --git a/lib/ui/mobile/setting/app_whitelist.dart b/lib/ui/mobile/setting/app_whitelist.dart index 2364490..b97ace1 100644 --- a/lib/ui/mobile/setting/app_whitelist.dart +++ b/lib/ui/mobile/setting/app_whitelist.dart @@ -31,8 +31,7 @@ class _AppWhitelistState extends State { @override void dispose() { if (changed && widget.proxyServer.isRunning) { - Vpn.stopVpn(); - Vpn.startVpn("127.0.0.1", widget.proxyServer.port, configuration.appWhitelist); + Vpn.restartVpn("127.0.0.1", widget.proxyServer.port, configuration.appWhitelist); configuration.flushConfig(); } super.dispose();