-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tr/mangatr): Fix popular/latest pages + fix http 403 (#244)
* fix: Update requestPath - Fixes popular/latest tabs * fix: Fix HTTP 403 - Steal DDoSGuardInterceptor from all/akuma * chore: Bump version
- Loading branch information
1 parent
29d1203
commit baaaa00
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
multisrc/overrides/fmreader/mangatr/src/DDoSGuardInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package eu.kanade.tachiyomi.extension.tr.mangatr | ||
|
||
import android.webkit.CookieManager | ||
import eu.kanade.tachiyomi.network.GET | ||
import okhttp3.Cookie | ||
import okhttp3.HttpUrl | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Response | ||
|
||
// Taken from src/all/akuma. | ||
class DDoSGuardInterceptor(private val client: OkHttpClient) : Interceptor { | ||
|
||
private val cookieManager by lazy { CookieManager.getInstance() } | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
val response = chain.proceed(originalRequest) | ||
|
||
// Check if DDos-GUARD is on | ||
if (response.code !in ERROR_CODES || response.header("Server") !in SERVER_CHECK) { | ||
return response | ||
} | ||
|
||
val cookies = cookieManager.getCookie(originalRequest.url.toString()) | ||
val oldCookie = if (cookies != null && cookies.isNotEmpty()) { | ||
cookies.split(";").mapNotNull { Cookie.parse(originalRequest.url, it) } | ||
} else { | ||
emptyList() | ||
} | ||
val ddg2Cookie = oldCookie.firstOrNull { it.name == "__ddg2_" } | ||
if (!ddg2Cookie?.value.isNullOrEmpty()) { | ||
return response | ||
} | ||
|
||
response.close() | ||
|
||
val newCookie = getNewCookie(originalRequest.url) | ||
?: return chain.proceed(originalRequest) | ||
|
||
val newCookieHeader = (oldCookie + newCookie) | ||
.joinToString("; ") { "${it.name}=${it.value}" } | ||
|
||
val modifiedRequest = originalRequest.newBuilder() | ||
.header("cookie", newCookieHeader) | ||
.build() | ||
|
||
return chain.proceed(modifiedRequest) | ||
} | ||
|
||
private fun getNewCookie(url: HttpUrl): Cookie? { | ||
val wellKnown = client.newCall(GET(wellKnownUrl)) | ||
.execute().body.string() | ||
.substringAfter("'", "") | ||
.substringBefore("'", "") | ||
val checkUrl = "${url.scheme}://${url.host + wellKnown}" | ||
val response = client.newCall(GET(checkUrl)).execute() | ||
return response.header("set-cookie")?.let { | ||
Cookie.parse(url, it) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val wellKnownUrl = "https://check.ddos-guard.net/check.js" | ||
private val ERROR_CODES = listOf(403) | ||
private val SERVER_CHECK = listOf("ddos-guard") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters