Fix proxy authentication
This commit is contained in:
@@ -6,12 +6,24 @@ import okhttp3.Request
|
|||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.Route
|
import okhttp3.Route
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
|
import java.net.PasswordAuthentication
|
||||||
|
import java.net.Proxy
|
||||||
|
|
||||||
class ProxyAuthenticator(
|
class ProxyAuthenticator(
|
||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
) : Authenticator {
|
) : Authenticator, java.net.Authenticator() {
|
||||||
|
|
||||||
|
init {
|
||||||
|
setDefault(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun authenticate(route: Route?, response: Response): Request? {
|
override fun authenticate(route: Route?, response: Response): Request? {
|
||||||
|
if (!isProxyEnabled()) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (response.request.header(CommonHeaders.PROXY_AUTHORIZATION) != null) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
val login = settings.proxyLogin ?: return null
|
val login = settings.proxyLogin ?: return null
|
||||||
val password = settings.proxyPassword ?: return null
|
val password = settings.proxyPassword ?: return null
|
||||||
val credential = Credentials.basic(login, password)
|
val credential = Credentials.basic(login, password)
|
||||||
@@ -19,4 +31,15 @@ class ProxyAuthenticator(
|
|||||||
.header(CommonHeaders.PROXY_AUTHORIZATION, credential)
|
.header(CommonHeaders.PROXY_AUTHORIZATION, credential)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getPasswordAuthentication(): PasswordAuthentication? {
|
||||||
|
if (!isProxyEnabled()) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val login = settings.proxyLogin ?: return null
|
||||||
|
val password = settings.proxyPassword ?: return null
|
||||||
|
return PasswordAuthentication(login, password.toCharArray())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isProxyEnabled() = settings.proxyType != Proxy.Type.DIRECT
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user