Skip to content

Commit

Permalink
Use flavor configuration to set api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-mullvad committed Oct 16, 2023
1 parent 65c8c95 commit 55099af
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package net.mullvad.mullvadvpn.lib.endpoint
import java.net.InetSocketAddress
import kotlinx.parcelize.Parcelize

const val CUSTOM_ENDPOINT_HTTPS_PORT = 443

@Parcelize
data class CustomApiEndpointConfiguration(
val hostname: String,
Expand Down
12 changes: 10 additions & 2 deletions android/service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ android {
create(Flavors.PROD) {
dimension = FlavorDimensions.INFRASTRUCTURE
isDefault = true
// Not used for production builds.
buildConfigField("String", "API_ENDPOINT", "\"\"")
}
create(Flavors.DEVMOLE) {
dimension = FlavorDimensions.INFRASTRUCTURE
buildConfigField("String", "API_ENDPOINT", "\"api.devmole.eu\"")
}
create(Flavors.STAGEMOLE) {
dimension = FlavorDimensions.INFRASTRUCTURE
buildConfigField("String", "API_ENDPOINT", "\"api.stagemole.eu\"")
}
create(Flavors.DEVMOLE) { dimension = FlavorDimensions.INFRASTRUCTURE }
create(Flavors.STAGEMOLE) { dimension = FlavorDimensions.INFRASTRUCTURE }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import net.mullvad.mullvadvpn.lib.common.constant.KEY_DISCONNECT_ACTION
import net.mullvad.mullvadvpn.lib.common.constant.KEY_QUIT_ACTION
import net.mullvad.mullvadvpn.lib.common.constant.MAIN_ACTIVITY_CLASS
import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpointConfiguration
import net.mullvad.mullvadvpn.lib.endpoint.DefaultApiEndpointConfiguration
import net.mullvad.mullvadvpn.lib.endpoint.getApiEndpointConfigurationExtras
import net.mullvad.mullvadvpn.model.Settings
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.service.di.apiEndpointModule
import net.mullvad.mullvadvpn.service.di.vpnServiceModule
import net.mullvad.mullvadvpn.service.endpoint.ServiceEndpoint
import net.mullvad.mullvadvpn.service.notifications.AccountExpiryNotification
import net.mullvad.talpid.TalpidVpnService
import org.koin.android.ext.android.get
import org.koin.core.context.loadKoinModules

class MullvadVpnService : TalpidVpnService() {
Expand Down Expand Up @@ -58,16 +59,15 @@ class MullvadVpnService : TalpidVpnService() {
endpoint.settingsListener.settings?.let { settings -> handlePendingAction(settings) }
}

private var apiEndpointConfiguration: ApiEndpointConfiguration =
DefaultApiEndpointConfiguration()
private lateinit var apiEndpointConfiguration: ApiEndpointConfiguration

// Suppressing since the tunnel state pref should be writted immediately.
@SuppressLint("ApplySharedPref")
override fun onCreate() {
super.onCreate()
Log.d(TAG, "Initializing service")

loadKoinModules(vpnServiceModule)
loadKoinModules(listOf(vpnServiceModule, apiEndpointModule))

daemonInstance = DaemonInstance(this)
keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
Expand Down Expand Up @@ -103,9 +103,14 @@ class MullvadVpnService : TalpidVpnService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(TAG, "Starting service")

if (BuildConfig.DEBUG) {
intent?.getApiEndpointConfigurationExtras()?.let { apiEndpointConfiguration = it }
}
val intentProvidedConfiguration =
if (BuildConfig.DEBUG) {
intent?.getApiEndpointConfigurationExtras()
} else {
null
}

apiEndpointConfiguration = intentProvidedConfiguration ?: get()

daemonInstance.apply {
intermittentDaemon.registerListener(this@MullvadVpnService) { daemon ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.mullvad.mullvadvpn.service.di

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.BuildConfig
import org.koin.dsl.bind
import org.koin.dsl.module

val apiEndpointModule = module {
single {
if (BuildConfig.FLAVOR_infrastructure != "prod") {
CustomApiEndpointConfiguration(BuildConfig.API_ENDPOINT, CUSTOM_ENDPOINT_HTTPS_PORT)
} else {
DefaultApiEndpointConfiguration()
}
} bind ApiEndpointConfiguration::class
}

0 comments on commit 55099af

Please sign in to comment.