2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-11-21 18:42:46 +05:30
|
|
|
|
|
|
|
import com.github.libretube.api.RetrofitInstance
|
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-12-19 21:28:34 +05:30
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2023-02-12 20:03:31 +05:30
|
|
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
2023-06-19 23:41:12 +05:30
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import okhttp3.Request
|
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
|
|
|
|
|
|
|
/**
|
2023-06-19 23:41:12 +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
|
|
|
*/
|
2023-06-19 23:41:12 +05:30
|
|
|
fun unwrapStreamUrl(url: String): String {
|
|
|
|
return if (shouldDisableProxy(url)) unwrapUrl(url) else url
|
|
|
|
}
|
|
|
|
|
|
|
|
fun shouldDisableProxy(url: String) = when {
|
|
|
|
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false) -> false
|
|
|
|
PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> isUrlUsable(
|
|
|
|
unwrapUrl(url)
|
|
|
|
)
|
|
|
|
else -> true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun unwrapImageUrl(url: String): String = if (
|
|
|
|
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false)
|
|
|
|
) url else unwrapUrl(url)
|
2023-03-11 21:29:58 +05:30
|
|
|
|
2023-06-19 23:41:12 +05:30
|
|
|
/**
|
|
|
|
* 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 {
|
2023-03-11 20:41:43 +05:30
|
|
|
it.newBuilder()
|
|
|
|
.host(it.queryParameter("host").orEmpty())
|
|
|
|
.removeAllQueryParameters("host")
|
|
|
|
.build()
|
2023-03-16 00:33:21 +05:30
|
|
|
.toString()
|
|
|
|
} ?: url
|
2023-06-19 23:41:12 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the Piped url to a YouTube url (or not) based on preferences
|
|
|
|
*/
|
|
|
|
private fun isUrlUsable(url: String): Boolean {
|
|
|
|
val client = OkHttpClient.Builder().build()
|
|
|
|
val request = Request.Builder().url(url).method("HEAD", null).build()
|
|
|
|
return runCatching {
|
|
|
|
client.newCall(request).execute().code == 200
|
|
|
|
}.getOrDefault(false)
|
2023-03-11 20:41:43 +05:30
|
|
|
}
|
2022-11-21 18:42:46 +05:30
|
|
|
}
|