From be695fa85e321c0aa01f9f5dce30c795aa36970e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 5 Aug 2024 15:59:02 +0200 Subject: [PATCH] Add dummy name resolver Co-authored-by: Jonatan Rhodin --- .../lib/daemon/grpc/ManagementService.kt | 8 ++++- .../daemon/grpc/resolver/DummyNameResolver.kt | 35 +++++++++++++++++++ .../grpc/resolver/DummyNameResolverFactory.kt | 18 ++++++++++ .../mullvadvpn/service/MullvadVpnService.kt | 7 ++-- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolver.kt create mode 100644 android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolverFactory.kt diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt index d05df426f831..7f1ae90b45ec 100644 --- a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt @@ -41,6 +41,7 @@ import mullvad_daemon.management_interface.ManagementInterface import mullvad_daemon.management_interface.ManagementServiceGrpcKt import net.mullvad.mullvadvpn.lib.daemon.grpc.mapper.fromDomain import net.mullvad.mullvadvpn.lib.daemon.grpc.mapper.toDomain +import net.mullvad.mullvadvpn.lib.daemon.grpc.resolver.DummyNameResolverFactory import net.mullvad.mullvadvpn.lib.daemon.grpc.util.LogInterceptor import net.mullvad.mullvadvpn.lib.daemon.grpc.util.connectivityFlow import net.mullvad.mullvadvpn.lib.model.AccountData @@ -142,6 +143,10 @@ class ManagementService( rpcSocketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM ) + // We need to provide a DummyNameResolver to avoid default NameResolver making incorrect + // InetSocketAddress look ups. For more info see: + // https://github.com/grpc/grpc-java/issues/11442 + .nameResolverFactory(DummyNameResolverFactory()) .build() val connectionState: StateFlow = @@ -194,7 +199,6 @@ class ManagementService( error("ManagementService already started") } - channel.resetConnectBackoff() job = scope.launch { subscribeEvents() } } @@ -204,6 +208,8 @@ class ManagementService( job = null } + fun enterIdle() = channel.enterIdle() + private suspend fun subscribeEvents() = withContext(Dispatchers.IO) { launch { diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolver.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolver.kt new file mode 100644 index 000000000000..37a3c9cdc07f --- /dev/null +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolver.kt @@ -0,0 +1,35 @@ +package net.mullvad.mullvadvpn.lib.daemon.grpc.resolver + +import io.grpc.EquivalentAddressGroup +import io.grpc.NameResolver +import java.net.InetSocketAddress + +class DummyNameResolver : NameResolver() { + + override fun getServiceAuthority(): String = SERVICE_AUTHORITY + + override fun start(listener: Listener2) { + val resolutionResult = + ResolutionResult.newBuilder() + .setAddresses( + listOf( + EquivalentAddressGroup( + InetSocketAddress.createUnresolved(DUMMY_HOST, DUMMY_PORT) + ) + ) + ) + .build() + + listener.onResult(resolutionResult) + } + + override fun shutdown() { + // Do nothing + } + + companion object { + const val SERVICE_AUTHORITY = "localhost" + private const val DUMMY_HOST = "" + private const val DUMMY_PORT = 80 + } +} diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolverFactory.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolverFactory.kt new file mode 100644 index 000000000000..1a8141985d79 --- /dev/null +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/resolver/DummyNameResolverFactory.kt @@ -0,0 +1,18 @@ +package net.mullvad.mullvadvpn.lib.daemon.grpc.resolver + +import io.grpc.NameResolver +import java.net.URI + +class DummyNameResolverFactory : NameResolver.Factory() { + override fun newNameResolver(targetUri: URI, args: NameResolver.Args): NameResolver { + return DummyNameResolver() + } + + override fun getDefaultScheme(): String { + return DNS_SCHEME + } + + companion object { + private const val DNS_SCHEME = "dns" + } +} diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index 40eb9bc312b3..370e014393a0 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -94,10 +94,10 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider { prepareFiles(this@MullvadVpnService) migrateSplitTunneling.migrate() - Logger.d("Start daemon") + Logger.i("Start daemon") startDaemon() - Logger.d("Start management service") + Logger.i("Start management service") managementService.start() } } @@ -213,6 +213,9 @@ class MullvadVpnService : TalpidVpnService(), ShouldBeOnForegroundProvider { Logger.i("Shutdown MullvadDaemon") MullvadDaemon.shutdown() + Logger.i("Enter Idle") + managementService.enterIdle() + Logger.i("Shutdown complete") super.onDestroy() }