From a2367ef14f6c9b371d95f41d0758494e3f99fe14 Mon Sep 17 00:00:00 2001 From: unclejay <2KPKNc#6RaKl> Date: Mon, 16 Mar 2020 21:12:15 +0100 Subject: [PATCH] added network proxy configuration --- CHANGES.md | 2 +- .../im/vector/matrix/android/api/Matrix.kt | 4 +++- .../android/api/config/ProxyConfiguration.kt | 24 +++++++++++++++++++ .../android/internal/di/NetworkModule.kt | 9 ++++++- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/api/config/ProxyConfiguration.kt diff --git a/CHANGES.md b/CHANGES.md index d26237fd13..b1034f9ad6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,7 +14,7 @@ Translations 🗣: - SDK API changes ⚠️: - - + - initialize with proxy configuration Build 🧱: - diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt index 22ac0324cf..4ebbeced8c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt @@ -23,6 +23,7 @@ import androidx.work.WorkManager import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.BuildConfig import im.vector.matrix.android.api.auth.AuthenticationService +import im.vector.matrix.android.api.config.ProxyConfiguration import im.vector.matrix.android.api.crypto.MXCryptoConfig import im.vector.matrix.android.internal.SessionManager import im.vector.matrix.android.internal.crypto.attachments.ElementToDecrypt @@ -37,7 +38,8 @@ import javax.inject.Inject data class MatrixConfiguration( val applicationFlavor: String = "Default-application-flavor", - val cryptoConfig: MXCryptoConfig = MXCryptoConfig() + val cryptoConfig: MXCryptoConfig = MXCryptoConfig(), + val proxyConfig: ProxyConfiguration? = null ) { interface Provider { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/config/ProxyConfiguration.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/config/ProxyConfiguration.kt new file mode 100644 index 0000000000..b23ffa82f9 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/config/ProxyConfiguration.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.matrix.android.api.config + +import java.net.Proxy + +/** + * This is the configuration to use a proxy to connect to the matrix servers + */ +data class ProxyConfiguration(val hostname: String, val port: Int, val proxyType: Proxy.Type) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt index 4d6c66b7ed..a3ebeb5e11 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt @@ -21,6 +21,7 @@ import com.squareup.moshi.Moshi import dagger.Module import dagger.Provides import im.vector.matrix.android.BuildConfig +import im.vector.matrix.android.api.MatrixConfiguration import im.vector.matrix.android.internal.network.TimeOutInterceptor import im.vector.matrix.android.internal.network.UserAgentInterceptor import im.vector.matrix.android.internal.network.interceptors.CurlLoggingInterceptor @@ -28,6 +29,8 @@ import im.vector.matrix.android.internal.network.interceptors.FormattedJsonHttpL import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import okreplay.OkReplayInterceptor +import java.net.InetSocketAddress +import java.net.Proxy import java.util.concurrent.TimeUnit @Module @@ -64,7 +67,8 @@ internal object NetworkModule { @Provides @JvmStatic @Unauthenticated - fun providesOkHttpClient(stethoInterceptor: StethoInterceptor, + fun providesOkHttpClient(matrixConfiguration: MatrixConfiguration, + stethoInterceptor: StethoInterceptor, timeoutInterceptor: TimeOutInterceptor, userAgentInterceptor: UserAgentInterceptor, httpLoggingInterceptor: HttpLoggingInterceptor, @@ -82,6 +86,9 @@ internal object NetworkModule { if (BuildConfig.LOG_PRIVATE_DATA) { addInterceptor(curlLoggingInterceptor) } + matrixConfiguration.proxyConfig?.let { + proxy(Proxy(it.proxyType, InetSocketAddress(it.hostname, it.port))) + } } .addInterceptor(okReplayInterceptor) .build()