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-captcha' into leftypol-release-tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroCarrara committed Jun 6, 2021
2 parents b3bd5d7 + 6027660 commit 1ce0183
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import com.github.adamantcheese.chan.utils.StringUtils;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -218,11 +220,41 @@ public void onSubmitClicked(boolean longClicked) {
return;
}

submitOrAuthenticate(authenticateOnly);
checkForAuth(authenticateOnly);
}

private void submitOrAuthenticate(boolean authenticateOnly) {
if (loadable.site.actions().postRequiresAuthentication()) {
private void checkForAuth(boolean authenticateOnly) {
final Future<Boolean> res = loadable.site.actions().postRequiresAuthentication();
if (res.isDone()) {
try {
this.submitOrAuthenticate(authenticateOnly, res.get());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
switchPage(Page.LOADING);
BackgroundUtils.runOnBackgroundThread(() -> {
Logger.d(this, "Background!");
try {
final boolean auth = res.get();
Logger.d(this, "Background: " + auth);
BackgroundUtils.runOnMainThread(() -> {
Logger.d(this, "Main: " + auth);
this.submitOrAuthenticate(authenticateOnly, auth);
});
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}

private void submitOrAuthenticate(boolean authenticateOnly, boolean requiresAuthentication) {
if (requiresAuthentication) {
switchPage(Page.AUTHENTICATION, true, !authenticateOnly);
} else {
makeSubmitCall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.github.adamantcheese.chan.core.site.parser.ChanReaderProcessingQueue;
import com.github.adamantcheese.chan.core.site.parser.CommentParser;
import com.github.adamantcheese.chan.core.site.parser.PostParser;
import com.github.adamantcheese.chan.utils.CompletableFuture;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import okhttp3.HttpUrl;
import okhttp3.Request;
Expand Down Expand Up @@ -224,8 +226,8 @@ public void pages(Board board, PagesListener pagesListener) {}
public void post(Loadable loadableWithDraft, PostListener postListener) {}

@Override
public boolean postRequiresAuthentication() {
return false;
public Future<Boolean> postRequiresAuthentication() {
return new CompletableFuture<>(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.github.adamantcheese.chan.core.site.http.LoginResponse;
import com.github.adamantcheese.chan.core.site.http.ReplyResponse;

import java.util.concurrent.Future;

public interface SiteActions {
void boards(BoardsListener boardsListener);

Expand All @@ -50,7 +52,7 @@ interface PostListener {
void onPostError(Exception exception);
}

boolean postRequiresAuthentication();
Future<Boolean> postRequiresAuthentication();

/**
* If {@link ReplyResponse#requireAuthentication} was {@code true}, or if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.CAPTCHA1;
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.CAPTCHA2;
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.CAPTCHA2_NOJS;
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.SECURIMAGE;
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.GENERIC_WEBVIEW;
import static com.github.adamantcheese.chan.core.site.SiteAuthentication.Type.NONE;

Expand All @@ -29,6 +30,7 @@ public enum Type {
CAPTCHA1,
CAPTCHA2,
CAPTCHA2_NOJS,
SECURIMAGE, // Simple PHP captcha displaying a image to text challenge
GENERIC_WEBVIEW
}

Expand Down Expand Up @@ -57,6 +59,17 @@ public static SiteAuthentication fromCaptcha2nojs(String siteKey, String baseUrl
return a;
}

/**
* Create a new authentication using Securimage's captcha
* @param captchaUrl The path to the securimage script. Usually something like "https://website.com/captcha.php"
* @return New authentication using Securimage's captcha
*/
public static SiteAuthentication fromSecurimage(String captchaUrl) {
SiteAuthentication a = new SiteAuthentication(SECURIMAGE);
a.baseUrl = captchaUrl;
return a;
}

public static SiteAuthentication fromUrl(String url, String retryText, String successText) {
SiteAuthentication a = new SiteAuthentication(GENERIC_WEBVIEW);
a.url = url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import com.github.adamantcheese.chan.core.site.parser.CommentParser.ResolveLink;
import com.github.adamantcheese.chan.core.site.parser.CommentParser.ThreadLink;
import com.github.adamantcheese.chan.core.site.parser.PostParser;
import com.github.adamantcheese.chan.utils.CompletableFuture;
import com.github.adamantcheese.chan.utils.JavaUtils.NoDeleteArrayList;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import kotlin.random.Random;
import okhttp3.HttpUrl;
Expand All @@ -47,7 +49,7 @@ public ExternalSiteArchive(
this.name = name;
this.boardCodes = new NoDeleteArrayList<>(boardCodes);
this.searchEnabled = searchEnabled;

id = Random.Default.nextInt(Integer.MIN_VALUE / 2, -1);
}

Expand Down Expand Up @@ -136,7 +138,7 @@ public void pages(Board board, PagesListener pagesListener) {}
public void post(Loadable loadableWithDraft, PostListener postListener) {}

@Override
public boolean postRequiresAuthentication() { return false; }
public Future<Boolean> postRequiresAuthentication() { return new CompletableFuture(false); }

@Override
public SiteAuthentication postAuthenticate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
import com.github.adamantcheese.chan.core.site.parser.PostParser;
import com.github.adamantcheese.chan.utils.BackgroundUtils;
import com.github.adamantcheese.chan.core.net.NetUtils;
import com.github.adamantcheese.chan.utils.CompletableFuture;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -478,8 +480,8 @@ public void handlePost(ReplyResponse response, Response httpResponse, String res
}

@Override
public boolean postRequiresAuthentication() {
return false;
public Future<Boolean> postRequiresAuthentication() {
return new CompletableFuture(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.github.adamantcheese.chan.core.site.http.LoginResponse;
import com.github.adamantcheese.chan.core.site.parser.ChanReader;
import com.github.adamantcheese.chan.utils.BackgroundUtils;
import com.github.adamantcheese.chan.utils.CompletableFuture;
import com.github.adamantcheese.chan.utils.Logger;
import com.github.adamantcheese.chan.core.net.NetUtils;
import com.github.adamantcheese.chan.core.net.NetUtilsClasses.HTMLProcessor;
Expand All @@ -64,6 +65,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Future;

import kotlin.random.Random;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -377,8 +379,8 @@ public void onHttpFail(CommonReplyHttpCall httpPost, Exception e) {
}

@Override
public boolean postRequiresAuthentication() {
return !isLoggedIn();
public Future<Boolean> postRequiresAuthentication() {
return new CompletableFuture<>(!isLoggedIn());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.github.adamantcheese.chan.core.site.http.DeleteRequest;
import com.github.adamantcheese.chan.core.site.http.HttpCall.HttpCallback;
import com.github.adamantcheese.chan.core.site.parser.CommentParser;
import com.github.adamantcheese.chan.utils.CompletableFuture;
import com.github.adamantcheese.chan.utils.Logger;
import com.github.adamantcheese.chan.core.net.NetUtils;
import com.github.adamantcheese.chan.core.net.NetUtilsClasses.ResponseResult;
Expand All @@ -26,6 +27,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import okhttp3.HttpUrl;

Expand Down Expand Up @@ -170,8 +172,8 @@ public void onHttpFail(CommonReplyHttpCall httpPost, Exception e) {
}

@Override
public boolean postRequiresAuthentication() {
return !isLoggedIn();
public Future<Boolean> postRequiresAuthentication() {
return new CompletableFuture<>(!isLoggedIn());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package com.github.adamantcheese.chan.core.site.sites.leftypol;

import com.github.adamantcheese.chan.Chan;
import com.github.adamantcheese.chan.core.di.NetModule;
import com.github.adamantcheese.chan.core.model.orm.Loadable;
import com.github.adamantcheese.chan.core.net.NetUtils;
import com.github.adamantcheese.chan.core.site.SiteAuthentication;
import com.github.adamantcheese.chan.core.site.common.CommonSite;
import com.github.adamantcheese.chan.core.site.common.MultipartHttpCall;
import com.github.adamantcheese.chan.core.site.common.vichan.VichanActions;
import com.github.adamantcheese.chan.core.site.http.HttpCall;
import com.github.adamantcheese.chan.core.site.http.ReplyResponse;
import com.github.adamantcheese.chan.utils.BackgroundUtils;
import com.github.adamantcheese.chan.utils.Logger;

import org.json.JSONObject;

import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LeftypolActions extends VichanActions {
Expand All @@ -26,6 +38,9 @@ public void setupPost(Loadable loadable, MultipartHttpCall call) {
super.setupPost(loadable, call);

call.parameter("user_flag", loadable.draft.flag);
if (loadable.draft.captchaResponse != "" && loadable.draft.captchaResponse != null) {
call.parameter("captcha", loadable.draft.captchaResponse);
}
}

@Override
Expand Down Expand Up @@ -55,6 +70,36 @@ public void process(Response response, String result) {
}
}

@Override
public Future<Boolean> postRequiresAuthentication() {
FutureTask<Boolean> future = new FutureTask<Boolean>(() -> {
// Build a request to "/status.php"
OkHttpClient.Builder cb = Chan.instance(NetModule.OkHttpClientWithUtils.class).newBuilder();
Request.Builder rb = new Request.Builder().url(this.rootUrl + "status.php");
Call call = cb.build().newCall(rb.build());

// Send the request, check if the captcha is enabled
Response r = call.execute();
if (r.isSuccessful()) {
JSONObject json = new JSONObject(r.body().string());
r.body().close();

return json.getBoolean("captcha");
} else {
Logger.e(this, "request to /status.php not successful");
r.body().close();
return false;
}
});
BackgroundUtils.runOnBackgroundThread(() -> future.run());
return future;
}

@Override
public SiteAuthentication postAuthenticate() {
return SiteAuthentication.fromSecurimage(this.rootUrl + "captcha.php");
}

private void makePostCall(HttpCall call, ReplyResponse replyResponse, PostListener postListener) {
NetUtils.makeHttpCall(call, new HttpCall.HttpCallback<HttpCall>() {
@Override
Expand Down
Loading

0 comments on commit 1ce0183

Please sign in to comment.