mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-05-20 16:15:47 +08:00
安卓vpn开启抓包才弹出授权
This commit is contained in:
@@ -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<br>
|
||||
* 设备可能弹出连接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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String>? = 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<String>? = 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<String>? = null
|
||||
proxyHost: String? = host,
|
||||
proxyPort: Int? = port,
|
||||
allowApps: ArrayList<String>? = 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<String>?) {
|
||||
this.host = proxyHost
|
||||
this.port = proxyPort
|
||||
this.allowApps = allowPackages
|
||||
private fun connect(proxyHost: String, proxyPort: Int, allowPackages: ArrayList<String>?) {
|
||||
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)
|
||||
|
||||
@@ -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<String>("proxyHost")
|
||||
val port = call.argument<Int>("proxyPort")
|
||||
val allowApps = call.argument<ArrayList<String>>("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<br>
|
||||
* 设备可能弹出连接vpn提示
|
||||
*/
|
||||
private fun prepareVpn(host: String, port: Int, allowApps: ArrayList<String>?): 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<String>?) {
|
||||
Log.i("com.network.proxy", "startVpn $host:$port $allowApps")
|
||||
val intent = ProxyVpnService.startVpnIntent(activity, host, port, allowApps)
|
||||
activity.startService(intent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user