Add proxy support #376
parent
3729b5f2f0
commit
eb7efaaac9
@ -0,0 +1,43 @@
|
||||
package org.koitharu.kotatsu.core.network
|
||||
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.util.ext.printStackTraceDebug
|
||||
import java.io.IOException
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Proxy
|
||||
import java.net.ProxySelector
|
||||
import java.net.SocketAddress
|
||||
import java.net.URI
|
||||
|
||||
class AppProxySelector(
|
||||
private val settings: AppSettings,
|
||||
) : ProxySelector() {
|
||||
|
||||
private var cachedProxy: Proxy? = null
|
||||
|
||||
override fun select(uri: URI?): List<Proxy> {
|
||||
return listOf(getProxy())
|
||||
}
|
||||
|
||||
override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) {
|
||||
ioe?.printStackTraceDebug()
|
||||
}
|
||||
|
||||
private fun getProxy(): Proxy {
|
||||
val type = settings.proxyType
|
||||
val address = settings.proxyAddress
|
||||
val port = settings.proxyPort
|
||||
if (type == Proxy.Type.DIRECT || address.isNullOrEmpty() || port == 0) {
|
||||
return Proxy.NO_PROXY
|
||||
}
|
||||
cachedProxy?.let {
|
||||
val addr = it.address() as? InetSocketAddress
|
||||
if (addr != null && it.type() == type && addr.port == port && addr.hostString == address) {
|
||||
return it
|
||||
}
|
||||
}
|
||||
val proxy = Proxy(type, InetSocketAddress(address, port))
|
||||
cachedProxy = proxy
|
||||
return proxy
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package org.koitharu.kotatsu.settings
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.Preference
|
||||
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 java.net.Proxy
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ProxySettingsFragment : BasePreferenceFragment(R.string.proxy),
|
||||
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
addPreferencesFromResource(R.xml.pref_proxy)
|
||||
findPreference<EditTextPreference>(AppSettings.KEY_PROXY_ADDRESS)?.setOnBindEditTextListener(
|
||||
EditTextBindListener(
|
||||
inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI,
|
||||
hint = null,
|
||||
validator = DomainValidator(),
|
||||
),
|
||||
)
|
||||
findPreference<EditTextPreference>(AppSettings.KEY_PROXY_PORT)?.setOnBindEditTextListener(
|
||||
EditTextBindListener(
|
||||
inputType = EditorInfo.TYPE_CLASS_NUMBER,
|
||||
hint = null,
|
||||
validator = null,
|
||||
),
|
||||
)
|
||||
updateDependencies()
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
settings.subscribe(this)
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
settings.unsubscribe(this)
|
||||
super.onDestroyView()
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
when (key) {
|
||||
AppSettings.KEY_PROXY_TYPE -> updateDependencies()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDependencies() {
|
||||
val isProxyEnabled = settings.proxyType != Proxy.Type.DIRECT
|
||||
findPreference<Preference>(AppSettings.KEY_PROXY_ADDRESS)?.isEnabled = isProxyEnabled
|
||||
findPreference<Preference>(AppSettings.KEY_PROXY_PORT)?.isEnabled = isProxyEnabled
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<ListPreference
|
||||
android:defaultValue="DIRECT"
|
||||
android:entries="@array/proxy_types"
|
||||
android:entryValues="@array/values_proxy_types"
|
||||
android:key="proxy_type"
|
||||
android:title="@string/type"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="proxy_address"
|
||||
android:title="@string/address"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="proxy_port"
|
||||
android:title="@string/port"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
|
||||
</PreferenceScreen>
|
||||
Loading…
Reference in New Issue