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

Commit

Permalink
Use webkit cookies jar instead of a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroCarrara committed Jan 22, 2021
1 parent b45b1ac commit 8798212
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
2 changes: 0 additions & 2 deletions Kuroba/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ dependencies {
implementation 'com.github.K1rakishou:Fuck-Storage-Access-Framework:v1.0-alpha42'

//debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'

implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
}

// Download the current archives.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package com.github.adamantcheese.chan.core.di;

import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
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 @@ -35,19 +34,22 @@
import org.jetbrains.annotations.NotNull;

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

import javax.inject.Singleton;

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

import static com.github.adamantcheese.chan.core.di.AppModule.getCacheDir;
import static com.github.adamantcheese.chan.core.net.DnsSelector.Mode.IPV4_ONLY;
import static com.github.adamantcheese.chan.core.net.DnsSelector.Mode.SYSTEM;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getAppContext;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getApplicationLabel;
import static okhttp3.Protocol.HTTP_1_1;
import static okhttp3.Protocol.HTTP_2;
Expand Down Expand Up @@ -113,19 +115,50 @@ public OkHttpClientWithUtils provideProxiedOkHttpClient() {
public static class OkHttpClientWithUtils
extends OkHttpClient {

private CookieJar cookies = null;
private static CookieJar cookies = null;

public OkHttpClientWithUtils(Builder builder) {
super(builder);
}

private static List<Cookie> parseCookies(HttpUrl url, String allCookies) {
// CookieJar jar = NetModule.OkHttpClientWithUtils.getCookies();
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;
}

@NotNull
@Override
public Builder newBuilder() {
if (cookies == null) {
cookies = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(
getAppContext()
));
cookies = new CookieJar(){
@Override
public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> list) {
// Right now the only cookies that matter are the ones coming from
// the js challenge, which are handled by the ChallengeController.
// TODO: Implement
}

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

return parseCookies(httpUrl, cookies);
}
};
}

return super.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import android.webkit.WebView;

import com.github.adamantcheese.chan.controller.Controller;
import com.github.adamantcheese.chan.core.di.NetModule;
import com.github.adamantcheese.chan.core.model.orm.Loadable;

public class ChallengeController extends Controller {
protected Loadable behindChallenge;

private WebView web;

public ChallengeController(Context context, Loadable behindChallenge) {
super(context);
this.behindChallenge = behindChallenge;
Expand All @@ -18,10 +21,17 @@ public ChallengeController(Context context, Loadable behindChallenge) {
public void onCreate() {
super.onCreate();

WebView web = new WebView(context);

web = new WebView(context);
web.loadUrl(behindChallenge.desktopUrl());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setUserAgentString(NetModule.USER_AGENT);

this.view = web;
}

@Override
public void onDestroy() {
super.onDestroy();
this.web.destroy();
}
}

0 comments on commit 8798212

Please sign in to comment.