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

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,56 +129,69 @@ class PunchnetService : VpnService() , IfaceTun {
} }
private fun disconnect() { private fun disconnect() {
Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show() synchronized(serviceLock) {
stopForeground(STOP_FOREGROUND_REMOVE) if (!isRunning.get()) {
input?.close() return
output?.close() }
vpnDescriptor?.close()
vpnDescriptor = null Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show()
stopForeground(STOP_FOREGROUND_REMOVE)
input?.close()
output?.close()
vpnDescriptor?.close()
stopSelf() vpnDescriptor = null
stopSelf()
}
} }
private fun connect(startArg: PunchnetServiceArgument?) { private fun connect(startArg: PunchnetServiceArgument?) {
val iface = this
val server = "punchnet.aioe.tech" synchronized(serviceLock) {
if (isRunning.get()) {
return
}
Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}") val iface = this
Log.d("DIR", "external storage = ${Environment.getExternalStorageDirectory().name}") val server = "punchnet.aioe.tech"
Log.d("DIR", "filesdir = ${this.filesDir.path}")
arpTable.routeTable.clearRoute() Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}")
routes.clear() Log.d("DIR", "external storage = ${Environment.getExternalStorageDirectory().name}")
for (route in startArg?.routes?.toList()?:listOf()) { Log.d("DIR", "filesdir = ${this.filesDir.path}")
val r = RouteDetail(
mask = route.mask_ip, arpTable.routeTable.clearRoute()
gw = route.gateway, routes.clear()
maskedAddr = route.net_ip, for (route in startArg?.routes?.toList()?:listOf()) {
val r = RouteDetail(
mask = route.mask_ip,
gw = route.gateway,
maskedAddr = route.net_ip,
)
routes.add(route)
// val r = cidrToRouteDetail(route.targetNetCIDR, route.gateway)
arpTable.routeTable.addRoute(r)
}
val argument = Arguments(
baseDir = this.filesDir.path,
sn = "$server:1265",
tcp = "$server:18083",
nat_server1 = "$server:1265",
nat_server2 = "47.98.178.3:1265",
token = startArg?.token?:"",
name = "tau",
) )
routes.add(route)
// val r = cidrToRouteDetail(route.targetNetCIDR, route.gateway) scope.launch {
arpTable.routeTable.addRoute(r) run_sdlan(scope, iface, argument, startArg)
}
val notification = createNotification()
startForeground(1, notification)
isRunning.set(true)
} }
val argument = Arguments(
baseDir = this.filesDir.path,
sn = "$server:1265",
tcp = "$server:18083",
nat_server1 = "$server:1265",
nat_server2 = "47.98.178.3:1265",
token = startArg?.token?:"",
name = "tau",
)
scope.launch {
run_sdlan(scope, iface, argument, startArg)
}
val notification = createNotification()
startForeground(1, notification)
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {