Defined ConfigurableParametersModule which currently provides base url.

MAILAND-1662
This commit is contained in:
Tomasz Giszczak 2021-04-09 16:15:31 +02:00
parent f14e8779d9
commit 025ff87595
8 changed files with 128 additions and 26 deletions

View File

@ -21,8 +21,8 @@ package ch.protonmail.android.api
import ch.protonmail.android.api.models.doh.Proxies
import ch.protonmail.android.api.segments.connectivity.ConnectivityApi
import ch.protonmail.android.api.segments.connectivity.PingService
import ch.protonmail.android.core.Constants
import ch.protonmail.android.core.ProtonMailApplication
import ch.protonmail.android.di.BaseUrl
import ch.protonmail.android.utils.Logger
import kotlinx.coroutines.runBlocking
import retrofit2.Response
@ -34,14 +34,12 @@ interface INetworkSwitcher {
fun <T> tryRequest(callFun: suspend (PingService) -> Response<T>)
}
/**
* Created by dinokadrikj on 3/6/20.
*/
@Singleton
class NetworkSwitcher @Inject constructor(
private val api: ProtonMailApiManager,
private val apiProvider: ProtonMailApiProvider,
private val protonOkHttpProvider: OkHttpProvider,
@BaseUrl private val baseUrl: String,
networkConfigurator: NetworkConfigurator
) : INetworkSwitcher {
@ -54,7 +52,7 @@ class NetworkSwitcher @Inject constructor(
* party proxies.
*/
override fun reconfigureProxy(proxies: Proxies?) { // TODO: DoH this can be done without null
val proxyItem = proxies?.getCurrentActiveProxy()?.baseUrl ?: Constants.ENDPOINT_URI
val proxyItem = proxies?.getCurrentActiveProxy()?.baseUrl ?: baseUrl
Logger.doLog("NetworkSwitcher", "proxyItem url is: $proxyItem")
val newApi: ProtonMailApi = apiProvider.rebuild(protonOkHttpProvider, proxyItem)
api.reset(newApi)

View File

@ -19,8 +19,8 @@
package ch.protonmail.android.api
import ch.protonmail.android.api.cookie.ProtonCookieStore
import ch.protonmail.android.core.Constants
import ch.protonmail.android.di.AlternativeApiPins
import ch.protonmail.android.di.BaseUrl
import ch.protonmail.android.utils.crypto.ServerTimeInterceptor
import okhttp3.ConnectionSpec
import okhttp3.Interceptor
@ -30,7 +30,8 @@ import javax.inject.Singleton
@Singleton
class OkHttpProvider @Inject constructor(
@AlternativeApiPins private val pinnedKeyHashes: List<String>
@AlternativeApiPins private val pinnedKeyHashes: List<String>,
@BaseUrl private val baseUrl: String
) {
// cache the clients, this way we can have separate client for every Uri/Url
private val okHttpClients = HashMap<String, ProtonOkHttpClient>()
@ -51,13 +52,14 @@ class OkHttpProvider @Inject constructor(
if (okHttpClients.containsKey(id)) {
return okHttpClients[id]!! // we can safely enforce here because we are sure it exists
}
okHttpClients[id] = if (endpointUri == Constants.ENDPOINT_URI) {
okHttpClients[id] = if (endpointUri == baseUrl) {
DefaultOkHttpClient(
timeout,
interceptor,
loggingLevel,
connectionSpecs,
serverTimeInterceptor,
baseUrl,
cookieStore
)
} else {

View File

@ -19,7 +19,6 @@
package ch.protonmail.android.api
import ch.protonmail.android.api.cookie.ProtonCookieStore
import ch.protonmail.android.core.Constants
import ch.protonmail.android.utils.AppUtil
import ch.protonmail.android.utils.crypto.ServerTimeInterceptor
import com.datatheorem.android.trustkit.TrustKit
@ -104,6 +103,7 @@ class DefaultOkHttpClient(
loggingLevel: HttpLoggingInterceptor.Level,
connectionSpecs: List<ConnectionSpec>,
serverTimeInterceptor: ServerTimeInterceptor?,
baseUrl: String,
cookieStore: ProtonCookieStore?
) : ProtonOkHttpClient(
timeout,
@ -111,7 +111,7 @@ class DefaultOkHttpClient(
loggingLevel,
connectionSpecs,
serverTimeInterceptor,
Constants.ENDPOINT_URI,
baseUrl,
cookieStore
) {

View File

@ -20,7 +20,6 @@ package ch.protonmail.android.api.models.doh
import android.content.SharedPreferences
import ch.protonmail.android.core.Constants
import ch.protonmail.libs.core.preferences.get
import com.google.gson.Gson
// region constants
@ -28,14 +27,13 @@ const val PREF_DNS_OVER_HTTPS_API_URL_LIST = "pref_dns_over_https_api_url_list"
const val PREF_DNS_OVER_HTTPS_API_URL = "pref_dns_over_https_api_url"
// endregion
/**
* Created by dinokadrikj on 3/5/20.
*/
class Proxies constructor(
val proxyList: ProxyList,
val prefs: SharedPreferences) {
var isDohActive : Boolean = false
class Proxies constructor(
val proxyList: ProxyList,
val prefs: SharedPreferences
) {
var isDohActive: Boolean = false
// init {
// save()
@ -92,7 +90,7 @@ class Proxies constructor(
prefs.edit().putString(PREF_DNS_OVER_HTTPS_API_URL, domain).apply()
}
fun getCurrentWorkingProxyDomain() : String {
fun getCurrentWorkingProxyDomain(): String {
return prefs.getString(PREF_DNS_OVER_HTTPS_API_URL, Constants.ENDPOINT_URI)!!
}
@ -104,10 +102,11 @@ class Proxies constructor(
* If you want to only get what has already been saved, just do not use that parameter.
*/
fun getInstance(proxyList: ProxyList? = null, prefs: SharedPreferences): Proxies {
return INSTANCE ?: synchronized(this) { INSTANCE ?:
if (proxyList != null) {
return Proxies(proxyList, prefs)
}
return INSTANCE ?: synchronized(this) {
INSTANCE
?: if (proxyList != null) {
return Proxies(proxyList, prefs)
}
val storedValue = prefs.getString(PREF_DNS_OVER_HTTPS_API_URL_LIST, null)
val proxyItems = if (storedValue != null) {
Gson().fromJson(storedValue, ProxyList::class.java)
@ -122,4 +121,4 @@ class Proxies constructor(
data class ProxyList(val proxies: List<ProxyItem>) {
constructor() : this(emptyList<ProxyItem>())
}
}

View File

@ -197,7 +197,8 @@ object ApplicationModule {
okHttpProvider: OkHttpProvider,
@DefaultSharedPreferences prefs: SharedPreferences,
userNotifier: UserNotifier,
sessionManager: SessionManager
sessionManager: SessionManager,
@BaseUrl baseUrl: String
): ProtonRetrofitBuilder {
// userManager.user.allowSecureConnectionsViaThirdParties)
@ -205,7 +206,7 @@ object ApplicationModule {
val dnsOverHttpsHost =
if (user != null && !user.usingDefaultApi)
Proxies.getInstance(null, prefs).getCurrentWorkingProxyDomain()
else Constants.ENDPOINT_URI
else baseUrl
return ProtonRetrofitBuilder(
userManager,

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.di
import ch.protonmail.android.core.Constants
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
object ConfigurableParametersModule {
@BaseUrl
@Provides
fun provideBaseUrl(): String = Constants.ENDPOINT_URI
}

View File

@ -75,3 +75,13 @@ annotation class DohProviders
@Retention(AnnotationRetention.BINARY)
@Target(allowedTargets = [AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION])
annotation class MessageDaoQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
@Target(allowedTargets = [AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION])
annotation class SearchMessageDaoQualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
@Target(allowedTargets = [AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION, AnnotationTarget.FIELD])
annotation class BaseUrl

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.uitests.tests
import ch.protonmail.android.di.BaseUrl
import ch.protonmail.android.uitests.robots.login.LoginRobot
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import org.junit.Rule
import org.robolectric.annotation.Config
import javax.inject.Inject
import kotlin.test.BeforeTest
import kotlin.test.Test
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class HiltTestDemo {
@get:Rule
var hiltRule = HiltAndroidRule(this)
private val loginRobot = LoginRobot()
@Inject
@BaseUrl
lateinit var baseUrl: String
@BeforeTest
fun init() {
hiltRule.inject()
}
@Test
fun justTestIt() {
}
}