Proxy authenticaton support #376

This commit is contained in:
Koitharu
2023-06-02 19:48:39 +03:00
parent 661e502003
commit 548c41fbf9
10 changed files with 87 additions and 2 deletions

View File

@@ -79,7 +79,7 @@ afterEvaluate {
}
dependencies {
//noinspection GradleDependency
implementation('com.github.KotatsuApp:kotatsu-parsers:fa7ea5b16a') {
implementation('com.github.KotatsuApp:kotatsu-parsers:44e28b40d3') {
exclude group: 'org.json', module: 'json'
}

View File

@@ -14,6 +14,7 @@ object CommonHeaders {
const val ACCEPT_ENCODING = "Accept-Encoding"
const val AUTHORIZATION = "Authorization"
const val CACHE_CONTROL = "Cache-Control"
const val PROXY_AUTHORIZATION = "Proxy-Authorization"
val CACHE_CONTROL_NO_STORE: CacheControl
get() = CacheControl.Builder().noStore().build()

View File

@@ -59,6 +59,7 @@ interface NetworkModule {
writeTimeout(20, TimeUnit.SECONDS)
cookieJar(cookieJar)
proxySelector(AppProxySelector(settings))
proxyAuthenticator(ProxyAuthenticator(settings))
dns(DoHManager(cache, settings))
if (settings.isSSLBypassEnabled) {
bypassSSLErrors()

View File

@@ -0,0 +1,22 @@
package org.koitharu.kotatsu.core.network
import okhttp3.Authenticator
import okhttp3.Credentials
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.koitharu.kotatsu.core.prefs.AppSettings
class ProxyAuthenticator(
private val settings: AppSettings,
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
val login = settings.proxyLogin ?: return null
val password = settings.proxyPassword ?: return null
val credential = Credentials.basic(login, password)
return response.request.newBuilder()
.header(CommonHeaders.PROXY_AUTHORIZATION, credential)
.build()
}
}

View File

@@ -298,6 +298,12 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
val proxyPort: Int
get() = prefs.getString(KEY_PROXY_PORT, null)?.toIntOrNull() ?: 0
val proxyLogin: String?
get() = prefs.getString(KEY_PROXY_LOGIN, null)?.takeUnless { it.isEmpty() }
val proxyPassword: String?
get() = prefs.getString(KEY_PROXY_PASSWORD, null)?.takeUnless { it.isEmpty() }
var localListOrder: SortOrder
get() = prefs.getEnumValue(KEY_LOCAL_LIST_ORDER, SortOrder.NEWEST)
set(value) = prefs.edit { putEnumValue(KEY_LOCAL_LIST_ORDER, value) }
@@ -451,6 +457,9 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
const val KEY_PROXY_TYPE = "proxy_type"
const val KEY_PROXY_ADDRESS = "proxy_address"
const val KEY_PROXY_PORT = "proxy_port"
const val KEY_PROXY_AUTH = "proxy_auth"
const val KEY_PROXY_LOGIN = "proxy_login"
const val KEY_PROXY_PASSWORD = "proxy_password"
const val KEY_IMAGES_PROXY = "images_proxy"
// About

View File

@@ -146,7 +146,7 @@ class ContentSettingsFragment :
summary = if (type == Proxy.Type.DIRECT || address.isNullOrEmpty() || port == 0) {
context.getString(R.string.disabled)
} else {
"$type $address:$port"
"$address:$port"
}
}
}

View File

@@ -6,11 +6,13 @@ import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.ui.BasePreferenceFragment
import org.koitharu.kotatsu.settings.utils.EditTextBindListener
import org.koitharu.kotatsu.settings.utils.PasswordSummaryProvider
import java.net.Proxy
@AndroidEntryPoint
@@ -33,6 +35,16 @@ class ProxySettingsFragment : BasePreferenceFragment(R.string.proxy),
validator = null,
),
)
findPreference<EditTextPreference>(AppSettings.KEY_PROXY_PASSWORD)?.let { pref ->
pref.setOnBindEditTextListener(
EditTextBindListener(
inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_PASSWORD,
hint = null,
validator = null,
),
)
pref.summaryProvider = PasswordSummaryProvider()
}
updateDependencies()
}
@@ -56,5 +68,8 @@ class ProxySettingsFragment : BasePreferenceFragment(R.string.proxy),
val isProxyEnabled = settings.proxyType != Proxy.Type.DIRECT
findPreference<Preference>(AppSettings.KEY_PROXY_ADDRESS)?.isEnabled = isProxyEnabled
findPreference<Preference>(AppSettings.KEY_PROXY_PORT)?.isEnabled = isProxyEnabled
findPreference<PreferenceCategory>(AppSettings.KEY_PROXY_AUTH)?.isEnabled = isProxyEnabled
findPreference<Preference>(AppSettings.KEY_PROXY_LOGIN)?.isEnabled = isProxyEnabled
findPreference<Preference>(AppSettings.KEY_PROXY_PASSWORD)?.isEnabled = isProxyEnabled
}
}

View File

@@ -0,0 +1,19 @@
package org.koitharu.kotatsu.settings.utils
import android.text.TextUtils
import androidx.preference.EditTextPreference
import androidx.preference.Preference
class PasswordSummaryProvider() : Preference.SummaryProvider<EditTextPreference> {
private val delegate = EditTextPreference.SimpleSummaryProvider.getInstance()
override fun provideSummary(preference: EditTextPreference): CharSequence? {
val summary = delegate.provideSummary(preference)
return if (summary != null && !TextUtils.isEmpty(preference.text)) {
String(CharArray(summary.length) { '\u2022' })
} else {
summary
}
}
}

View File

@@ -429,4 +429,7 @@
<string name="images_proxy_title">Images optimization proxy</string>
<string name="images_procy_description">Use the wsrv.nl service to reduce traffic usage and speed up image loading if possible</string>
<string name="invert_colors">Invert colors</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="authorization_optional">Authorization (optional)</string>
</resources>

View File

@@ -21,4 +21,19 @@
android:title="@string/port"
app:useSimpleSummaryProvider="true" />
<PreferenceCategory
android:key="proxy_auth"
android:title="@string/authorization_optional">
<EditTextPreference
android:key="proxy_login"
android:title="@string/username"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:key="proxy_password"
android:title="@string/password" />
</PreferenceCategory>
</PreferenceScreen>