2016-12-07 18:34:39 +01:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2020-04-11 11:51:40 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-12-07 18:34:39 +01:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2020-01-29 19:36:57 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
2016-12-07 18:34:39 +01:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.webkit.CookieManager;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
|
2020-02-18 21:50:28 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2020-04-10 18:48:40 +02:00
|
|
|
import androidx.annotation.Nullable;
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.app.NavUtils;
|
2020-04-11 11:51:40 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2020-12-19 04:41:01 +05:30
|
|
|
import androidx.webkit.WebViewClientCompat;
|
2020-03-31 19:20:15 +02:00
|
|
|
|
2020-10-31 14:10:00 +05:30
|
|
|
import org.schabi.newpipe.databinding.ActivityRecaptchaBinding;
|
2020-03-31 19:20:15 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2020-01-29 19:36:57 +01:00
|
|
|
|
2020-04-10 18:48:40 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
/*
|
2016-12-07 18:34:39 +01:00
|
|
|
* Created by beneth <bmauduit@beneth.fr> on 06.12.16.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
|
|
|
|
* ReCaptchaActivity.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
public class ReCaptchaActivity extends AppCompatActivity {
|
2017-01-02 17:30:35 +01:00
|
|
|
public static final int RECAPTCHA_REQUEST = 10;
|
2019-08-17 09:30:42 +02:00
|
|
|
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";
|
2016-12-07 18:34:39 +01:00
|
|
|
public static final String TAG = ReCaptchaActivity.class.toString();
|
|
|
|
public static final String YT_URL = "https://www.youtube.com";
|
2020-03-15 16:53:29 -05:00
|
|
|
public static final String RECAPTCHA_COOKIES_KEY = "recaptcha_cookies";
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-12-17 16:32:03 +01:00
|
|
|
public static String sanitizeRecaptchaUrl(@Nullable final String url) {
|
|
|
|
if (url == null || url.trim().isEmpty()) {
|
|
|
|
return YT_URL; // YouTube is the most likely service to have thrown a recaptcha
|
|
|
|
} else {
|
|
|
|
// remove "pbj=1" parameter from YouYube urls, as it makes the page JSON and not HTML
|
|
|
|
return url.replace("&pbj=1", "").replace("pbj=1&", "").replace("?pbj=1", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:10:00 +05:30
|
|
|
private ActivityRecaptchaBinding recaptchaBinding;
|
2020-01-29 19:36:57 +01:00
|
|
|
private String foundCookies = "";
|
2019-08-17 09:30:42 +02:00
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
2020-02-01 17:53:43 +01:00
|
|
|
ThemeHelper.setTheme(this);
|
2016-12-07 18:34:39 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-10-31 14:10:00 +05:30
|
|
|
|
|
|
|
recaptchaBinding = ActivityRecaptchaBinding.inflate(getLayoutInflater());
|
|
|
|
setContentView(recaptchaBinding.getRoot());
|
|
|
|
setSupportActionBar(recaptchaBinding.toolbar);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-12-17 16:32:03 +01:00
|
|
|
final String url = sanitizeRecaptchaUrl(getIntent().getStringExtra(RECAPTCHA_URL_EXTRA));
|
2020-02-02 21:48:45 +01:00
|
|
|
// set return to Cancel by default
|
2017-01-02 17:30:35 +01:00
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
|
2020-02-02 21:48:45 +01:00
|
|
|
// enable Javascript
|
2020-10-31 14:10:00 +05:30
|
|
|
final WebSettings webSettings = recaptchaBinding.reCaptchaWebView.getSettings();
|
2016-12-07 18:34:39 +01:00
|
|
|
webSettings.setJavaScriptEnabled(true);
|
2020-12-18 18:36:40 +01:00
|
|
|
webSettings.setUserAgentString(DownloaderImpl.USER_AGENT);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-12-19 04:41:01 +05:30
|
|
|
recaptchaBinding.reCaptchaWebView.setWebViewClient(new WebViewClientCompat() {
|
2020-04-10 18:48:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
|
|
|
|
if (MainActivity.DEBUG) {
|
|
|
|
Log.d(TAG, "shouldOverrideUrlLoading: url=" + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCookiesFromUrl(url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onPageFinished(final WebView view, final String url) {
|
2020-01-29 19:36:57 +01:00
|
|
|
super.onPageFinished(view, url);
|
2020-04-10 18:48:40 +02:00
|
|
|
handleCookiesFromUrl(url);
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
|
|
|
});
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-02-02 21:48:45 +01:00
|
|
|
// cleaning cache, history and cookies from webView
|
2020-10-31 14:10:00 +05:30
|
|
|
recaptchaBinding.reCaptchaWebView.clearCache(true);
|
|
|
|
recaptchaBinding.reCaptchaWebView.clearHistory();
|
|
|
|
final CookieManager cookieManager = CookieManager.getInstance();
|
2016-12-07 18:34:39 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2020-12-18 18:36:40 +01:00
|
|
|
cookieManager.removeAllCookies(value -> { });
|
2016-12-07 18:34:39 +01:00
|
|
|
} else {
|
|
|
|
cookieManager.removeAllCookie();
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:10:00 +05:30
|
|
|
recaptchaBinding.reCaptchaWebView.loadUrl(url);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public boolean onCreateOptionsMenu(final Menu menu) {
|
2020-02-01 17:53:43 +01:00
|
|
|
getMenuInflater().inflate(R.menu.menu_recaptcha, menu);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-08-16 10:24:58 +02:00
|
|
|
final ActionBar actionBar = getSupportActionBar();
|
2020-01-29 19:36:57 +01:00
|
|
|
if (actionBar != null) {
|
2020-02-01 17:53:43 +01:00
|
|
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
2020-01-29 19:36:57 +01:00
|
|
|
actionBar.setTitle(R.string.title_activity_recaptcha);
|
|
|
|
actionBar.setSubtitle(R.string.subtitle_activity_recaptcha);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:53:43 +01:00
|
|
|
return true;
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
saveCookiesAndFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
2020-12-18 18:36:40 +01:00
|
|
|
if (item.getItemId() == R.id.menu_item_done) {
|
|
|
|
saveCookiesAndFinish();
|
|
|
|
return true;
|
2017-01-02 17:30:35 +01:00
|
|
|
}
|
2020-12-18 18:36:40 +01:00
|
|
|
return false;
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
2017-01-02 17:30:35 +01:00
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
private void saveCookiesAndFinish() {
|
2020-10-31 14:10:00 +05:30
|
|
|
// try to get cookies of unclosed page
|
|
|
|
handleCookiesFromUrl(recaptchaBinding.reCaptchaWebView.getUrl());
|
2020-04-10 18:48:40 +02:00
|
|
|
if (MainActivity.DEBUG) {
|
|
|
|
Log.d(TAG, "saveCookiesAndFinish: foundCookies=" + foundCookies);
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
if (!foundCookies.isEmpty()) {
|
2020-04-11 11:51:40 +02:00
|
|
|
// save cookies to preferences
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getApplicationContext());
|
|
|
|
final String key = getApplicationContext().getString(R.string.recaptcha_cookies_key);
|
|
|
|
prefs.edit().putString(key, foundCookies).apply();
|
|
|
|
|
2020-02-02 21:48:45 +01:00
|
|
|
// give cookies to Downloader class
|
2020-03-15 16:53:29 -05:00
|
|
|
DownloaderImpl.getInstance().setCookie(RECAPTCHA_COOKIES_KEY, foundCookies);
|
2020-01-29 19:36:57 +01:00
|
|
|
setResult(RESULT_OK);
|
|
|
|
}
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-08-16 10:24:58 +02:00
|
|
|
final Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
|
2020-01-29 19:36:57 +01:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
NavUtils.navigateUpTo(this, intent);
|
|
|
|
}
|
2017-01-02 17:30:35 +01:00
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-04-11 11:51:40 +02:00
|
|
|
private void handleCookiesFromUrl(@Nullable final String url) {
|
2020-04-10 18:48:40 +02:00
|
|
|
if (MainActivity.DEBUG) {
|
|
|
|
Log.d(TAG, "handleCookiesFromUrl: url=" + (url == null ? "null" : url));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-16 10:24:58 +02:00
|
|
|
final String cookies = CookieManager.getInstance().getCookie(url);
|
2020-04-10 18:48:40 +02:00
|
|
|
handleCookies(cookies);
|
|
|
|
|
|
|
|
// sometimes cookies are inside the url
|
2020-08-16 10:24:58 +02:00
|
|
|
final int abuseStart = url.indexOf("google_abuse=");
|
2020-04-10 18:48:40 +02:00
|
|
|
if (abuseStart != -1) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final int abuseEnd = url.indexOf("+path");
|
2020-04-10 18:48:40 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:51:40 +02:00
|
|
|
private void handleCookies(@Nullable final String cookies) {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (MainActivity.DEBUG) {
|
2020-04-10 18:48:40 +02:00
|
|
|
Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies));
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|
2020-04-10 18:48:40 +02:00
|
|
|
|
2020-04-11 11:50:56 +02:00
|
|
|
if (cookies == null) {
|
2020-03-31 19:20:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-01-29 19:36:57 +01:00
|
|
|
|
|
|
|
addYoutubeCookies(cookies);
|
2020-04-10 18:48:40 +02:00
|
|
|
// add here methods to extract cookies for other services
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 11:51:40 +02:00
|
|
|
private void addYoutubeCookies(@NonNull final String cookies) {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (cookies.contains("s_gl=") || cookies.contains("goojf=")
|
2020-04-10 18:48:40 +02:00
|
|
|
|| cookies.contains("VISITOR_INFO1_LIVE=")
|
|
|
|
|| cookies.contains("GOOGLE_ABUSE_EXEMPTION=")) {
|
2020-02-02 21:48:45 +01:00
|
|
|
// youtube seems to also need the other cookies:
|
2020-01-29 19:36:57 +01:00
|
|
|
addCookie(cookies);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
private void addCookie(final String cookie) {
|
2020-02-02 21:33:07 +01:00
|
|
|
if (foundCookies.contains(cookie)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
if (foundCookies.isEmpty() || foundCookies.endsWith("; ")) {
|
|
|
|
foundCookies += cookie;
|
|
|
|
} else if (foundCookies.endsWith(";")) {
|
|
|
|
foundCookies += " " + cookie;
|
|
|
|
} else {
|
|
|
|
foundCookies += "; " + cookie;
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|