Fix recaptcha activity for another type of recaptcha page

Try to get cookies from pages even when they start loading, because non-html pages like ones with `pbj=1` never stop loading.
Try to extract the cookie directly from the redirection url, by looking at the field "google_abuse=".
Add `GOOGLE_ABUSE_EXEMPTION=` to the youtube recaptcha cookies.
This commit is contained in:
Stypox 2020-04-10 18:48:40 +02:00
parent d7d45fb8e2
commit e2cbf40957
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 74 additions and 10 deletions

View File

@ -7,11 +7,14 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
@ -19,6 +22,9 @@ import androidx.core.app.NavUtils;
import org.schabi.newpipe.util.ThemeHelper;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/*
* Created by beneth <bmauduit@beneth.fr> on 06.12.16.
*
@ -71,10 +77,33 @@ public class ReCaptchaActivity extends AppCompatActivity {
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(final WebView view,
final WebResourceRequest request) {
String url = request.getUrl().toString();
if (MainActivity.DEBUG) {
Log.d(TAG, "shouldOverrideUrlLoading: request.url=" + url);
}
handleCookiesFromUrl(url);
return false;
}
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if (MainActivity.DEBUG) {
Log.d(TAG, "shouldOverrideUrlLoading: url=" + url);
}
handleCookiesFromUrl(url);
return false;
}
@Override
public void onPageFinished(final WebView view, final String url) {
super.onPageFinished(view, url);
handleCookies(url);
handleCookiesFromUrl(url);
}
});
@ -124,7 +153,11 @@ public class ReCaptchaActivity extends AppCompatActivity {
}
private void saveCookiesAndFinish() {
handleCookies(webView.getUrl()); // try to get cookies of unclosed page
handleCookiesFromUrl(webView.getUrl()); // try to get cookies of unclosed page
if (MainActivity.DEBUG) {
Log.d(TAG, "saveCookiesAndFinish: foundCookies=" + foundCookies);
}
if (!foundCookies.isEmpty()) {
// give cookies to Downloader class
DownloaderImpl.getInstance().setCookies(foundCookies);
@ -137,23 +170,54 @@ public class ReCaptchaActivity extends AppCompatActivity {
}
private void handleCookies(final String url) {
String cookies = CookieManager.getInstance().getCookie(url);
private void handleCookiesFromUrl(final @Nullable String url) {
if (MainActivity.DEBUG) {
Log.d(TAG, "handleCookies: "
+ "url=" + url + "; cookies=" + (cookies == null ? "null" : cookies));
Log.d(TAG, "handleCookiesFromUrl: url=" + (url == null ? "null" : url));
}
if (cookies == null) {
if (url == null) {
return;
}
String cookies = CookieManager.getInstance().getCookie(url);
handleCookies(cookies);
// sometimes cookies are inside the url
int abuseStart = url.indexOf("google_abuse=");
if (abuseStart != -1) {
int abuseEnd = url.indexOf("+path");
try {
String abuseCookie = url.substring(abuseStart + 13, abuseEnd);
abuseCookie = URLDecoder.decode(abuseCookie, "UTF-8");
handleCookies(abuseCookie);
} catch (UnsupportedEncodingException | StringIndexOutOfBoundsException e) {
if (MainActivity.DEBUG) {
e.printStackTrace();
Log.d(TAG, "handleCookiesFromUrl: invalid google abuse starting at "
+ abuseStart + " and ending at " + abuseEnd + " for url " + url);
}
}
}
}
private void handleCookies(final @Nullable String cookies) {
if (MainActivity.DEBUG) {
Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies));
}
if (cookies == null || foundCookies.contains(cookies)) {
return;
}
addYoutubeCookies(cookies);
// add other methods to extract cookies here
// add here methods to extract cookies for other services
}
private void addYoutubeCookies(@NonNull final String cookies) {
private void addYoutubeCookies(final @NonNull String cookies) {
if (cookies.contains("s_gl=") || cookies.contains("goojf=")
|| cookies.contains("VISITOR_INFO1_LIVE=")) {
|| cookies.contains("VISITOR_INFO1_LIVE=")
|| cookies.contains("GOOGLE_ABUSE_EXEMPTION=")) {
// youtube seems to also need the other cookies:
addCookie(cookies);
}