2952/proxy (#2961)

* replace hard-coded strings with existing constants

* proxy port

* * custom proxy port and hostname inputs
* typesafety, refactor, linting, unit tests

* relocate ProxyConfiguration in app structure

* remove unused editTextPreference fn

* allow preference category to have no title

* refactor proxy prefs hierarchy/dependency
This commit is contained in:
kylegoetz 2022-12-07 12:29:18 -06:00 committed by GitHub
commit 25443217c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 143 additions and 21 deletions

View file

@ -18,6 +18,7 @@ package com.keylesspalace.tusky.di
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.util.Log
import at.connyduck.calladapter.networkresult.NetworkResultCallAdapterFactory
import com.google.gson.Gson
import com.google.gson.GsonBuilder
@ -27,6 +28,10 @@ import com.keylesspalace.tusky.json.Rfc3339DateJsonAdapter
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.network.MediaUploadApi
import com.keylesspalace.tusky.settings.PrefKeys.HTTP_PROXY_ENABLED
import com.keylesspalace.tusky.settings.PrefKeys.HTTP_PROXY_PORT
import com.keylesspalace.tusky.settings.PrefKeys.HTTP_PROXY_SERVER
import com.keylesspalace.tusky.settings.ProxyConfiguration
import com.keylesspalace.tusky.util.getNonNullString
import dagger.Module
import dagger.Provides
@ -38,6 +43,7 @@ import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.net.IDN
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.Date
@ -64,9 +70,9 @@ class NetworkModule {
context: Context,
preferences: SharedPreferences
): OkHttpClient {
val httpProxyEnabled = preferences.getBoolean("httpProxyEnabled", false)
val httpServer = preferences.getNonNullString("httpProxyServer", "")
val httpPort = preferences.getNonNullString("httpProxyPort", "-1").toIntOrNull() ?: -1
val httpProxyEnabled = preferences.getBoolean(HTTP_PROXY_ENABLED, false)
val httpServer = preferences.getNonNullString(HTTP_PROXY_SERVER, "")
val httpPort = preferences.getNonNullString(HTTP_PROXY_PORT, "-1").toIntOrNull() ?: -1
val cacheSize = 25 * 1024 * 1024L // 25 MiB
val builder = OkHttpClient.Builder()
.addInterceptor { chain ->
@ -87,10 +93,13 @@ class NetworkModule {
.writeTimeout(30, TimeUnit.SECONDS)
.cache(Cache(context.cacheDir, cacheSize))
if (httpProxyEnabled && httpServer.isNotEmpty() && httpPort > 0 && httpPort < 65535) {
val address = InetSocketAddress.createUnresolved(httpServer, httpPort)
builder.proxy(Proxy(Proxy.Type.HTTP, address))
if (httpProxyEnabled) {
ProxyConfiguration.create(httpServer, httpPort)?.also { conf ->
val address = InetSocketAddress.createUnresolved(IDN.toASCII(conf.hostname), conf.port)
builder.proxy(Proxy(Proxy.Type.HTTP, address))
} ?: Log.w(TAG, "Invalid proxy configuration: ($httpServer, $httpPort)")
}
return builder
.apply {
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
@ -132,4 +141,8 @@ class NetworkModule {
.build()
.create()
}
companion object {
private const val TAG = "NetworkModule"
}
}