Add onError callback to handle errors in protocol threads
This commit is contained in:
parent
5c3e253067
commit
5835a756ce
5 changed files with 28 additions and 27 deletions
|
@ -11,8 +11,8 @@ import net.openvpn.ovpn3.ClientAPI_Config
|
|||
import org.amnezia.vpn.protocol.BadConfigException
|
||||
import org.amnezia.vpn.protocol.Protocol
|
||||
import org.amnezia.vpn.protocol.ProtocolState
|
||||
import org.amnezia.vpn.protocol.ProtocolState.DISCONNECTED
|
||||
import org.amnezia.vpn.protocol.Statistics
|
||||
import org.amnezia.vpn.protocol.VpnException
|
||||
import org.amnezia.vpn.protocol.VpnStartException
|
||||
import org.amnezia.vpn.util.net.InetNetwork
|
||||
import org.amnezia.vpn.util.net.getLocalNetworks
|
||||
|
@ -53,8 +53,8 @@ open class OpenVpn : Protocol() {
|
|||
return Statistics.EMPTY_STATISTICS
|
||||
}
|
||||
|
||||
override fun initialize(context: Context, state: MutableStateFlow<ProtocolState>) {
|
||||
super.initialize(context, state)
|
||||
override fun initialize(context: Context, state: MutableStateFlow<ProtocolState>, onError: (String) -> Unit) {
|
||||
super.initialize(context, state, onError)
|
||||
loadSharedLibrary(context, "ovpn3")
|
||||
this.context = context
|
||||
scope = CoroutineScope(Dispatchers.IO)
|
||||
|
@ -64,11 +64,12 @@ open class OpenVpn : Protocol() {
|
|||
val configBuilder = OpenVpnConfig.Builder()
|
||||
|
||||
openVpnClient = OpenVpnClient(
|
||||
configBuilder,
|
||||
state,
|
||||
{ ipv6 -> getLocalNetworks(context, ipv6) },
|
||||
makeEstablish(configBuilder, vpnBuilder),
|
||||
protect
|
||||
configBuilder = configBuilder,
|
||||
state = state,
|
||||
getLocalNetworks = { ipv6 -> getLocalNetworks(context, ipv6) },
|
||||
establish = makeEstablish(configBuilder, vpnBuilder),
|
||||
protect = protect,
|
||||
onError = onError
|
||||
)
|
||||
|
||||
try {
|
||||
|
@ -92,7 +93,8 @@ open class OpenVpn : Protocol() {
|
|||
scope.launch {
|
||||
val status = client.connect()
|
||||
if (status.error) {
|
||||
throw VpnException("OpenVpn connect() error: ${status.status}: ${status.message}")
|
||||
state.value = DISCONNECTED
|
||||
onError("OpenVpn connect() error: ${status.status}: ${status.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import net.openvpn.ovpn3.ClientAPI_TransportStats
|
|||
import org.amnezia.vpn.protocol.ProtocolState
|
||||
import org.amnezia.vpn.protocol.ProtocolState.CONNECTED
|
||||
import org.amnezia.vpn.protocol.ProtocolState.DISCONNECTED
|
||||
import org.amnezia.vpn.protocol.VpnStartException
|
||||
import org.amnezia.vpn.util.Log
|
||||
import org.amnezia.vpn.util.net.InetNetwork
|
||||
import org.amnezia.vpn.util.net.parseInetAddress
|
||||
|
@ -27,7 +26,8 @@ class OpenVpnClient(
|
|||
private val state: MutableStateFlow<ProtocolState>,
|
||||
private val getLocalNetworks: (Boolean) -> List<InetNetwork>,
|
||||
private val establish: () -> Int,
|
||||
private val protect: (Int) -> Boolean
|
||||
private val protect: (Int) -> Boolean,
|
||||
private val onError: (String) -> Unit
|
||||
) : ClientAPI_OpenVPNClient() {
|
||||
|
||||
/**************************************************************************
|
||||
|
@ -368,16 +368,12 @@ class OpenVpnClient(
|
|||
"COMPRESSION_ENABLED", "WARN" -> Log.w(TAG, "$name: $info")
|
||||
"CONNECTED" -> state.value = CONNECTED
|
||||
"DISCONNECTED" -> state.value = DISCONNECTED
|
||||
"CONNECTION_TIMEOUT" -> {
|
||||
Log.w(TAG, "$name: $info")
|
||||
}
|
||||
if (event.error || event.fatal) {
|
||||
state.value = DISCONNECTED
|
||||
// todo: test it
|
||||
throw VpnStartException("Connection timeout")
|
||||
onError("OpenVpn ${if (event.error) "ERROR" else "FATAL"}: $name: $info")
|
||||
}
|
||||
}
|
||||
if (event.error) Log.e(TAG, "OpenVpn ERROR: $name: $info")
|
||||
if (event.fatal) Log.e(TAG, "OpenVpn FATAL: $name: $info")
|
||||
}
|
||||
|
||||
// Callback for logging.
|
||||
// Will be called from the thread executing connect().
|
||||
|
|
|
@ -30,9 +30,11 @@ abstract class Protocol {
|
|||
|
||||
abstract val statistics: Statistics
|
||||
protected lateinit var state: MutableStateFlow<ProtocolState>
|
||||
protected lateinit var onError: (String) -> Unit
|
||||
|
||||
open fun initialize(context: Context, state: MutableStateFlow<ProtocolState>) {
|
||||
open fun initialize(context: Context, state: MutableStateFlow<ProtocolState>, onError: (String) -> Unit) {
|
||||
this.state = state
|
||||
this.onError = onError
|
||||
}
|
||||
|
||||
abstract fun startVpn(config: JSONObject, vpnBuilder: Builder, protect: (Int) -> Boolean)
|
||||
|
|
|
@ -345,21 +345,22 @@ class AmneziaVpnService : VpnService() {
|
|||
"openvpn" -> OpenVpn()
|
||||
"cloak" -> Cloak()
|
||||
else -> throw IllegalArgumentException("Protocol '$protocolName' not found")
|
||||
}.apply { initialize(applicationContext, protocolState) }
|
||||
}.apply { initialize(applicationContext, protocolState, ::onError) }
|
||||
.also { protocolCache[protocolName] = it }
|
||||
|
||||
/**
|
||||
* Utils methods
|
||||
*/
|
||||
@MainThread
|
||||
private fun onError(msg: String) {
|
||||
Log.e(TAG, msg)
|
||||
mainScope.launch {
|
||||
clientMessenger.send {
|
||||
ServiceEvent.ERROR.packToMessage {
|
||||
putString(ERROR_MSG, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseConfigToJson(vpnConfig: String?): JSONObject? =
|
||||
try {
|
||||
|
|
|
@ -78,8 +78,8 @@ open class Wireguard : Protocol() {
|
|||
}
|
||||
}
|
||||
|
||||
override fun initialize(context: Context, state: MutableStateFlow<ProtocolState>) {
|
||||
super.initialize(context, state)
|
||||
override fun initialize(context: Context, state: MutableStateFlow<ProtocolState>, onError: (String) -> Unit) {
|
||||
super.initialize(context, state, onError)
|
||||
loadSharedLibrary(context, "wg-go")
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue