@@ -23,119 +23,86 @@ import com.lagradost.cloudstream3.utils.loadExtractor
2323import com.lagradost.cloudstream3.utils.newExtractorLink
2424import com.lagradost.nicehttp.NiceResponse
2525import kotlinx.coroutines.runBlocking
26+ import kotlinx.coroutines.Dispatchers
27+ import kotlinx.coroutines.delay
2628import okhttp3.Interceptor
2729import okhttp3.Response
2830import org.json.JSONObject
2931import org.jsoup.Jsoup
3032import org.jsoup.nodes.Element
3133
3234class TurnstileInterceptor (private val targetCookie : String = " _as_turnstile" ) : Interceptor {
33-
3435 @SuppressLint(" SetJavaScriptEnabled" )
3536 override fun intercept (chain : Interceptor .Chain ): Response {
3637 val originalRequest = chain.request()
3738 val url = originalRequest.url.toString()
38- val domainUrl = " ${originalRequest.url.scheme} ://${originalRequest.url.host} "
3939 val cookieManager = CookieManager .getInstance()
4040
41- cookieManager.setAcceptCookie(true )
42-
43- var currentCookies = cookieManager.getCookie(domainUrl) ? : " "
41+ var currentCookies = cookieManager.getCookie(url) ? : " "
4442 var userAgent = originalRequest.header(" User-Agent" ) ? : " "
43+
44+ var response: Response ? = null
4545 var needsRefresh = false
46- var initialResponse: Response ? = null
4746
48- if (currentCookies.contains(targetCookie)) {
47+ if (! currentCookies.contains(targetCookie)) {
48+ needsRefresh = true
49+ } else {
4950 val requestBuilder = originalRequest.newBuilder()
5051 .header(" Cookie" , currentCookies)
52+ response = chain.proceed(requestBuilder.build())
5153
52- initialResponse = chain.proceed(requestBuilder.build())
53-
54- if (initialResponse.code == 403 || initialResponse.code == 503 ) {
54+ if (response.code == 403 || response.code == 503 ) {
5555 needsRefresh = true
56- initialResponse.close()
57- } else {
58- return initialResponse
56+ response.close()
5957 }
60- } else {
61- needsRefresh = true
6258 }
6359
6460 if (needsRefresh) {
65- val context = AcraApplication .context
66- if (context != null ) {
67- val handler = Handler (Looper .getMainLooper())
68- var webView: WebView ? = null
69-
70- handler.post {
71- val newWebView = WebView (context)
72- webView = newWebView
73-
74- newWebView.resumeTimers()
75- newWebView.onResume()
76- newWebView.layoutParams = android.view.ViewGroup .LayoutParams (1080 , 1920 )
77-
78- cookieManager.setAcceptCookie(true )
79- cookieManager.setAcceptThirdPartyCookies(newWebView, true )
80-
81- newWebView.settings.apply {
82- javaScriptEnabled = true
83- domStorageEnabled = true
84- databaseEnabled = true
85- loadWithOverviewMode = true
86- useWideViewPort = true
87- mediaPlaybackRequiresUserGesture = false
88- javaScriptCanOpenWindowsAutomatically = true
89- mixedContentMode = android.webkit.WebSettings .MIXED_CONTENT_ALWAYS_ALLOW
90- var customUa = userAgent.ifBlank { userAgentString }
91- if (customUa.contains(" ; wv" )) {
92- customUa = customUa.replace(" ; wv" , " " )
93- }
94- userAgentString = customUa
95- }
96- userAgent = newWebView.settings.userAgentString ? : userAgent
97-
98- newWebView.webViewClient = object : WebViewClient () {
61+ runBlocking(Dispatchers .Main ) {
62+ val context = AcraApplication .context
63+ if (context != null ) {
64+ val webView = WebView (context)
65+
66+ webView.settings.javaScriptEnabled = true
67+ webView.settings.domStorageEnabled = true
68+ webView.settings.userAgentString = userAgent.ifBlank { webView.settings.userAgentString }
69+ userAgent = webView.settings.userAgentString
70+
71+ webView.webViewClient = object : WebViewClient () {
9972 override fun onPageFinished (view : WebView ? , url : String? ) {
10073 super .onPageFinished(view, url)
101- cookieManager.flush()
10274 }
10375 }
10476
105- cookieManager.removeAllCookies(null )
106- cookieManager.flush()
77+ cookieManager.setCookie(url, " $targetCookie =; Max-Age=0" )
10778
108- newWebView.loadUrl(url)
109- }
79+ webView.loadUrl(url)
11080
111- var attempts = 0
112- val maxAttempts = 60
113- while (attempts < maxAttempts) {
114- Thread .sleep(1000 )
115- val checkCookies = cookieManager.getCookie(domainUrl) ? : " "
116- if (checkCookies.contains(targetCookie)) {
117- cookieManager.flush()
118- break
81+ var attempts = 0
82+ val maxAttempts = 60
83+ while (attempts < maxAttempts) {
84+ delay(1000 )
85+ val checkCookies = cookieManager.getCookie(url) ? : " "
86+ if (checkCookies.contains(targetCookie)) {
87+ break
88+ }
89+ attempts++
11990 }
120- attempts++
121- }
12291
123- handler.post {
124- webView?.stopLoading()
125- webView?.destroy()
126- webView = null
92+ webView.stopLoading()
93+ webView.destroy()
12794 }
12895 }
12996
130- currentCookies = cookieManager.getCookie(domainUrl ) ? : " "
97+ currentCookies = cookieManager.getCookie(url ) ? : " "
13198 val newRequestBuilder = originalRequest.newBuilder()
13299 .header(" User-Agent" , userAgent)
133100 .header(" Cookie" , currentCookies)
134-
135- return chain.proceed(newRequestBuilder.build())
101+
102+ response = chain.proceed(newRequestBuilder.build())
136103 }
137104
138- return initialResponse ? : chain.proceed(originalRequest)
105+ return response ? : chain.proceed(originalRequest)
139106 }
140107}
141108
@@ -172,12 +139,10 @@ class AnimeSailProvider : MainAPI() {
172139 }
173140 }
174141
175- private val turnstileInterceptor = TurnstileInterceptor (" _as_turnstile" )
176-
177142 private suspend fun request (url : String , ref : String? = null): NiceResponse {
178143 return app.get(
179144 url,
180- interceptor = turnstileInterceptor ,
145+ interceptor = TurnstileInterceptor ( " _as_turnstile " ) ,
181146 headers = mapOf (
182147 " Accept" to " text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" ,
183148 " User-Agent" to " Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36" ,
0 commit comments