Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge branch 'leftypol-cloudflare-ddos' into leftypol-release-tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroCarrara committed Feb 11, 2021
2 parents bf2f70c + 68d05e1 commit 2371596
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.github.adamantcheese.chan.core.di;

import android.webkit.CookieManager;

import com.github.adamantcheese.chan.BuildConfig;
import com.github.adamantcheese.chan.core.cache.CacheHandler;
import com.github.adamantcheese.chan.core.cache.FileCacheV2;
Expand All @@ -29,13 +31,20 @@
import com.github.k1rakishou.fsaf.file.RawFile;

import org.codejargon.feather.Provides;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.inject.Singleton;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

Expand Down Expand Up @@ -108,7 +117,8 @@ public static class OkHttpClientWithUtils
extends OkHttpClient {

public OkHttpClientWithUtils(Builder builder) {
super(builder);
// Add shared cookies between calls and webviews
super(builder.cookieJar(genCookies()));
}

//This adds a proxy to the base client
Expand All @@ -120,5 +130,61 @@ public OkHttpClient getProxiedClient() {
public OkHttpClient getHttpRedirectClient() {
return newBuilder().addInterceptor(new HttpEquivRefreshInterceptor()).build();
}

@NotNull
@Override
public Builder newBuilder() {
return super.newBuilder().cookieJar(genCookies());
}

private static CookieJar genCookies() {
return new CookieJar(){
@Override
public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> list) {
String str = "";

Iterator<Cookie> it = list.iterator();

if (it.hasNext()) {
Cookie c = it.next();
str = c.name() + "=" + c.value();
}

while (it.hasNext()) {
Cookie c = it.next();
str += "; " + c.name() + "=" + c.value();
}

if (!str.equals("")) {
CookieManager.getInstance().setCookie(httpUrl.resolve("/").toString(), str);
}
}

@NotNull
@Override
public List<Cookie> loadForRequest(@NotNull HttpUrl httpUrl) {
String cookies = CookieManager.getInstance().getCookie(httpUrl.toString());
if (cookies == null) {
cookies = "";
}

return parseCookies(httpUrl.resolve("/"), cookies);
}
};
}

private static List<Cookie> parseCookies(HttpUrl url, String allCookies) {
ArrayList<Cookie> cookies = new ArrayList<>();

if (allCookies.length() <= 0) {
return cookies;
}

for (String cookie : allCookies.split(";")) {
cookies.add(Cookie.parse(url, cookie.trim()));
}

return cookies;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ public boolean isNotFound() {
return exception instanceof HttpCodeException && ((HttpCodeException) exception).isServerErrorNotFound();
}

public boolean isNotAvailable() {
return exception instanceof HttpCodeException && ((HttpCodeException) exception).code == 503;
}

public int getErrorMessage() {
//by default, a network error has occurred if the exception field is not null
int errorMessage = exception != null ? R.string.thread_load_failed_network : R.string.empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ public void showBoardAndSearch(Loadable catalogLoadable, String searchQuery) {
setBoard(catalogLoadable.board);
}

@Override
public void showChallenge(Loadable loadable) {
ChallengeController c = new ChallengeController(context, loadable);

if (doubleNavigationController != null) {
doubleNavigationController.pushController(c);
} else {
navigationController.pushController(c);
}
}

// Creates or updates the target ViewThreadController
// This controller can be in various places depending on the layout, so we dynamically search for it
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.adamantcheese.chan.ui.controller;

import android.content.Context;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.adamantcheese.chan.R;
import com.github.adamantcheese.chan.controller.Controller;
import com.github.adamantcheese.chan.core.di.NetModule;
import com.github.adamantcheese.chan.core.model.orm.Loadable;
import com.github.adamantcheese.chan.core.site.common.CommonSite;
import com.github.adamantcheese.chan.utils.AndroidUtils;

public class ChallengeController extends Controller {
protected Loadable behindChallenge;

private WebView web;

public ChallengeController(Context context, Loadable behindChallenge) {
super(context);
this.behindChallenge = behindChallenge;
}

@Override
public void onCreate() {
super.onCreate();
navigation.title = AndroidUtils.getString(R.string.challenge_screen);

web = new WebView(context);
web.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
});

CommonSite.CommonCallModifier siteCallModifier = behindChallenge.site.callModifier();
if (siteCallModifier != null) {
siteCallModifier.modifyWebView(web);
}

web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.getSettings().setUserAgentString(NetModule.USER_AGENT);
web.loadUrl(behindChallenge.desktopUrl());

this.view = web;
}

@Override
public void onDestroy() {
super.onDestroy();
this.web.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.github.adamantcheese.chan.ui.toolbar.ToolbarMenuSubItem;
import com.github.adamantcheese.chan.ui.view.FloatingMenu;
import com.github.adamantcheese.chan.utils.AndroidUtils;
import com.github.adamantcheese.chan.utils.Logger;
import com.github.k1rakishou.fsaf.FileManager;
import com.skydoves.balloon.ArrowConstraints;
import com.skydoves.balloon.ArrowOrientation;
Expand Down Expand Up @@ -226,6 +227,11 @@ public void onEvent(PinMessages.PinsChangedMessage message) {
setPinIconState(true);
}

@Override
public void showChallenge(Loadable loadable) {
// TODO: Implement
}

@Override
public void showThread(final Loadable threadLoadable) {
if (threadLoadable.site instanceof ExternalSiteArchive && !loadable.site.equals(threadLoadable.site)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.github.adamantcheese.chan.ui.view.ThumbnailView;
import com.github.adamantcheese.chan.utils.AndroidUtils;
import com.github.adamantcheese.chan.utils.BackgroundUtils;
import com.github.adamantcheese.chan.utils.Logger;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
Expand Down Expand Up @@ -260,6 +261,10 @@ public void postClicked(Post post) {
public void showError(ChanThreadLoader.ChanLoaderException error) {
String errorMessage = getString(error.getErrorMessage());

if (error.isNotAvailable()) {
callback.showChallenge(presenter.getLoadable());
}

if (visible == Visible.THREAD) {
threadListLayout.showError(errorMessage);
} else {
Expand Down Expand Up @@ -764,6 +769,8 @@ public void showHideOrRemoveWholeChainDialog(boolean hide, Post post, int thread
}

public interface ThreadLayoutCallback {
void showChallenge(Loadable loadableBehindChallenge);

void showThread(Loadable threadLoadable);

void showBoard(Loadable catalogLoadable);
Expand Down
2 changes: 2 additions & 0 deletions Kuroba/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ Enabled filters have priority from top to bottom in your list. Filter precedence

<string name="drawer_settings">Settings</string>

<string name="challenge_screen">Site Challenge</string>

<!-- Main settings -->
<string name="settings_screen">Settings</string>

Expand Down

0 comments on commit 2371596

Please sign in to comment.