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

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 java.io.FileInputStream
import java.io.FileOutputStream
import java.util.concurrent.atomic.AtomicBoolean
class PunchnetService : VpnService() , IfaceTun {
private val TAG = "PunchnetService"
@ -46,6 +47,9 @@ class PunchnetService : VpnService() , IfaceTun {
private var vpnDescriptor: ParcelFileDescriptor? = null
private val serviceLock = Any()
private val isRunning = AtomicBoolean(false)
var input: FileInputStream? = null
var output: FileOutputStream? = null
@ -125,56 +129,69 @@ class PunchnetService : VpnService() , IfaceTun {
}
private fun disconnect() {
Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show()
stopForeground(STOP_FOREGROUND_REMOVE)
input?.close()
output?.close()
vpnDescriptor?.close()
synchronized(serviceLock) {
if (!isRunning.get()) {
return
}
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?) {
val iface = this
val server = "punchnet.aioe.tech"
synchronized(serviceLock) {
if (isRunning.get()) {
return
}
Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}")
Log.d("DIR", "external storage = ${Environment.getExternalStorageDirectory().name}")
Log.d("DIR", "filesdir = ${this.filesDir.path}")
val iface = this
val server = "punchnet.aioe.tech"
arpTable.routeTable.clearRoute()
routes.clear()
for (route in startArg?.routes?.toList()?:listOf()) {
val r = RouteDetail(
mask = route.mask_ip,
gw = route.gateway,
maskedAddr = route.net_ip,
Log.d("DIR", "datadir = ${Environment.getDataDirectory().name}")
Log.d("DIR", "external storage = ${Environment.getExternalStorageDirectory().name}")
Log.d("DIR", "filesdir = ${this.filesDir.path}")
arpTable.routeTable.clearRoute()
routes.clear()
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)
arpTable.routeTable.addRoute(r)
scope.launch {
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 {