Skip to content

Commit

Permalink
Start daemon and management service on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Aug 28, 2024
1 parent e1af89b commit 846af74
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.mullvad.mullvadvpn.lib.common.util

import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

fun <T> runBlockingWithTimeout(
context: CoroutineContext,
timeout: Long,
block: suspend CoroutineScope.() -> T,
): T =
runBlocking(context = context) {
val d = async { block() }
withTimeout(timeout) { d.await() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.mullvad.mullvadvpn.service

import co.touchlab.kermit.Logger
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import net.mullvad.mullvadvpn.lib.common.util.runBlockingWithTimeout
import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpoint
import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpointConfiguration

class ApiEndpointResolver(private val dispatcher: CoroutineDispatcher = Dispatchers.IO) {
fun resolve(apiEndpointConfiguration: ApiEndpointConfiguration): ApiEndpoint? =
try {
runBlockingWithTimeout(
context = CoroutineScope(dispatcher).coroutineContext,
timeout = MAX_RESOLVE_WAIT_TIME_MS
) {
apiEndpointConfiguration.apiEndpoint()
}
} catch (e: CancellationException) {
Logger.e("Could not resolve api endpoint configuration")
null
}

companion object {
const val MAX_RESOLVE_WAIT_TIME_MS = 5000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.lifecycle.lifecycleScope
import arrow.atomic.AtomicInt
import co.touchlab.kermit.Logger
import java.io.File
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -43,6 +42,7 @@ class MullvadVpnService : TalpidVpnService() {
private lateinit var keyguardManager: KeyguardManager

private lateinit var apiEndpointConfiguration: ApiEndpointConfiguration
private lateinit var apiEndpointResolver: ApiEndpointResolver
private lateinit var managementService: ManagementService
private lateinit var migrateSplitTunneling: MigrateSplitTunneling
private lateinit var intentProvider: IntentProvider
Expand Down Expand Up @@ -71,6 +71,7 @@ class MullvadVpnService : TalpidVpnService() {
get<NotificationManager>()

apiEndpointConfiguration = get()
apiEndpointResolver = get()
migrateSplitTunneling = get()
intentProvider = get()
connectionProxy = get()
Expand All @@ -79,18 +80,14 @@ class MullvadVpnService : TalpidVpnService() {

keyguardManager = getSystemService<KeyguardManager>()!!

// TODO We should avoid lifecycleScope.launch (current needed due to InetSocketAddress
// with intent from API)
lifecycleScope.launch(context = Dispatchers.IO) {
prepareFiles(this@MullvadVpnService)
migrateSplitTunneling.migrate()
prepareFiles(this@MullvadVpnService)
migrateSplitTunneling.migrate()

Logger.i("Start daemon")
startDaemon()
Logger.i("Start daemon")
startDaemon()

Logger.i("Start management service")
managementService.start()
}
Logger.i("Start management service")
managementService.start()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down Expand Up @@ -161,7 +158,7 @@ class MullvadVpnService : TalpidVpnService() {
rpcSocketPath = rpcSocketFile.absolutePath,
filesDirectory = filesDir.absolutePath,
cacheDirectory = cacheDir.absolutePath,
apiEndpoint = apiEndpointConfiguration.apiEndpoint()
apiEndpoint = apiEndpointResolver.resolve(apiEndpointConfiguration)
)
Logger.i("MullvadVpnService: Daemon initialized")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpointConfiguration
import net.mullvad.mullvadvpn.lib.endpoint.CUSTOM_ENDPOINT_HTTPS_PORT
import net.mullvad.mullvadvpn.lib.endpoint.CustomApiEndpointConfiguration
import net.mullvad.mullvadvpn.lib.endpoint.DefaultApiEndpointConfiguration
import net.mullvad.mullvadvpn.service.ApiEndpointResolver
import net.mullvad.mullvadvpn.service.BuildConfig
import org.koin.dsl.bind
import org.koin.dsl.module
Expand All @@ -16,4 +17,5 @@ val apiEndpointModule = module {
DefaultApiEndpointConfiguration()
}
} bind ApiEndpointConfiguration::class
single { ApiEndpointResolver() }
}

0 comments on commit 846af74

Please sign in to comment.