mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2026-01-31 16:04:08 +01:00
Add proxy support
This commit is contained in:
@@ -7,22 +7,43 @@
|
||||
package ca.gosyer.data.server
|
||||
|
||||
import ca.gosyer.BuildConfig
|
||||
import ca.gosyer.data.server.model.Proxy
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.ProxyBuilder
|
||||
import io.ktor.client.engine.ProxyConfig
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.features.json.JsonFeature
|
||||
import io.ktor.client.features.json.serializer.KotlinxSerializer
|
||||
import io.ktor.client.features.logging.LogLevel
|
||||
import io.ktor.client.features.logging.Logging
|
||||
import io.ktor.client.features.websocket.WebSockets
|
||||
import io.ktor.http.URLBuilder
|
||||
import kotlinx.serialization.json.Json
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
|
||||
typealias Http = HttpClient
|
||||
|
||||
internal class HttpProvider @Inject constructor() : Provider<Http> {
|
||||
internal class HttpProvider @Inject constructor(
|
||||
private val serverPreferences: ServerPreferences
|
||||
) : Provider<Http> {
|
||||
override fun get(): Http {
|
||||
return HttpClient(OkHttp) {
|
||||
engine {
|
||||
proxy = when (serverPreferences.proxy().get()) {
|
||||
Proxy.NO_PROXY -> ProxyConfig.NO_PROXY
|
||||
Proxy.HTTP_PROXY -> ProxyBuilder.http(
|
||||
URLBuilder(
|
||||
host = serverPreferences.proxyHttpHost().get(),
|
||||
port = serverPreferences.proxyHttpPort().get()
|
||||
).build()
|
||||
)
|
||||
Proxy.SOCKS_PROXY -> ProxyBuilder.socks(
|
||||
serverPreferences.proxySocksHost().get(),
|
||||
serverPreferences.proxySocksPort().get()
|
||||
)
|
||||
}
|
||||
}
|
||||
install(JsonFeature) {
|
||||
serializer = KotlinxSerializer(
|
||||
Json {
|
||||
|
||||
@@ -8,6 +8,7 @@ package ca.gosyer.data.server
|
||||
|
||||
import ca.gosyer.common.prefs.Preference
|
||||
import ca.gosyer.common.prefs.PreferenceStore
|
||||
import ca.gosyer.data.server.model.Proxy
|
||||
|
||||
class ServerPreferences(private val preferenceStore: PreferenceStore) {
|
||||
|
||||
@@ -26,4 +27,24 @@ class ServerPreferences(private val preferenceStore: PreferenceStore) {
|
||||
fun serverUrl(): Preference<String> {
|
||||
return ServerUrlPreference("", server(), port())
|
||||
}
|
||||
|
||||
fun proxy(): Preference<Proxy> {
|
||||
return preferenceStore.getJsonObject("proxy", Proxy.NO_PROXY, Proxy.serializer())
|
||||
}
|
||||
|
||||
fun proxyHttpHost(): Preference<String> {
|
||||
return preferenceStore.getString("proxy_http_host")
|
||||
}
|
||||
|
||||
fun proxyHttpPort(): Preference<Int> {
|
||||
return preferenceStore.getInt("proxy_http_port")
|
||||
}
|
||||
|
||||
fun proxySocksHost(): Preference<String> {
|
||||
return preferenceStore.getString("proxy_socks_host")
|
||||
}
|
||||
|
||||
fun proxySocksPort(): Preference<Int> {
|
||||
return preferenceStore.getInt("proxy_socks_port")
|
||||
}
|
||||
}
|
||||
|
||||
16
src/main/kotlin/ca/gosyer/data/server/model/Proxy.kt
Normal file
16
src/main/kotlin/ca/gosyer/data/server/model/Proxy.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.data.server.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
enum class Proxy {
|
||||
NO_PROXY,
|
||||
HTTP_PROXY,
|
||||
SOCKS_PROXY
|
||||
}
|
||||
@@ -10,8 +10,11 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import ca.gosyer.data.server.ServerPreferences
|
||||
import ca.gosyer.data.server.model.Proxy
|
||||
import ca.gosyer.ui.base.components.Toolbar
|
||||
import ca.gosyer.ui.base.prefs.ChoicePreference
|
||||
import ca.gosyer.ui.base.prefs.EditTextPreference
|
||||
import ca.gosyer.ui.base.prefs.SwitchPreference
|
||||
import ca.gosyer.ui.base.prefs.asStateIn
|
||||
@@ -24,16 +27,31 @@ import com.github.zsoltk.compose.router.BackStack
|
||||
import javax.inject.Inject
|
||||
|
||||
class SettingsServerViewModel @Inject constructor(
|
||||
private val serverPreferences: ServerPreferences
|
||||
serverPreferences: ServerPreferences
|
||||
) : ViewModel() {
|
||||
val host = serverPreferences.host().asStateIn(scope)
|
||||
val server = serverPreferences.server().asStateIn(scope)
|
||||
val port = serverPreferences.port().asStringStateIn(scope)
|
||||
|
||||
val proxy = serverPreferences.proxy().asStateIn(scope)
|
||||
|
||||
@Composable
|
||||
fun getProxyChoices() = mapOf(
|
||||
Proxy.NO_PROXY to stringResource("no_proxy"),
|
||||
Proxy.HTTP_PROXY to stringResource("http_proxy"),
|
||||
Proxy.SOCKS_PROXY to stringResource("socks_proxy")
|
||||
)
|
||||
|
||||
val httpHost = serverPreferences.proxyHttpHost().asStateIn(scope)
|
||||
val httpPort = serverPreferences.proxyHttpPort().asStringStateIn(scope)
|
||||
val socksHost = serverPreferences.proxySocksHost().asStateIn(scope)
|
||||
val socksPort = serverPreferences.proxySocksPort().asStringStateIn(scope)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SettingsServerScreen(navController: BackStack<Route>) {
|
||||
val vm = viewModel<SettingsServerViewModel>()
|
||||
val proxy by vm.proxy.collectAsState()
|
||||
Column {
|
||||
Toolbar(stringResource("settings_server_screen"), navController, true)
|
||||
SwitchPreference(preference = vm.host, title = stringResource("host_server"))
|
||||
@@ -44,6 +62,28 @@ fun SettingsServerScreen(navController: BackStack<Route>) {
|
||||
item {
|
||||
EditTextPreference(vm.port, stringResource("server_port"), subtitle = vm.port.collectAsState().value)
|
||||
}
|
||||
item {
|
||||
ChoicePreference(vm.proxy, vm.getProxyChoices(), stringResource("server_proxy"))
|
||||
}
|
||||
when (proxy) {
|
||||
Proxy.NO_PROXY -> Unit
|
||||
Proxy.HTTP_PROXY -> {
|
||||
item {
|
||||
EditTextPreference(vm.httpHost, stringResource("http_proxy"), vm.httpHost.collectAsState().value)
|
||||
}
|
||||
item {
|
||||
EditTextPreference(vm.httpPort, stringResource("http_port"), vm.httpPort.collectAsState().value)
|
||||
}
|
||||
}
|
||||
Proxy.SOCKS_PROXY -> {
|
||||
item {
|
||||
EditTextPreference(vm.socksHost, stringResource("socks_proxy"), vm.socksHost.collectAsState().value)
|
||||
}
|
||||
item {
|
||||
EditTextPreference(vm.socksPort, stringResource("socks_port"), vm.socksPort.collectAsState().value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,4 +170,10 @@
|
||||
<string name="host_server">Host server inside TachideskJUI</string>
|
||||
<string name="server_url">Server URL</string>
|
||||
<string name="server_port">Server PORT</string>
|
||||
<string name="no_proxy">No Proxy</string>
|
||||
<string name="http_proxy">HTTP Proxy</string>
|
||||
<string name="socks_proxy">SOCKS Proxy</string>
|
||||
<string name="server_proxy">Proxy access to the server</string>
|
||||
<string name="http_port">HTTP PORT</string>
|
||||
<string name="socks_port">SOCKS PORT</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user