Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy support #124

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/di/Di.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ir.amirab.downloader.utils.IDiskStat
import ir.amirab.util.startup.Startup
import com.abdownloadmanager.integration.Integration
import ir.amirab.downloader.DownloadManager
import ir.amirab.util.config.datastore.createMyConfigPreferences
import ir.amirab.util.config.datastore.createMapConfigDatastore
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json
import okhttp3.Dispatcher
Expand All @@ -39,8 +39,13 @@ import com.abdownloadmanager.utils.FileIconProvider
import com.abdownloadmanager.utils.FileIconProviderUsingCategoryIcons
import com.abdownloadmanager.utils.category.*
import com.abdownloadmanager.utils.compose.IMyIcons
import com.abdownloadmanager.utils.proxy.IProxyStorage
import com.abdownloadmanager.utils.proxy.ProxyData
import com.abdownloadmanager.utils.proxy.ProxyManager
import ir.amirab.downloader.connection.proxy.ProxyStrategyProvider
import ir.amirab.downloader.monitor.IDownloadMonitor
import ir.amirab.downloader.utils.EmptyFileCreator
import ir.amirab.util.config.datastore.kotlinxSerializationDataStore

val downloaderModule = module {
single<IDownloadQueueDatabase> {
Expand Down Expand Up @@ -84,6 +89,11 @@ val downloaderModule = module {
8,
)
}
single {
ProxyManager(
get()
)
}.bind<ProxyStrategyProvider>()
single<DownloaderClient> {
OkHttpDownloaderClient(
OkHttpClient
Expand All @@ -92,9 +102,9 @@ val downloaderModule = module {
//bypass limit on concurrent connections!
maxRequests = Int.MAX_VALUE
maxRequestsPerHost = Int.MAX_VALUE
}).build()
}).build(),
get()
)

}
single {
val downloadSettings: DownloadSettings = get()
Expand Down Expand Up @@ -215,17 +225,26 @@ val appModule = module {
single {
MyIcons
}.bind<IMyIcons>()
single {
ProxyDatastoreStorage(
kotlinxSerializationDataStore(
AppInfo.optionsDir.resolve("proxySettings.json"),
get(),
ProxyData::default,
)
)
}.bind<IProxyStorage>()
single {
AppSettingsStorage(
createMyConfigPreferences(
createMapConfigDatastore(
AppInfo.configDir.resolve("appSettings.json"),
get(),
)
)
}
single {
PageStatesStorage(
createMyConfigPreferences(
createMapConfigDatastore(
AppInfo.configDir.resolve("pageStatesStorage.json"),
get(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private fun WildcardLengthUi(
verticalAlignment = Alignment.CenterVertically
) {
Multiselect(
selections = WildcardSelect.entries,
selections = entries,
selectedItem = WildcardSelect.fromWildcardLength(wildcardLength),
onSelectionChange = {
onChangeWildcardLength(
Expand Down Expand Up @@ -258,48 +258,6 @@ private fun WildcardLengthUi(
}
}

@Composable
private fun <T> Multiselect(
selections: List<T>,
selectedItem: T,
onSelectionChange: (T) -> Unit,
render: @Composable (T) -> Unit,
) {
val shape = RoundedCornerShape(6.dp)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clip(shape)
.background(myColors.surface)
) {
for (item in selections) {
val isSelected = item == selectedItem
Box(
Modifier
.padding(vertical = 4.dp, horizontal = 4.dp)
.clip(shape)
.ifThen(isSelected) {
background(LocalContentColor.current / 10)
}
.clickable {
onSelectionChange(item)
}
.padding(vertical = 2.dp, horizontal = 4.dp)
) {
WithContentAlpha(
if (isSelected) {
1f
} else {
0.5f
}
) {
render(item)
}
}
}
}
}

@Composable
private fun LabeledContent(
label: @Composable () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.abdownloadmanager.desktop.utils.convertSpeedToHumanReadable
import com.abdownloadmanager.desktop.utils.mvi.ContainsEffects
import com.abdownloadmanager.desktop.utils.mvi.supportEffects
import androidx.compose.runtime.*
import com.abdownloadmanager.utils.proxy.ProxyManager
import com.abdownloadmanager.utils.proxy.ProxyMode
import com.arkivanov.decompose.ComponentContext
import ir.amirab.util.osfileutil.FileUtils
import ir.amirab.util.flow.createMutableStateFlowFromStateFlow
Expand Down Expand Up @@ -133,6 +135,28 @@ fun defaultDownloadFolderConfig(appSettings: AppSettingsStorage): FolderConfigur
)
}

fun proxyConfig(proxyManager: ProxyManager, scope: CoroutineScope): ProxyConfigurable {
return ProxyConfigurable(
title = "Use Proxy",
description = "Use proxy for downloading files",
backedBy = proxyManager.proxyData,

validate = {
true
},
describe = {
val str = when (it.proxyMode) {
ProxyMode.Direct -> "No proxy"
ProxyMode.UseSystem -> "System proxy"
ProxyMode.Manual -> it.proxyWithRules.proxy.run {
"$type $host:$port"
}
}
"$str will be used"
}
)
}

/*
fun uiScaleConfig(appSettings: AppSettings): EnumConfigurable<Float?> {
return EnumConfigurable(
Expand Down Expand Up @@ -270,6 +294,7 @@ class SettingsComponent(
ContainsEffects<SettingPageEffects> by supportEffects() {
val appSettings by inject<AppSettingsStorage>()
val appRepository by inject<AppRepository>()
val proxyManager by inject<ProxyManager>()
val themeManager by inject<ThemeManager>()
val allConfigs = object : SettingSectionGetter {
override operator fun get(key: SettingSections): List<Configurable<*>> {
Expand All @@ -290,6 +315,7 @@ class SettingsComponent(

DownloadEngine -> listOf(
defaultDownloadFolderConfig(appSettings),
proxyConfig(proxyManager, scope),
useAverageSpeedConfig(appRepository),
speedLimitConfig(appRepository),
threadCountConfig(appRepository),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.ui.graphics.Color
import com.abdownloadmanager.desktop.pages.settings.ThemeInfo
import com.abdownloadmanager.desktop.pages.settings.configurable.BooleanConfigurable.RenderMode
import com.abdownloadmanager.desktop.ui.theme.MyColors
import com.abdownloadmanager.utils.proxy.ProxyData
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -298,3 +299,21 @@ class DayOfWeekConfigurable(
enabled = enabled,
visible = visible,
)

class ProxyConfigurable(
title: String,
description: String,
backedBy: MutableStateFlow<ProxyData>,
describe: (ProxyData) -> String,
validate: (ProxyData) -> Boolean,
enabled: StateFlow<Boolean> = DefaultEnabledValue,
visible: StateFlow<Boolean> = DefaultVisibleValue,
) : Configurable<ProxyData>(
title = title,
description = description,
backedBy = backedBy,
describe = describe,
validate = validate,
enabled = enabled,
visible = visible,
)
Loading