Skip to content

Commit

Permalink
🐛 fix(proxy): Improve proxy validation and handling. Fixed #687
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Jan 9, 2025
1 parent e2f7af2 commit e554d79
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ crowdin.yml
# Kotlin 2.0
.kotlin/
.idea
/build_and_sign_apk.sh
/simpmusic.jks
Original file line number Diff line number Diff line change
Expand Up @@ -618,20 +618,25 @@ class DataStoreManager(
}
}

fun getProxy(): Proxy? =
fun getJVMProxy(): Proxy? =
runBlocking {
if (usingProxy.first() == TRUE) {
val proxyType = proxyType.first()
val proxyHost = proxyHost.first()
val proxyPort = proxyPort.first()
return@runBlocking Proxy(
when (proxyType) {
ProxyType.PROXY_TYPE_HTTP -> Proxy.Type.HTTP
ProxyType.PROXY_TYPE_SOCKS -> Proxy.Type.SOCKS
},
java.net.InetSocketAddress(proxyHost, proxyPort)
)
} else {
try {
if (usingProxy.first() == TRUE) {
val proxyType = proxyType.first()
val proxyHost = proxyHost.first()
val proxyPort = proxyPort.first()
return@runBlocking Proxy(
when (proxyType) {
ProxyType.PROXY_TYPE_HTTP -> Proxy.Type.HTTP
ProxyType.PROXY_TYPE_SOCKS -> Proxy.Type.SOCKS
},
java.net.InetSocketAddress(proxyHost, proxyPort)
)
} else {
return@runBlocking null
}
} catch (e: Exception) {
e.printStackTrace()
return@runBlocking null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private fun provideMediaSourceFactory(
downloadCache,
playerCache,
context,
dataStoreManager.getProxy()
dataStoreManager.getJVMProxy()
),
downloadCache,
playerCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import coil3.compose.AsyncImage
import coil3.request.ImageRequest
import coil3.request.crossfade
import com.maxrave.kotlinytmusicscraper.extension.isTwoLetterCode
import com.maxrave.kotlinytmusicscraper.extension.isValidProxyHost
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.LIMIT_CACHE_SIZE
import com.maxrave.simpmusic.common.QUALITY
Expand Down Expand Up @@ -486,7 +487,7 @@ fun SettingScreen(
label = context.getString(R.string.proxy_host),
value = proxyHost,
verifyCodeBlock = {
(it.toHttpUrlOrNull() != null) to context.getString(R.string.invalid_host)
isValidProxyHost(it) to context.getString(R.string.invalid_host)
},
),
confirm =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,31 @@ fun String.verifyYouTubePlaylistId(): String = if (startsWith("VL")) this else "
fun String.isTwoLetterCode(): Boolean {
val regex = "^[A-Za-z]{2}$".toRegex()
return regex.matches(this)
}

fun isValidProxyHost(host: String): Boolean {
// Regular expression to validate proxy host (without port)
val proxyHostRegex = Regex(
pattern = "^(?!-)[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(?<!-)\$",
options = setOf(RegexOption.IGNORE_CASE)
)

// Return true if the host matches the regex or is an IP address
return proxyHostRegex.matches(host) || isIPAddress(host)
}

private fun isIPAddress(host: String): Boolean {
// Check if the host is an IPv4 address
val ipv4Regex = Regex(
pattern = "^([0-9]{1,3}\\.){3}[0-9]{1,3}\$"
)
if (ipv4Regex.matches(host)) {
return host.split('.').all { it.toInt() in 0..255 }
}

// Check if the host is an IPv6 address
val ipv6Regex = Regex(
pattern = "^[0-9a-fA-F:]+$"
)
return ipv6Regex.matches(host)
}

0 comments on commit e554d79

Please sign in to comment.