-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix ReCaptcha Activity for another type of recaptcha page #3414
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,24 +1,32 @@ | ||
| package org.schabi.newpipe; | ||
|
|
||
| import android.content.Intent; | ||
| import android.content.SharedPreferences; | ||
| import android.os.Build; | ||
| import android.os.Bundle; | ||
| 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; | ||
| import androidx.core.app.NavUtils; | ||
| import androidx.preference.PreferenceManager; | ||
|
|
||
| import org.schabi.newpipe.util.ThemeHelper; | ||
|
|
||
| import java.io.UnsupportedEncodingException; | ||
| import java.net.URLDecoder; | ||
|
|
||
| /* | ||
| * Created by beneth <[email protected]> on 06.12.16. | ||
| * | ||
|
|
@@ -71,10 +79,33 @@ protected void onCreate(final Bundle savedInstanceState) { | |
| 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,8 +155,18 @@ public boolean onOptionsItemSelected(final MenuItem item) { | |
| } | ||
|
|
||
| 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()) { | ||
| // 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(); | ||
|
|
||
| // give cookies to Downloader class | ||
| DownloaderImpl.getInstance().setCookies(foundCookies); | ||
| setResult(RESULT_OK); | ||
|
|
@@ -137,23 +178,54 @@ private void saveCookiesAndFinish() { | |
| } | ||
|
|
||
|
|
||
| private void handleCookies(final String url) { | ||
| private void handleCookiesFromUrl(@Nullable final String url) { | ||
| if (MainActivity.DEBUG) { | ||
| Log.d(TAG, "handleCookiesFromUrl: url=" + (url == null ? "null" : url)); | ||
| } | ||
|
|
||
| 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(@Nullable final String cookies) { | ||
| if (MainActivity.DEBUG) { | ||
| Log.d(TAG, "handleCookies: " | ||
| + "url=" + url + "; cookies=" + (cookies == null ? "null" : cookies)); | ||
| Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies)); | ||
| } | ||
|
|
||
| if (cookies == null) { | ||
| 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) { | ||
| 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); | ||
| } | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that information useful to find further cookies? We might want to show a snackbar to allow reporting the error and more importantly, the url
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This piece of code has never been useful in my testing, since it just obtains a cookie that would anyway appear as a cookie in one of the subsequent requests. Though to prevent issues under different environment where the webview behaves strangely (who knows?), it doesn't hurt to obtain the cookie twice in a different way and call it a day.
I left a Log there just so that when I send people an apk to debug, I don't have to make a custom one that throws a random Exception with the log message inside (as I did with the other cookie testing apk), but I can just tell them to install a logcat viewer and I have the information needed for the whole execution of the activity.
Anyway, if the catch block is ever reached it means the url isn't useful to obtain a cookie, so there is no need to report anything to a normal user imho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However logcat apps need root access since Jelly Bean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, do they? I didn't know ;-)
But what could I implement then, that would log everything and display it to the user after he presses "Ok" on a snackbar? Should I do some kind of bug report, but with logs instead of stack trace?