Update CHANGELOG.md and rename ServerConnectionListener.kt function.

This commit is contained in:
dkadrikj 2021-11-22 10:53:58 +01:00 committed by Dino Kadrikj
parent 42b21ec123
commit b69d40f9fb
6 changed files with 16 additions and 16 deletions

View File

@ -4,10 +4,10 @@
### Changes ### Changes
- Added GuestHole support, that can be used by the clients to overcome potential blocked api. Mainly - Added Server Connection error support, that can be used by the clients to overcome potential blocked api.
this is supported now from VPN client. Mainly and initially this is supported now from VPN client for their guest hole feature.
- Clients should provide a new dependency for `GuestHoleFallbackListener` or use the default one that - Clients should provide a new dependency for `GuestHoleFallbackListener` or use the default one that
Core provides `DefaultGustHoleFallbackListener` which does not do anything. Core provides `ServerConnectionListener` which does not do anything.
## Auth [1.18.4], Network [1.15.8], Util Kotlin [1.15.3] ## Auth [1.18.4], Network [1.15.8], Util Kotlin [1.15.3]
## Auth [1.18.5], Network [1.15.9], Util Kotlin [1.15.4] ## Auth [1.18.5], Network [1.15.9], Util Kotlin [1.15.4]

View File

@ -142,13 +142,13 @@ class NetworkModule {
@Provides @Provides
@Singleton @Singleton
fun provideGuestHoleFallbackListener(): ServerConnectionListener = object: ServerConnectionListener { fun provideGuestHoleFallbackListener(): ServerConnectionListener = object: ServerConnectionListener {
override suspend fun <T> fallbackCall( override suspend fun <T> onPotentiallyBlocked(
path: String?, path: String?,
query: String?, query: String?,
blockToRetry: suspend () -> ApiResult<T> backendCall: suspend () -> ApiResult<T>
): ApiResult<T>? { ): ApiResult<T>? {
delay(1000) delay(1000)
return blockToRetry() return backendCall()
} }
} }
} }

View File

@ -137,15 +137,15 @@ class ApiManagerFactory(
): List<ApiErrorHandler<Api>> { ): List<ApiErrorHandler<Api>> {
val refreshTokenHandler = RefreshTokenHandler<Api>(sessionId, sessionProvider, sessionListener, monoClockMs) val refreshTokenHandler = RefreshTokenHandler<Api>(sessionId, sessionProvider, sessionListener, monoClockMs)
val forceUpdateHandler = ProtonForceUpdateHandler<Api>(apiClient) val forceUpdateHandler = ProtonForceUpdateHandler<Api>(apiClient)
val guestHoleHandler = ServerConnectionHandler<Api>(serverConnectionListener) val serverConnectionHandler = ServerConnectionHandler<Api>(serverConnectionListener)
val humanVerificationNeededHandler = val humanVerificationNeededHandler =
HumanVerificationNeededHandler<Api>(sessionId, clientIdProvider, humanVerificationListener, monoClockMs) HumanVerificationNeededHandler<Api>(sessionId, clientIdProvider, humanVerificationListener, monoClockMs)
val humanVerificationInvalidHandler = val humanVerificationInvalidHandler =
HumanVerificationInvalidHandler<Api>(sessionId, clientIdProvider, humanVerificationListener) HumanVerificationInvalidHandler<Api>(sessionId, clientIdProvider, humanVerificationListener)
return listOf( return listOf(
serverConnectionHandler,
refreshTokenHandler, refreshTokenHandler,
forceUpdateHandler, forceUpdateHandler,
guestHoleHandler,
humanVerificationInvalidHandler, humanVerificationInvalidHandler,
humanVerificationNeededHandler humanVerificationNeededHandler
) )

View File

@ -47,7 +47,7 @@ internal suspend fun <Api, T> safeApiCall(
} catch (e: CertificateException) { } catch (e: CertificateException) {
ApiResult.Error.Certificate(e) ApiResult.Error.Certificate(e)
} catch (e: ServerConnectionException) { } catch (e: ServerConnectionException) {
e.parse(networkManager) e.toApiResult(networkManager)
} }
if (result is ApiResult.Error) { if (result is ApiResult.Error) {
result.cause?.let { CoreLogger.e(LogTag.DEFAULT, it) } result.cause?.let { CoreLogger.e(LogTag.DEFAULT, it) }
@ -55,7 +55,7 @@ internal suspend fun <Api, T> safeApiCall(
return result return result
} }
private fun ServerConnectionException.parse(networkManager: NetworkManager): ApiResult.Error.Connection { private fun ServerConnectionException.toApiResult(networkManager: NetworkManager): ApiResult.Error.Connection {
// handle the exceptions that might indicate that the API is potentially blocked // handle the exceptions that might indicate that the API is potentially blocked
return when (originalException) { return when (originalException) {
is SSLHandshakeException -> ApiResult.Error.Certificate(this) is SSLHandshakeException -> ApiResult.Error.Certificate(this)

View File

@ -36,12 +36,12 @@ class ServerConnectionHandler<Api>(
): ApiResult<T> { ): ApiResult<T> {
if (!error.isPotentialBlocking) return error if (!error.isPotentialBlocking) return error
if (error !is ApiResult.Error.Connection) return error if (error !is ApiResult.Error.Connection) return error
// it should suspend and ask the client to establish guest hole
val result = return serverConnectionListener?.let { listener ->
globalMutex.withLock { globalMutex.withLock {
serverConnectionListener?.fallbackCall(error.path, error.query) { backend(call) } listener.onPotentiallyBlocked(error.path, error.query) { backend(call) }
} }
return result ?: error } ?: error
} }
companion object { companion object {

View File

@ -22,9 +22,9 @@ import me.proton.core.network.domain.ApiResult
interface ServerConnectionListener { interface ServerConnectionListener {
suspend fun <T> fallbackCall( suspend fun <T> onPotentiallyBlocked(
path: String?, path: String?,
query: String?, query: String?,
blockToRetry: suspend () -> ApiResult<T> backendCall: suspend () -> ApiResult<T>
): ApiResult<T>? ): ApiResult<T>?
} }