LibreTube/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt

88 lines
3.1 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-11-21 18:42:46 +05:30
import com.github.libretube.api.CronetHelper
2022-11-21 18:42:46 +05:30
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys
import java.net.HttpURLConnection
import java.net.URL
2022-12-19 21:28:34 +05:30
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
2023-02-12 20:03:31 +05:30
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
2022-11-21 18:42:46 +05:30
object ProxyHelper {
fun fetchProxyUrl() {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
RetrofitInstance.api.getConfig().imageProxyUrl?.let {
2023-02-12 20:03:31 +05:30
PreferenceHelper.putString(PreferenceKeys.IMAGE_PROXY_URL, it)
2022-11-21 18:42:46 +05:30
}
}
}
}
fun rewriteUrl(url: String?): String? {
2023-02-12 20:03:31 +05:30
val proxyUrl = PreferenceHelper.getString(PreferenceKeys.IMAGE_PROXY_URL, "")
.toHttpUrlOrNull() ?: return url
2022-11-21 18:42:46 +05:30
2023-02-12 20:03:31 +05:30
return url?.toHttpUrlOrNull()?.newBuilder()
?.host(proxyUrl.host)
?.port(proxyUrl.port)
?.toString()
2022-11-21 18:42:46 +05:30
}
2023-03-11 20:41:43 +05:30
/**
* Detect whether the proxy should be used or not for a given stream URL based on user preferences
2023-03-11 20:41:43 +05:30
*/
suspend fun unwrapStreamUrl(url: String): String {
return if (useYouTubeSourceWithoutProxy(url)) unwrapUrl(url) else url
}
suspend fun useYouTubeSourceWithoutProxy(url: String) = when {
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false) -> false
PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> {
// check whether the URL has content available, and disable proxy if that's the case
isUrlUsable(unwrapUrl(url))
}
else -> true
}
fun unwrapImageUrl(url: String) = if (
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false)
) {
url
} else {
unwrapUrl(url)
}
/**
* Convert a proxied Piped url to a YouTube url that's not proxied
*/
fun unwrapUrl(url: String, unwrap: Boolean = true) = url.toHttpUrlOrNull()
?.takeIf { unwrap }?.let {
val host = it.queryParameter("host")
// if there's no host parameter specified, there's no way to unwrap the URL
// and the proxied one must be used. That's the case if using LBRY.
if (host.isNullOrEmpty()) return@let url
2023-03-11 20:41:43 +05:30
it.newBuilder()
.host(host)
2023-03-11 20:41:43 +05:30
.removeAllQueryParameters("host")
.build()
2023-03-16 00:33:21 +05:30
.toString()
} ?: url
/**
* Parse the Piped url to a YouTube url (or not) based on preferences
*/
private suspend fun isUrlUsable(url: String): Boolean = withContext(Dispatchers.IO) {
return@withContext runCatching {
val connection = CronetHelper.cronetEngine.openConnection(URL(url)) as HttpURLConnection
connection.requestMethod = "HEAD"
val isSuccess = connection.responseCode == 200
connection.disconnect()
return@runCatching isSuccess
}.getOrDefault(false)
2023-03-11 20:41:43 +05:30
}
2022-11-21 18:42:46 +05:30
}