Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReCaptcha activity and correctly save obtained cookies #3035

Merged
merged 13 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@

<activity
android:name=".ReCaptchaActivity"
android:label="@string/reCaptchaActivity"/>
android:label="@string/recaptcha"/>

<provider
android:name="androidx.core.content.FileProvider"
Expand Down
168 changes: 88 additions & 80 deletions app/src/main/java/org/schabi/newpipe/ReCaptchaActivity.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package org.schabi.newpipe;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.NavUtils;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.schabi.newpipe.util.ThemeHelper;

import javax.annotation.Nonnull;

/*
* Created by beneth <[email protected]> on 06.12.16.
*
Expand All @@ -37,126 +42,129 @@
public class ReCaptchaActivity extends AppCompatActivity {
public static final int RECAPTCHA_REQUEST = 10;
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";

public static final String TAG = ReCaptchaActivity.class.toString();
public static final String YT_URL = "https://www.youtube.com";

private String url;
private WebView webView;
private String foundCookies = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeHelper.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recaptcha);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
String url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
if (url == null || url.isEmpty()) {
url = YT_URL;
}


// Set return to Cancel by default
// set return to Cancel by default
setResult(RESULT_CANCELED);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.reCaptcha_title);
actionBar.setDisplayShowTitleEnabled(true);
}

WebView myWebView = findViewById(R.id.reCaptchaWebView);
webView = findViewById(R.id.reCaptchaWebView);

// Enable Javascript
WebSettings webSettings = myWebView.getSettings();
// enable Javascript
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

ReCaptchaWebViewClient webClient = new ReCaptchaWebViewClient(this);
myWebView.setWebViewClient(webClient);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
handleCookies(url);
}
});

// Cleaning cache, history and cookies from webView
myWebView.clearCache(true);
myWebView.clearHistory();
// cleaning cache, history and cookies from webView
webView.clearCache(true);
webView.clearHistory();
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(aBoolean -> {});
} else {
cookieManager.removeAllCookie();
}

myWebView.loadUrl(url);
webView.loadUrl(url);
}

private class ReCaptchaWebViewClient extends WebViewClient {
private final Activity context;
private String mCookies;

ReCaptchaWebViewClient(Activity ctx) {
context = ctx;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO: Start Loader
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
String cookies = CookieManager.getInstance().getCookie(url);

// TODO: Stop Loader

// find cookies : s_gl & goojf and Add cookies to Downloader
if (find_access_cookies(cookies)) {
// Give cookies to Downloader class
DownloaderImpl.getInstance().setCookies(mCookies);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_recaptcha, menu);

// Closing activity and return to parent
setResult(RESULT_OK);
finish();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setTitle(R.string.title_activity_recaptcha);
actionBar.setSubtitle(R.string.subtitle_activity_recaptcha);
}

private boolean find_access_cookies(String cookies) {
boolean ret = false;
String c_s_gl = "";
String c_goojf = "";

String[] parts = cookies.split("; ");
for (String part : parts) {
if (part.trim().startsWith("s_gl")) {
c_s_gl = part.trim();
}
if (part.trim().startsWith("goojf")) {
c_goojf = part.trim();
}
}
if (c_s_gl.length() > 0 && c_goojf.length() > 0) {
ret = true;
//mCookies = c_s_gl + "; " + c_goojf;
// Youtube seems to also need the other cookies:
mCookies = cookies;
}
return true;
}

return ret;
}
@Override
public void onBackPressed() {
saveCookiesAndFinish();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home: {
Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
case R.id.menu_item_done:
saveCookiesAndFinish();
return true;
}
default:
return false;
}
}

private void saveCookiesAndFinish() {
handleCookies(webView.getUrl()); // try to get cookies of unclosed page
if (!foundCookies.isEmpty()) {
// give cookies to Downloader class
DownloaderImpl.getInstance().setCookies(foundCookies);
setResult(RESULT_OK);
}

Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
}



private void handleCookies(String url) {
String cookies = CookieManager.getInstance().getCookie(url);
if (MainActivity.DEBUG) Log.d(TAG, "handleCookies: url=" + url + "; cookies=" + (cookies == null ? "null" : cookies));
if (cookies == null) return;

addYoutubeCookies(cookies);
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
// add other methods to extract cookies here
}

private void addYoutubeCookies(@Nonnull String cookies) {
if (cookies.contains("s_gl=") || cookies.contains("goojf=") || cookies.contains("VISITOR_INFO1_LIVE=")) {
// youtube seems to also need the other cookies:
addCookie(cookies);
}
}

private void addCookie(String cookie) {
if (foundCookies.contains(cookie)) {
return;
}

if (foundCookies.isEmpty() || foundCookies.endsWith("; ")) {
foundCookies += cookie;
} else if (foundCookies.endsWith(";")) {
foundCookies += " " + cookie;
} else {
foundCookies += "; " + cookie;
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_done_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_done_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
</vector>
4 changes: 1 addition & 3 deletions app/src/main/res/layout/activity_recaptcha.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextAppearance="@style/Toolbar.Title">

</androidx.appcompat.widget.Toolbar>
app:titleTextAppearance="@style/Toolbar.Title"/>

<WebView
android:id="@+id/reCaptchaWebView"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/menu_recaptcha.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_item_done"
android:title="@string/recaptcha_done_button"
android:icon="?attr/ic_done"
app:showAsAction="always"/>
</menu>
4 changes: 2 additions & 2 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
<string name="no_available_dir">يرجى تحديد مجلد التنزيل لاحقا في الإعدادات</string>
<string name="msg_popup_permission">هذا الإذن مطلوب
\nللفتح في وضع النافذة المنبثقة</string>
<string name="reCaptchaActivity">اختبار reCAPTCHA</string>
<string name="recaptcha">اختبار reCAPTCHA</string>
<string name="settings_file_charset_title">السماح بالرموز في أسماء الملفات</string>
<string name="settings_file_replacement_character_summary">يتم استبدال الرموز غير المسموح بها بهذه القيمة</string>
<string name="settings_file_replacement_character_title">استبدال الحرف</string>
Expand Down Expand Up @@ -235,7 +235,7 @@
<string name="play_queue_audio_settings">الإعدادات الصوتية</string>
<string name="start_here_on_main">تشغيل هنا</string>
<string name="start_here_on_popup">بدأ التشغيل في نافذة منبثقة جديدة</string>
<string name="reCaptcha_title">تحدي الكابتشا</string>
<string name="title_activity_recaptcha">تحدي الكابتشا</string>
<string name="hold_to_append">ضغط مطول للإدراج الى قائمة الانتظار</string>
<plurals name="views">
<item quantity="zero">%s بدون مشهادة</item>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-b+ast/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<string name="short_billion">Mil mill.</string>
<string name="msg_popup_permission">Precísase esti permisu
\np\'abrir nel mou ventanu</string>
<string name="reCaptchaActivity">reCAPTCHA</string>
<string name="reCaptcha_title">Retu de reCAPTCHA</string>
<string name="recaptcha">reCAPTCHA</string>
<string name="title_activity_recaptcha">Retu de reCAPTCHA</string>
<string name="recaptcha_request_toast">Solicitóse\'l retu de reCAPTCHA</string>
<string name="controls_background_title">En segundu planu</string>
<string name="controls_popup_title">Ventanu</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-b+zh+HANS+CN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<string name="msg_running_detail">点击了解详情</string>
<string name="msg_wait">请稍候…</string>
<string name="msg_copied">复制至剪贴板</string>
<string name="reCaptchaActivity">reCAPTCHA验证码</string>
<string name="recaptcha">reCAPTCHA验证码</string>
<string name="controls_popup_title">悬浮窗播放</string>
<string name="title_activity_about">关于NewPipe</string>
<string name="action_settings">设置</string>
Expand Down Expand Up @@ -207,7 +207,7 @@
<string name="no_available_dir">请稍后在设置中设定下载目录</string>
<string name="msg_popup_permission">用悬浮窗模式
\n需要此权限</string>
<string name="reCaptcha_title">reCAPTCHA验证</string>
<string name="title_activity_recaptcha">reCAPTCHA验证</string>
<string name="recaptcha_request_toast">请求的新的CAPTCHA验证</string>
<string name="popup_mode_share_menu_title">NewPipe 悬浮窗模式</string>
<string name="popup_playing_toast">在悬浮窗中播放</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-be/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@
<string name="msg_popup_permission">Гэтае разрозненне трэба для
\nпрайгравання ў акне</string>
<string name="one_item_deleted">1 элемент выдалены.</string>
<string name="reCaptchaActivity">reCAPTCHA</string>
<string name="reCaptcha_title">Запыт reCAPTCHA</string>
<string name="recaptcha">reCAPTCHA</string>
<string name="title_activity_recaptcha">Запыт reCAPTCHA</string>
<string name="recaptcha_request_toast">Запытаны ўвод reCAPTCHA</string>
<string name="settings_category_downloads_title">Загрузкі</string>
<string name="settings_file_charset_title">Дапушчальныя сімвалы назвы файлаў</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@
<string name="msg_popup_permission">Това разрешение се изисква за
\nвъзпроизвеждане в отделен прозорец</string>
<string name="one_item_deleted">1 елемент е изтрит.</string>
<string name="reCaptchaActivity">преКАПЧА</string>
<string name="reCaptcha_title">reCAPTCHA заявка</string>
<string name="recaptcha">преКАПЧА</string>
<string name="title_activity_recaptcha">reCAPTCHA заявка</string>
<string name="recaptcha_request_toast">Изисква се въвеждане на reCAPTCHA</string>
<string name="settings_category_downloads_title">Изтегляне</string>
<string name="charset_most_special_characters">Повечето специални символи</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-bn-rBD/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
<!-- Checksum types -->
<!-- <string name="md5" translatable="false">MD5</string> -->
<!-- <string name="sha1" translatable="false">SHA1</string> -->
<string name="reCaptchaActivity">রিক্যাপচা</string>
<string name="reCaptcha_title">reCAPTCHA চ্যালেঞ্জ</string>
<string name="recaptcha">রিক্যাপচা</string>
<string name="title_activity_recaptcha">reCAPTCHA চ্যালেঞ্জ</string>
<string name="recaptcha_request_toast">reCAPTCHA চ্যালেঞ্জ অনুরোধ করা হয়েছে</string>
<!-- End of GigaGet's Strings -->
<string name="info_labels">কি:\\nঅনুরোধ:\\nকন্টেন্ট ভাষা:\\nসার্ভিস:\\nসময়(GMT এ):\\nপ্যাকেজ:\\nসংস্করণ:\\nওএস সংস্করণ:\\nআইপি পরিসর:</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<string name="finish">D\'acord</string>
<string name="msg_name">Nom de fitxer</string>
<string name="msg_error">Error</string>
<string name="reCaptchaActivity">reCAPTCHA</string>
<string name="recaptcha">reCAPTCHA</string>
<string name="settings_category_downloads_title">Baixades</string>
<string name="action_settings">Paràmetres</string>
<string name="action_about">Quant a</string>
Expand Down Expand Up @@ -307,7 +307,7 @@
<string name="msg_running_detail">Feu un toc aquí per a més detalls</string>
<string name="no_available_dir">Defineix una carpeta de baixades més endavant als paràmetres</string>
<string name="msg_popup_permission">Es necessita aquest permís per a obrir el mode emergent</string>
<string name="reCaptcha_title">Camp reCAPTCHA</string>
<string name="title_activity_recaptcha">Camp reCAPTCHA</string>
<string name="recaptcha_request_toast">S\'ha sol·licitat l\'emplenament d\'un camp reCAPTCHA</string>
<string name="settings_file_replacement_character_summary">Se substituiran els caràcters no vàlids amb aquest valor</string>
<string name="settings_file_replacement_character_title">Caràcter de substitució</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-cmn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@
<string name="msg_popup_permission">在悬浮窗模式打开
\n需要此权限</string>
<string name="one_item_deleted">已删除一个项目。</string>
<string name="reCaptchaActivity">reCAPTCHA 验证</string>
<string name="reCaptcha_title">reCAPTCHA 验证</string>
<string name="recaptcha">reCAPTCHA 验证</string>
<string name="title_activity_recaptcha">reCAPTCHA 验证</string>
<string name="recaptcha_request_toast">需完成 reCAPTCHA 验证</string>
<string name="settings_category_downloads_title">下载</string>
<string name="settings_file_charset_title">文件名中允许的字符</string>
Expand Down
Loading