修复了开启,关闭,再开启,连不上的问题

This commit is contained in:
asxalex 2025-07-15 10:28:43 +08:00
parent c7aed59f25
commit fa2453a733
4 changed files with 57 additions and 40 deletions

Binary file not shown.

View File

@ -37,6 +37,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.concurrent.atomic.AtomicBoolean
class PunchnetService : VpnService() , IfaceTun { class PunchnetService : VpnService() , IfaceTun {
private val TAG = "PunchnetService" private val TAG = "PunchnetService"
@ -46,6 +47,9 @@ class PunchnetService : VpnService() , IfaceTun {
private var vpnDescriptor: ParcelFileDescriptor? = null private var vpnDescriptor: ParcelFileDescriptor? = null
private val serviceLock = Any()
private val isRunning = AtomicBoolean(false)
var input: FileInputStream? = null var input: FileInputStream? = null
var output: FileOutputStream? = null var output: FileOutputStream? = null
@ -125,6 +129,11 @@ class PunchnetService : VpnService() , IfaceTun {
} }
private fun disconnect() { private fun disconnect() {
synchronized(serviceLock) {
if (!isRunning.get()) {
return
}
Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show() Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show()
stopForeground(STOP_FOREGROUND_REMOVE) stopForeground(STOP_FOREGROUND_REMOVE)
input?.close() input?.close()
@ -132,13 +141,18 @@ class PunchnetService : VpnService() , IfaceTun {
vpnDescriptor?.close() vpnDescriptor?.close()
vpnDescriptor = null vpnDescriptor = null
stopSelf() stopSelf()
} }
}
private fun connect(startArg: PunchnetServiceArgument?) { private fun connect(startArg: PunchnetServiceArgument?) {
val iface = this
synchronized(serviceLock) {
if (isRunning.get()) {
return
}
val iface = this
val server = "punchnet.aioe.tech" val server = "punchnet.aioe.tech"
Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}") Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}")
@ -175,6 +189,9 @@ class PunchnetService : VpnService() , IfaceTun {
val notification = createNotification() val notification = createNotification()
startForeground(1, notification) startForeground(1, notification)
isRunning.set(true)
}
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {