From dba5dd048e7cad2f4689d19598f5f58006f33fb8 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 11 Mar 2023 16:11:43 +0100 Subject: [PATCH 1/4] Add option to disable Piped proxy --- .../com/github/libretube/api/obj/Streams.kt | 9 ++++--- .../libretube/constants/PreferenceKeys.kt | 1 + .../github/libretube/helpers/DashHelper.kt | 4 ++-- .../github/libretube/helpers/ImageHelper.kt | 4 +++- .../github/libretube/helpers/ProxyHelper.kt | 13 ++++++++++ .../libretube/services/BackgroundMode.kt | 3 ++- .../libretube/ui/adapters/CommentsAdapter.kt | 5 +++- .../libretube/ui/fragments/PlayerFragment.kt | 24 ++++++++++--------- app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/audio_video_settings.xml | 7 ++++++ 10 files changed, 53 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/Streams.kt b/app/src/main/java/com/github/libretube/api/obj/Streams.kt index 1aabe0752..aaa1e96f2 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Streams.kt @@ -2,6 +2,7 @@ package com.github.libretube.api.obj import com.github.libretube.db.obj.DownloadItem import com.github.libretube.enums.FileType +import com.github.libretube.helpers.ProxyHelper import kotlinx.datetime.LocalDate import kotlinx.serialization.Serializable @@ -54,7 +55,7 @@ data class Streams( videoId = videoId, fileName = stream?.getQualityString(fileName).orEmpty(), path = "", - url = stream?.url, + url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) }, format = videoFormat, quality = videoQuality ) @@ -71,7 +72,7 @@ data class Streams( videoId = videoId, fileName = stream?.getQualityString(fileName).orEmpty(), path = "", - url = stream?.url, + url = stream?.url?.let { ProxyHelper.unwrapIfEnabled(it) }, format = audioFormat, quality = audioQuality ) @@ -85,7 +86,9 @@ data class Streams( videoId = videoId, fileName = "${fileName}_$subtitleCode.srt", path = "", - url = subtitles.find { it.code == subtitleCode }?.url + url = subtitles.find { + it.code == subtitleCode + }?.url?.let { ProxyHelper.unwrapIfEnabled(it) } ) ) } diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 0a38fada7..6f93be93d 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -133,6 +133,7 @@ object PreferenceKeys { const val CONFIRM_UNSUBSCRIBE = "confirm_unsubscribing" const val CLEAR_BOOKMARKS = "clear_bookmarks" const val MAX_CONCURRENT_DOWNLOADS = "max_concurrent_downloads" + const val DISABLE_VIDEO_IMAGE_PROXY = "disable_video_image_proxy" /** * History diff --git a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt index b5816c7a7..cc4b81474 100644 --- a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt @@ -149,7 +149,7 @@ object DashHelper { audioChannelConfiguration.setAttribute("value", "2") val baseUrl = doc.createElement("BaseURL") - baseUrl.appendChild(doc.createTextNode(stream.url!!)) + baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapIfEnabled(stream.url!!))) val segmentBase = doc.createElement("SegmentBase") segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}") @@ -175,7 +175,7 @@ object DashHelper { representation.setAttribute("frameRate", stream.fps.toString()) val baseUrl = doc.createElement("BaseURL") - baseUrl.appendChild(doc.createTextNode(stream.url!!)) + baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapIfEnabled(stream.url!!))) val segmentBase = doc.createElement("SegmentBase") segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}") diff --git a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt index 7e2325504..9419b6e64 100644 --- a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt @@ -51,7 +51,9 @@ object ImageHelper { */ fun loadImage(url: String?, target: ImageView) { // only load the image if the data saver mode is disabled - if (!DataSaverMode.isEnabled(target.context)) target.load(url, imageLoader) + if (DataSaverMode.isEnabled(target.context) || url == null) return + val urlToLoad = ProxyHelper.unwrapIfEnabled(url) + target.load(urlToLoad, imageLoader) } fun downloadImage(context: Context, url: String, path: String) { diff --git a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt index 175fbd740..179394f41 100644 --- a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt @@ -27,4 +27,17 @@ object ProxyHelper { ?.port(proxyUrl.port) ?.toString() } + + /** + * Load the YT url directly instead of the proxied Piped URL if enabled + */ + fun unwrapIfEnabled(url: String): String { + if (!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false)) return url + return url.toHttpUrlOrNull()?.let { + it.newBuilder() + .host(it.queryParameter("host").orEmpty()) + .removeAllQueryParameters("host") + .build() + }.toString() + } } diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index d0b1138da..e41a359ce 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -29,6 +29,7 @@ import com.github.libretube.extensions.toID import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper.checkForSegments import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams +import com.github.libretube.helpers.ProxyHelper import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.PlayingQueue import com.google.android.exoplayer2.ExoPlayer @@ -304,7 +305,7 @@ class BackgroundMode : LifecycleService() { } val mediaItem = MediaItem.Builder() - .setUri(uri) + .setUri(ProxyHelper.rewriteUrl(uri)) .build() player?.setMediaItem(mediaItem) } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt index d313e9bca..98cf79bfc 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt @@ -116,7 +116,10 @@ class CommentsAdapter( } } root.setOnLongClickListener { - ClipboardHelper.save(root.context, comment.commentText.orEmpty().parseAsHtml().toString()) + ClipboardHelper.save( + root.context, + comment.commentText.orEmpty().parseAsHtml().toString() + ) Toast.makeText(root.context, R.string.copied, Toast.LENGTH_SHORT).show() true } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 0b9b44911..65ea9ee0c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -67,6 +67,7 @@ import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper.checkForSegments import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams import com.github.libretube.helpers.PreferenceHelper +import com.github.libretube.helpers.ProxyHelper import com.github.libretube.obj.ShareData import com.github.libretube.obj.VideoResolution import com.github.libretube.services.BackgroundMode @@ -424,7 +425,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { streams.hls ?: return@setOnLongClickListener true // start an intent with video as mimetype using the hls stream - val uri: Uri = Uri.parse(streams.hls) + val uri = Uri.parse(ProxyHelper.unwrapIfEnabled(streams.hls!!)) val intent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(uri, "video/*") putExtra(Intent.EXTRA_TITLE, streams.title) @@ -970,14 +971,12 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { } } - if (streams.hls != null) { - binding.relPlayerPip.setOnClickListener { - if (SDK_INT < Build.VERSION_CODES.O) return@setOnClickListener - try { - activity?.enterPictureInPictureMode(getPipParams()) - } catch (e: Exception) { - e.printStackTrace() - } + binding.relPlayerPip.setOnClickListener { + if (SDK_INT < Build.VERSION_CODES.O) return@setOnClickListener + try { + activity?.enterPictureInPictureMode(getPipParams()) + } catch (e: Exception) { + e.printStackTrace() } } initializeRelatedVideos(streams.relatedStreams.filter { !it.title.isNullOrBlank() }) @@ -1285,7 +1284,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { // DASH !PreferenceHelper.getBoolean(PreferenceKeys.USE_HLS_OVER_DASH, false) && streams.videoStreams.isNotEmpty() -> { // only use the dash manifest generated by YT if either it's a livestream or no other source is available - val uri = streams.dash?.toUri().takeIf { + val uri = streams.dash?.let { ProxyHelper.unwrapIfEnabled(it) }?.toUri().takeIf { streams.livestream || streams.videoStreams.isEmpty() } ?: let { val manifest = DashHelper.createManifest(streams) @@ -1300,7 +1299,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { } // HLS streams.hls != null -> { - setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8) + setMediaSource( + ProxyHelper.unwrapIfEnabled(streams.hls!!).toUri(), + MimeTypes.APPLICATION_M3U8 + ) } // NO STREAM FOUND else -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index df0ee17e7..68e499e88 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -453,6 +453,8 @@ Playing next in %1$s LBRY HLS Use LBRY HLS for streaming if available. + Disable Piped proxy + Load videos and images directly from YouTube\'s servers. Only enable the option if you use a VPN anyways! Download Service diff --git a/app/src/main/res/xml/audio_video_settings.xml b/app/src/main/res/xml/audio_video_settings.xml index b3b6e75a2..b37529fef 100644 --- a/app/src/main/res/xml/audio_video_settings.xml +++ b/app/src/main/res/xml/audio_video_settings.xml @@ -96,6 +96,13 @@ app:title="@string/codecs" app:useSimpleSummaryProvider="true" /> + + \ No newline at end of file From 5011c35413aa7e07161a7040fc3f2f33ca5ea4b3 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 11 Mar 2023 16:59:58 +0100 Subject: [PATCH 2/4] Fix the preference key of disable proxy preference --- .../java/com/github/libretube/helpers/ProxyHelper.kt | 9 ++++++++- app/src/main/res/xml/audio_video_settings.xml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt index 179394f41..34754a1a7 100644 --- a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt @@ -32,7 +32,14 @@ object ProxyHelper { * Load the YT url directly instead of the proxied Piped URL if enabled */ fun unwrapIfEnabled(url: String): String { - if (!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false)) return url + if (!PreferenceHelper.getBoolean( + PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, + false + ) + ) { + return url + } + return url.toHttpUrlOrNull()?.let { it.newBuilder() .host(it.queryParameter("host").orEmpty()) diff --git a/app/src/main/res/xml/audio_video_settings.xml b/app/src/main/res/xml/audio_video_settings.xml index b37529fef..26321a671 100644 --- a/app/src/main/res/xml/audio_video_settings.xml +++ b/app/src/main/res/xml/audio_video_settings.xml @@ -101,7 +101,7 @@ android:icon="@drawable/ic_server" android:summary="@string/disable_proxy_summary" android:title="@string/disable_proxy" - app:key="lbry_hls" /> + app:key="disable_video_image_proxy" /> From a4e8a7b1dc46c0db77d69d13a6bc7d4f82160cff Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 15 Mar 2023 20:03:21 +0100 Subject: [PATCH 3/4] Avoid null as url when un-proxying --- app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt index 34754a1a7..7e4d6c2b9 100644 --- a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt @@ -45,6 +45,7 @@ object ProxyHelper { .host(it.queryParameter("host").orEmpty()) .removeAllQueryParameters("host") .build() - }.toString() + .toString() + } ?: url } } From eb38b167584e19a9ba9fa2beb0fd6e96ddada751 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 21 Mar 2023 19:17:51 +0100 Subject: [PATCH 4/4] Add note about YT music when disabling proxy --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 19cbea815..6dacebb78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -454,7 +454,7 @@ LBRY HLS Use LBRY HLS for streaming if available. Disable Piped proxy - Load videos and images directly from YouTube\'s servers. Only enable the option if you use a VPN anyways! + Load videos and images directly from YouTube\'s servers. Only enable the option if you use a VPN anyways! Note that this might not work with content from YT music.