Skip to content

Commit

Permalink
fix(anilib): video access
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Jan 18, 2025
1 parent f7f5c05 commit d4ef48d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 29 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ dependencies {
implementation(libs.androidx.media3.exoplayer.dash)
implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.exoplayer.hls)
implementation(libs.androidx.media3.datasource.okhttp)

// Firebase
implementation(platform(libs.com.google.firebase.bom))
Expand All @@ -140,7 +139,6 @@ dependencies {

// Network
implementation(libs.com.github.kittinunf.fuel)
implementation(files("../libs/re-bridge-1.0-SNAPSHOT.jar"))

// Datastore
implementation(libs.androidx.datastore.preferences)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.shirabox.app.service.media

import ddosGuardBridge
import com.google.common.net.InternetDomainName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
Expand All @@ -27,6 +27,7 @@ import org.shirabox.core.util.Util
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
import java.net.URI


object DownloadsServiceHelper {
Expand Down Expand Up @@ -154,10 +155,15 @@ object DownloadsServiceHelper {
if (file.exists() && mediaDownloadTask.pauseData == null) file.delete()
if (!file.exists()) file.createNewFile()

val host = URI(mediaDownloadTask.url).host
val topDomain = InternetDomainName.from(host).topPrivateDomain()

val request = Request.Builder()
.url(mediaDownloadTask.url)
.addHeader("Range", "bytes=$pausedBytes-")
.ddosGuardBridge(mediaDownloadTask.url) // DDoS-Guard.net bypass
.apply {
mpegHeaders("https://$topDomain/").forEach { addHeader(it.key, it.value) }
}
.build()

val responseBody = okHttpClient.newCall(request).execute().body
Expand Down Expand Up @@ -408,6 +414,21 @@ object DownloadsServiceHelper {
.forEach { it.state.value = TaskState.STOPPED }
}

fun mpegHeaders(rootHost: String): Map<String, String> = mapOf(
"Accept" to "*/*",
"Accept-Encoding" to "identity;q=1, *;q=0",
"Origin" to rootHost,
"Priority" to "i",
"Referer" to rootHost,
"Sec-Ch-Ua" to "\"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
"Sec-Ch-Ua-Mobile" to "?0",
"Sec-Ch-Ua-Platform" to "\"Windows\"",
"Sec-Fetch-Dest" to "video",
"Sec-Fetch-Mode" to "cors",
"Sec-Fetch-Site" to "same-site",
"User-Agent" to "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)

class ForcedInterruptionException : Exception()

class MpegRepairmentFailureException : Exception()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.FileDataSource
import androidx.media3.datasource.okhttp.OkHttpDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.hls.HlsMediaSource
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import androidx.media3.ui.PlayerView
import ddosGuardBridge
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import org.shirabox.app.ui.activity.player.presentation.PlayerScaffold
import org.shirabox.app.ui.activity.player.presentation.SettingsBottomSheet
import org.shirabox.app.ui.activity.player.presentation.hideControls
import org.shirabox.core.datastore.DataStoreScheme
import org.shirabox.core.entity.EpisodeEntity
import org.shirabox.core.model.Quality
import org.shirabox.core.model.StreamProtocol
import org.shirabox.data.content.AbstractContentRepository


@OptIn(UnstableApi::class)
Expand Down Expand Up @@ -131,22 +129,13 @@ private fun PlayerSurface(exoPlayer: ExoPlayer, model: PlayerViewModel, playlist
.createMediaSource(MediaItem.fromUri(uri))
}

// DDos-Guard.net bypass
val client = OkHttpClient.Builder().addInterceptor { chain ->
val request = chain.request().newBuilder()
.ddosGuardBridge(chain.request().url.toString()).build()
chain.proceed(request)
}.build()
val okHttpDataSourceFactory = OkHttpDataSource.Factory(client)

val streamMediaSource = when (repository.streamingType) {
StreamProtocol.MPEG -> ProgressiveMediaSource
.Factory(okHttpDataSourceFactory)
.Factory(DefaultHttpDataSource.Factory().setDefaultRequestProperties(repository.hostHeaders()))
StreamProtocol.HLS -> HlsMediaSource
.Factory(DefaultHttpDataSource.Factory())
.Factory(DefaultHttpDataSource.Factory().setDefaultRequestProperties(repository.hostHeaders()))
}.createMediaSource(MediaItem.fromUri(stream.value))


return@map offlineMediaSource ?: streamMediaSource
})

Expand All @@ -173,6 +162,7 @@ private fun PlayerSurface(exoPlayer: ExoPlayer, model: PlayerViewModel, playlist
rebuildExoPlaylist(
exoPlayer,
playlist,
model.currentRepository!!,
model.currentQuality
)
}
Expand Down Expand Up @@ -212,20 +202,29 @@ private fun PlayerSurface(exoPlayer: ExoPlayer, model: PlayerViewModel, playlist
SettingsBottomSheet(exoPlayer = exoPlayer, playlist = playlist, model = model)
}

@OptIn(UnstableApi::class)
private fun rebuildExoPlaylist(
exoPlayer: ExoPlayer,
playlist: List<EpisodeEntity>,
repository: AbstractContentRepository,
currentQuality: Quality
) {
val currentPosition = exoPlayer.currentPosition
val currentItemIndex = exoPlayer.currentMediaItemIndex

exoPlayer.apply {
setMediaItems(
playlist.map { MediaItem.fromUri(it.videos[currentQuality] ?: "") },
currentItemIndex,
currentPosition
setMediaSources(
playlist.map {
val streamMediaSource = when (repository.streamingType) {
StreamProtocol.MPEG -> ProgressiveMediaSource
.Factory(DefaultHttpDataSource.Factory().setDefaultRequestProperties(repository.hostHeaders()))
StreamProtocol.HLS -> HlsMediaSource
.Factory(DefaultHttpDataSource.Factory().setDefaultRequestProperties(repository.hostHeaders()))
}.createMediaSource(MediaItem.fromUri(it.videos[currentQuality] ?: ""))
streamMediaSource
}
)
seekTo(currentItemIndex, currentPosition)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.LocalActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.AnimatedVisibility
Expand All @@ -32,10 +33,6 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.NotificationAdd
import androidx.compose.material.icons.outlined.NotificationsOff
import androidx.compose.material.icons.rounded.Share
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
Expand Down Expand Up @@ -76,7 +73,6 @@ import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -157,7 +153,7 @@ fun Resource(
model: ResourceViewModel = hiltViewModel(),
colorScheme: ColorScheme = MaterialTheme.colorScheme
) {
val activity = LocalContext.current as Activity?
val activity = LocalActivity.current
val content = model.content.value
val isFavourite = model.isFavourite.value
val isRefreshing = model.isRefreshing.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ abstract class AbstractContentRepository (

abstract suspend fun searchEpisodes(content: Content): Flow<List<EpisodeEntity>>
abstract suspend fun searchEpisodesInRange(content: Content, range: IntRange): Flow<List<EpisodeEntity>>
abstract fun hostHeaders(): Map<String, String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ class AniLibRepository : AbstractContentRepository(
} catch (ex: Exception) { throw ex }
}

override fun hostHeaders(): Map<String, String> = mapOf(
"Accept" to "*/*",
"Accept-Encoding" to "identity;q=1, *;q=0",
"Origin" to "https://anilib.me",
"Priority" to "i",
"Referer" to "https://anilib.me/",
"Sec-Ch-Ua" to "\"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
"Sec-Ch-Ua-Mobile" to "?0",
"Sec-Ch-Ua-Platform" to "\"Windows\"",
"Sec-Fetch-Dest" to "video",
"Sec-Fetch-Mode" to "cors",
"Sec-Fetch-Site" to "same-site",
"User-Agent" to "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)


private fun fetchEpisode(id: Int): Flow<EpisodeObject> = flow {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class LibriaRepository : AbstractContentRepository(
)
}

override fun hostHeaders(): Map<String, String> = emptyMap()

private suspend fun lookupEpisodes(content: Content): List<EpisodeEntity> {
try {
val id = search(content)?.id
Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", versi
androidx-media3-exoplayer-dash = { module = "androidx.media3:media3-exoplayer-dash", version.ref = "androidxMedia3" }
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "androidxMedia3" }
androidx-media3-exoplayer-hls = { module = "androidx.media3:media3-exoplayer-hls", version.ref = "androidxMedia3" }
androidx-media3-datasource-okhttp = { module = "androidx.media3:media3-datasource-okhttp", version.ref = "androidxMedia3"}
com-google-firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "comGoogleFirebase" }
io-coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "ioCoilKt" }
io-coil-kt-svg = { module = "io.coil-kt:coil-svg", version.ref = "ioCoilKt" }
Expand Down
Binary file removed libs/re-bridge-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit d4ef48d

Please sign in to comment.