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
This commit is contained in:
Aria Moradi
2024-02-19 19:36:39 +03:30
committed by GitHub
parent dda86cdb93
commit fc53d69f82
6 changed files with 67 additions and 9 deletions

View File

@@ -37,8 +37,11 @@ class SettingsMutation {
// proxy // proxy
updateSetting(settings.socksProxyEnabled, serverConfig.socksProxyEnabled) updateSetting(settings.socksProxyEnabled, serverConfig.socksProxyEnabled)
updateSetting(settings.socksProxyVersion, serverConfig.socksProxyVersion)
updateSetting(settings.socksProxyHost, serverConfig.socksProxyHost) updateSetting(settings.socksProxyHost, serverConfig.socksProxyHost)
updateSetting(settings.socksProxyPort, serverConfig.socksProxyPort) updateSetting(settings.socksProxyPort, serverConfig.socksProxyPort)
updateSetting(settings.socksProxyUsername, serverConfig.socksProxyUsername)
updateSetting(settings.socksProxyPassword, serverConfig.socksProxyPassword)
// webUI // webUI
updateSetting(settings.webUIFlavor?.uiName, serverConfig.webUIFlavor) updateSetting(settings.webUIFlavor?.uiName, serverConfig.webUIFlavor)

View File

@@ -21,8 +21,11 @@ interface Settings : Node {
// proxy // proxy
val socksProxyEnabled: Boolean? val socksProxyEnabled: Boolean?
val socksProxyVersion: Int?
val socksProxyHost: String? val socksProxyHost: String?
val socksProxyPort: String? val socksProxyPort: String?
val socksProxyUsername: String?
val socksProxyPassword: String?
// webUI // webUI
// requires restart (found no way to mutate (serve + "unserve") served files during runtime), exclude for now // 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?, override val port: Int?,
// proxy // proxy
override val socksProxyEnabled: Boolean?, override val socksProxyEnabled: Boolean?,
override val socksProxyVersion: Int?,
override val socksProxyHost: String?, override val socksProxyHost: String?,
override val socksProxyPort: String?, override val socksProxyPort: String?,
override val socksProxyUsername: String?,
override val socksProxyPassword: String?,
// webUI // webUI
override val webUIFlavor: WebUIFlavor?, override val webUIFlavor: WebUIFlavor?,
override val initialOpenInBrowserEnabled: Boolean?, override val initialOpenInBrowserEnabled: Boolean?,
@@ -150,8 +156,11 @@ class SettingsType(
override val port: Int, override val port: Int,
// proxy // proxy
override val socksProxyEnabled: Boolean, override val socksProxyEnabled: Boolean,
override val socksProxyVersion: Int,
override val socksProxyHost: String, override val socksProxyHost: String,
override val socksProxyPort: String, override val socksProxyPort: String,
override val socksProxyUsername: String,
override val socksProxyPassword: String,
// webUI // webUI
override val webUIFlavor: WebUIFlavor, override val webUIFlavor: WebUIFlavor,
override val initialOpenInBrowserEnabled: Boolean, override val initialOpenInBrowserEnabled: Boolean,
@@ -207,8 +216,11 @@ class SettingsType(
config.port.value, config.port.value,
// proxy // proxy
config.socksProxyEnabled.value, config.socksProxyEnabled.value,
config.socksProxyVersion.value,
config.socksProxyHost.value, config.socksProxyHost.value,
config.socksProxyPort.value, config.socksProxyPort.value,
config.socksProxyUsername.value,
config.socksProxyPassword.value,
// webUI // webUI
WebUIFlavor.from(config.webUIFlavor.value), WebUIFlavor.from(config.webUIFlavor.value),
config.initialOpenInBrowserEnabled.value, config.initialOpenInBrowserEnabled.value,

View File

@@ -80,8 +80,11 @@ class ServerConfig(getConfig: () -> Config, val moduleName: String = SERVER_CONF
// proxy // proxy
val socksProxyEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter) val socksProxyEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val socksProxyVersion: MutableStateFlow<Int> by OverrideConfigValue(IntConfigAdapter)
val socksProxyHost: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter) val socksProxyHost: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
val socksProxyPort: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter) val socksProxyPort: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
val socksProxyUsername: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
val socksProxyPassword: MutableStateFlow<String> by OverrideConfigValue(StringConfigAdapter)
// webUI // webUI
val webUIEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter) val webUIEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)

View File

@@ -15,6 +15,7 @@ import io.javalin.plugin.json.JavalinJackson
import io.javalin.plugin.json.JsonMapper import io.javalin.plugin.json.JsonMapper
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import mu.KotlinLogging import mu.KotlinLogging
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
@@ -207,19 +208,52 @@ fun applicationSetup() {
serverConfig.subscribeTo( serverConfig.subscribeTo(
combine( combine(
serverConfig.socksProxyEnabled, serverConfig.socksProxyEnabled,
serverConfig.socksProxyVersion,
serverConfig.socksProxyHost, serverConfig.socksProxyHost,
serverConfig.socksProxyPort, serverConfig.socksProxyPort,
) { proxyEnabled, proxyHost, proxyPort -> serverConfig.socksProxyUsername,
Triple(proxyEnabled, proxyHost, proxyPort) serverConfig.socksProxyPassword,
}, ) { vargs ->
{ (proxyEnabled, proxyHost, proxyPort) -> data class ProxySettings(
logger.info("Socks Proxy changed - enabled= $proxyEnabled, proxy= $proxyHost:$proxyPort") 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) { if (proxyEnabled) {
System.getProperties()["socksProxyHost"] = proxyHost System.setProperty("socksProxyHost", proxyHost)
System.getProperties()["socksProxyPort"] = proxyPort 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 { } else {
System.getProperties()["socksProxyHost"] = "" System.clearProperty("socksProxyHost")
System.getProperties()["socksProxyPort"] = "" System.clearProperty("socksProxyPort")
System.clearProperty("socksProxyVersion")
} }
}, },
ignoreInitialValue = false, ignoreInitialValue = false,

View File

@@ -4,8 +4,11 @@ server.port = 4567
# Socks5 proxy # Socks5 proxy
server.socksProxyEnabled = false server.socksProxyEnabled = false
server.socksProxyVersion = 5 # 4 or 5
server.socksProxyHost = "" server.socksProxyHost = ""
server.socksProxyPort = "" server.socksProxyPort = ""
server.socksProxyUsername = ""
server.socksProxyPassword = ""
# webUI # webUI
server.webUIEnabled = true server.webUIEnabled = true

View File

@@ -4,8 +4,11 @@ server.port = 4567
# Socks5 proxy # Socks5 proxy
server.socksProxyEnabled = false server.socksProxyEnabled = false
server.socksProxyVersion = 5 # 4 or 5
server.socksProxyHost = "" server.socksProxyHost = ""
server.socksProxyPort = "" server.socksProxyPort = ""
server.socksProxyUsername = ""
server.socksProxyPassword = ""
# downloader # downloader
server.downloadAsCbz = false server.downloadAsCbz = false