From fc53d69f82058208b43c53d3940f60c3ac4cac87 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Mon, 19 Feb 2024 19:36:39 +0330 Subject: [PATCH] Add auth and version support to socks proxy (#883) * Add auth support to socsk proxy * better logging * fix lint issue * implement fixes and version * add to test reference too --- .../graphql/mutations/SettingsMutation.kt | 3 ++ .../tachidesk/graphql/types/SettingsType.kt | 12 +++++ .../suwayomi/tachidesk/server/ServerConfig.kt | 3 ++ .../suwayomi/tachidesk/server/ServerSetup.kt | 52 +++++++++++++++---- .../src/main/resources/server-reference.conf | 3 ++ .../src/test/resources/server-reference.conf | 3 ++ 6 files changed, 67 insertions(+), 9 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt index 2da0455a0..c88b1e85e 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt @@ -37,8 +37,11 @@ class SettingsMutation { // proxy updateSetting(settings.socksProxyEnabled, serverConfig.socksProxyEnabled) + updateSetting(settings.socksProxyVersion, serverConfig.socksProxyVersion) updateSetting(settings.socksProxyHost, serverConfig.socksProxyHost) updateSetting(settings.socksProxyPort, serverConfig.socksProxyPort) + updateSetting(settings.socksProxyUsername, serverConfig.socksProxyUsername) + updateSetting(settings.socksProxyPassword, serverConfig.socksProxyPassword) // webUI updateSetting(settings.webUIFlavor?.uiName, serverConfig.webUIFlavor) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt index 0c4f45efb..5a28e5135 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt @@ -21,8 +21,11 @@ interface Settings : Node { // proxy val socksProxyEnabled: Boolean? + val socksProxyVersion: Int? val socksProxyHost: String? val socksProxyPort: String? + val socksProxyUsername: String? + val socksProxyPassword: String? // webUI // requires restart (found no way to mutate (serve + "unserve") served files during runtime), exclude for now @@ -92,8 +95,11 @@ data class PartialSettingsType( override val port: Int?, // proxy override val socksProxyEnabled: Boolean?, + override val socksProxyVersion: Int?, override val socksProxyHost: String?, override val socksProxyPort: String?, + override val socksProxyUsername: String?, + override val socksProxyPassword: String?, // webUI override val webUIFlavor: WebUIFlavor?, override val initialOpenInBrowserEnabled: Boolean?, @@ -150,8 +156,11 @@ class SettingsType( override val port: Int, // proxy override val socksProxyEnabled: Boolean, + override val socksProxyVersion: Int, override val socksProxyHost: String, override val socksProxyPort: String, + override val socksProxyUsername: String, + override val socksProxyPassword: String, // webUI override val webUIFlavor: WebUIFlavor, override val initialOpenInBrowserEnabled: Boolean, @@ -207,8 +216,11 @@ class SettingsType( config.port.value, // proxy config.socksProxyEnabled.value, + config.socksProxyVersion.value, config.socksProxyHost.value, config.socksProxyPort.value, + config.socksProxyUsername.value, + config.socksProxyPassword.value, // webUI WebUIFlavor.from(config.webUIFlavor.value), config.initialOpenInBrowserEnabled.value, diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt index 8e811314d..afab08317 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt @@ -80,8 +80,11 @@ class ServerConfig(getConfig: () -> Config, val moduleName: String = SERVER_CONF // proxy val socksProxyEnabled: MutableStateFlow by OverrideConfigValue(BooleanConfigAdapter) + val socksProxyVersion: MutableStateFlow by OverrideConfigValue(IntConfigAdapter) val socksProxyHost: MutableStateFlow by OverrideConfigValue(StringConfigAdapter) val socksProxyPort: MutableStateFlow by OverrideConfigValue(StringConfigAdapter) + val socksProxyUsername: MutableStateFlow by OverrideConfigValue(StringConfigAdapter) + val socksProxyPassword: MutableStateFlow by OverrideConfigValue(StringConfigAdapter) // webUI val webUIEnabled: MutableStateFlow by OverrideConfigValue(BooleanConfigAdapter) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt index 864c2a3f3..61ef2f066 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt @@ -15,6 +15,7 @@ import io.javalin.plugin.json.JavalinJackson import io.javalin.plugin.json.JsonMapper import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.serialization.json.Json import mu.KotlinLogging import org.bouncycastle.jce.provider.BouncyCastleProvider @@ -207,19 +208,52 @@ fun applicationSetup() { serverConfig.subscribeTo( combine( serverConfig.socksProxyEnabled, + serverConfig.socksProxyVersion, serverConfig.socksProxyHost, serverConfig.socksProxyPort, - ) { proxyEnabled, proxyHost, proxyPort -> - Triple(proxyEnabled, proxyHost, proxyPort) - }, - { (proxyEnabled, proxyHost, proxyPort) -> - logger.info("Socks Proxy changed - enabled= $proxyEnabled, proxy= $proxyHost:$proxyPort") + serverConfig.socksProxyUsername, + serverConfig.socksProxyPassword, + ) { vargs -> + data class ProxySettings( + val proxyEnabled: Boolean, + val socksProxyVersion: Int, + val proxyHost: String, + val proxyPort: String, + val proxyUsername: String, + val proxyPassword: String, + ) + ProxySettings( + vargs[0] as Boolean, + vargs[1] as Int, + vargs[2] as String, + vargs[3] as String, + vargs[4] as String, + vargs[5] as String, + ) + }.distinctUntilChanged(), + { (proxyEnabled, proxyVersion, proxyHost, proxyPort, proxyUsername, proxyPassword) -> + logger.info( + "Socks Proxy changed - enabled=$proxyEnabled address=$proxyHost:$proxyPort , username=$proxyUsername, password=[REDACTED]", + ) if (proxyEnabled) { - System.getProperties()["socksProxyHost"] = proxyHost - System.getProperties()["socksProxyPort"] = proxyPort + System.setProperty("socksProxyHost", proxyHost) + System.setProperty("socksProxyPort", proxyPort) + System.setProperty("socksProxyVersion", proxyVersion.toString()) + + if (proxyUsername.isNotBlank()) { + System.setProperty("java.net.socks.username", proxyUsername) + } else { + System.clearProperty("java.net.socks.username") + } + if (proxyPassword.isNotBlank()) { + System.setProperty("java.net.socks.password", proxyPassword) + } else { + System.clearProperty("java.net.socks.password") + } } else { - System.getProperties()["socksProxyHost"] = "" - System.getProperties()["socksProxyPort"] = "" + System.clearProperty("socksProxyHost") + System.clearProperty("socksProxyPort") + System.clearProperty("socksProxyVersion") } }, ignoreInitialValue = false, diff --git a/server/src/main/resources/server-reference.conf b/server/src/main/resources/server-reference.conf index 9ff691b76..5b4bd174c 100644 --- a/server/src/main/resources/server-reference.conf +++ b/server/src/main/resources/server-reference.conf @@ -4,8 +4,11 @@ server.port = 4567 # Socks5 proxy server.socksProxyEnabled = false +server.socksProxyVersion = 5 # 4 or 5 server.socksProxyHost = "" server.socksProxyPort = "" +server.socksProxyUsername = "" +server.socksProxyPassword = "" # webUI server.webUIEnabled = true diff --git a/server/src/test/resources/server-reference.conf b/server/src/test/resources/server-reference.conf index 99566947e..7670bb577 100644 --- a/server/src/test/resources/server-reference.conf +++ b/server/src/test/resources/server-reference.conf @@ -4,8 +4,11 @@ server.port = 4567 # Socks5 proxy server.socksProxyEnabled = false +server.socksProxyVersion = 5 # 4 or 5 server.socksProxyHost = "" server.socksProxyPort = "" +server.socksProxyUsername = "" +server.socksProxyPassword = "" # downloader server.downloadAsCbz = false