Fix disconnect bug
This commit is contained in:
parent
e7658f9859
commit
1e64413904
6 changed files with 40 additions and 43 deletions
|
@ -3,7 +3,10 @@ package org.amnezia.vpn.protocol.openvpn
|
|||
import android.content.Context
|
||||
import android.net.VpnService.Builder
|
||||
import android.os.Build
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import net.openvpn.ovpn3.ClientAPI_Config
|
||||
import org.amnezia.vpn.protocol.BadConfigException
|
||||
import org.amnezia.vpn.protocol.Protocol
|
||||
|
@ -35,7 +38,8 @@ import org.json.JSONObject
|
|||
open class OpenVpn : Protocol() {
|
||||
|
||||
private lateinit var context: Context
|
||||
protected var openVpnClient: OpenVpnClient? = null
|
||||
private var openVpnClient: OpenVpnClient? = null
|
||||
private lateinit var scope: CoroutineScope
|
||||
|
||||
override val statistics: Statistics
|
||||
get() {
|
||||
|
@ -53,6 +57,7 @@ open class OpenVpn : Protocol() {
|
|||
super.initialize(context, state)
|
||||
loadSharedLibrary(context, "ovpn3")
|
||||
this.context = context
|
||||
scope = CoroutineScope(Dispatchers.IO)
|
||||
}
|
||||
|
||||
override fun startVpn(config: JSONObject, vpnBuilder: Builder, protect: (Int) -> Boolean) {
|
||||
|
@ -84,9 +89,11 @@ open class OpenVpn : Protocol() {
|
|||
configSplitTunnel(config)
|
||||
}
|
||||
|
||||
val status = client.connect()
|
||||
if (status.error) {
|
||||
throw VpnException("OpenVpn connect() error: ${status.status}: ${status.message}")
|
||||
scope.launch {
|
||||
val status = client.connect()
|
||||
if (status.error) {
|
||||
throw VpnException("OpenVpn connect() error: ${status.status}: ${status.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
|
|
|
@ -22,10 +22,11 @@ import kotlinx.coroutines.Dispatchers
|
|||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import org.amnezia.vpn.protocol.BadConfigException
|
||||
import org.amnezia.vpn.protocol.LoadLibraryException
|
||||
import org.amnezia.vpn.protocol.Protocol
|
||||
|
@ -56,6 +57,7 @@ const val AFTER_PERMISSION_CHECK = "AFTER_PERMISSION_CHECK"
|
|||
private const val PREFS_CONFIG_KEY = "LAST_CONF"
|
||||
private const val NOTIFICATION_ID = 1337
|
||||
private const val STATISTICS_SENDING_TIMEOUT = 1000L
|
||||
private const val DISCONNECT_TIMEOUT = 1500L
|
||||
|
||||
class AmneziaVpnService : VpnService() {
|
||||
|
||||
|
@ -227,8 +229,12 @@ class AmneziaVpnService : VpnService() {
|
|||
|
||||
override fun onDestroy() {
|
||||
Log.v(TAG, "Destroy service")
|
||||
// todo: add sync disconnect
|
||||
disconnect()
|
||||
runBlocking {
|
||||
withTimeoutOrNull(DISCONNECT_TIMEOUT) {
|
||||
disconnect()
|
||||
disconnectionJob?.join()
|
||||
}
|
||||
}
|
||||
connectionScope.cancel()
|
||||
mainScope.cancel()
|
||||
super.onDestroy()
|
||||
|
@ -322,9 +328,7 @@ class AmneziaVpnService : VpnService() {
|
|||
protocolState.value = DISCONNECTING
|
||||
|
||||
disconnectionJob = connectionScope.launch {
|
||||
connectionJob?.let {
|
||||
if (it.isActive) it.cancelAndJoin()
|
||||
}
|
||||
connectionJob?.join()
|
||||
connectionJob = null
|
||||
|
||||
protocol?.stopVpn()
|
||||
|
@ -342,6 +346,7 @@ class AmneziaVpnService : VpnService() {
|
|||
"cloak" -> Cloak()
|
||||
else -> throw IllegalArgumentException("Protocol '$protocolName' not found")
|
||||
}.apply { initialize(applicationContext, protocolState) }
|
||||
.also { protocolCache[protocolName] = it }
|
||||
|
||||
/**
|
||||
* Utils methods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue