Skip to content

Commit

Permalink
update: use proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Sep 4, 2024
1 parent 90682eb commit 16969e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.ooni.probe.data.models

import kotlinx.coroutines.flow.firstOrNull
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.Settings_Proxy_Custom
import ooniprobe.composeapp.generated.resources.Settings_Proxy_None
import ooniprobe.composeapp.generated.resources.Settings_Proxy_Psiphon
import org.jetbrains.compose.resources.StringResource
import org.ooni.probe.data.repositories.PreferenceRepository

const val IP_ADDRESS = (
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]" +
Expand Down Expand Up @@ -92,38 +90,42 @@ class ProxySettings {

companion object {
/**
* Creates a new ProxySettings object from the values stored in the preference repository.
* @param preferenceRepository the [PreferenceRepository].
* Creates a new ProxySettings object from the given protocol, hostname, and port.
*
* @param protocol the proxy protocol
* @param hostname the proxy hostname
* @param port the proxy port
*/
suspend fun newProxySettings(preferenceRepository: PreferenceRepository): ProxySettings {
val protocol =
preferenceRepository.getValueByKey(SettingsKey.PROXY_PROTOCOL).firstOrNull() as String?
fun newProxySettings(protocol: String?, hostname: String?, port: String?): ProxySettings {
val settings = ProxySettings()

when (protocol) {
ProxyProtocol.NONE.protocol -> {
settings.protocol = ProxyProtocol.NONE
}
ProxyProtocol.PSIPHON.protocol -> {
settings.protocol = ProxyProtocol.PSIPHON
}
ProxyProtocol.SOCKS5.protocol, ProxyProtocol.HTTP.protocol, ProxyProtocol.HTTPS.protocol -> {
// ProxyProtocol.valueOf will only accept one of the values in ProxyProtocol
// as in the enum definition(uppercase).
settings.protocol =
ProxyProtocol.valueOf(protocol)
}
else -> {
// This is where we will extend the code to add support for
// more proxies, e.g., HTTP proxies.
throw InvalidProxyURL("unhandled URL scheme")
protocol?.let { protocol ->
when (protocol) {
ProxyProtocol.NONE.name -> {
settings.protocol = ProxyProtocol.NONE
}
ProxyProtocol.PSIPHON.name -> {
settings.protocol = ProxyProtocol.PSIPHON
}
ProxyProtocol.SOCKS5.name, ProxyProtocol.HTTP.name, ProxyProtocol.HTTPS.name -> {
// ProxyProtocol.valueOf will only accept one of the values in ProxyProtocol
// as in the enum definition(uppercase).
settings.protocol =
ProxyProtocol.valueOf(protocol)
}
else -> {
// This is where we will extend the code to add support for
// more proxies, e.g., HTTP proxies.
throw InvalidProxyURL("unhandled URL scheme")
}
}
} ?: run {
settings.protocol = ProxyProtocol.NONE
}

settings.apply {
hostname =
preferenceRepository.getValueByKey(SettingsKey.PROXY_HOSTNAME).firstOrNull() as String? ?: ""
port = (preferenceRepository.getValueByKey(SettingsKey.PROXY_PORT).firstOrNull() as Int? ?: "").toString()
this.hostname = hostname ?: ""
this.port = (port as Int? ?: "").toString()
}
return settings
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.flow.first
import org.ooni.engine.models.EnginePreferences
import org.ooni.engine.models.TaskLogLevel
import org.ooni.engine.models.WebConnectivityCategory
import org.ooni.probe.data.models.ProxySettings
import org.ooni.probe.data.models.SettingsKey
import org.ooni.probe.data.repositories.PreferenceRepository
import kotlin.time.Duration.Companion.seconds
Expand All @@ -21,8 +22,11 @@ class GetEnginePreferences(
},
uploadResults = getValueForKey(SettingsKey.UPLOAD_RESULTS) == true,
maxRuntime = (getValueForKey(SettingsKey.MAX_RUNTIME) as? Int)?.seconds,
// TODO: Implement proxy settings
proxy = null,
proxy = ProxySettings.newProxySettings(
protocol = getValueForKey(SettingsKey.PROXY_PROTOCOL) as? String,
hostname = getValueForKey(SettingsKey.PROXY_HOSTNAME) as? String,
port = getValueForKey(SettingsKey.PROXY_PORT) as? String,
).getProxyString(),
)

private suspend fun getEnabledCategories(): List<String> {
Expand Down

0 comments on commit 16969e5

Please sign in to comment.