Improve login check for Chaturbate

This commit is contained in:
0xb00bface 2023-11-05 15:36:56 +01:00
parent c376f30c56
commit a08d6ec8c6
1 changed files with 32 additions and 25 deletions

View File

@ -3,9 +3,8 @@ package ctbrec.sites.chaturbate;
import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InterruptedIOException;
@ -15,9 +14,9 @@ import java.util.concurrent.Semaphore;
import static ctbrec.io.HttpConstants.REFERER;
import static ctbrec.io.HttpConstants.USER_AGENT;
@Slf4j
public class ChaturbateHttpClient extends HttpClient {
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateHttpClient.class);
private static final String PATH = "/auth/login/"; // NOSONAR
protected String token;
@ -33,7 +32,7 @@ public class ChaturbateHttpClient extends HttpClient {
Cookie csrfToken = cookieJar.getCookie(request.url(), "csrftoken");
token = csrfToken.value();
} catch (NoSuchElementException e) {
LOG.trace("CSRF token not found in cookies");
log.trace("CSRF token not found in cookies");
}
}
@ -51,18 +50,17 @@ public class ChaturbateHttpClient extends HttpClient {
}
if (checkLogin()) {
loggedIn = true;
LOG.debug("Logged in with cookies");
log.debug("Logged in with cookies");
return true;
}
try {
Request login = new Request.Builder()
.url(Chaturbate.baseUrl + PATH)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
Response response = client.newCall(login).execute();
String content = response.body().string();
Request login = new Request.Builder()
.url(Chaturbate.baseUrl + PATH)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (var initResponse = client.newCall(login).execute()) {
String content = initResponse.body().string();
token = HtmlParser.getTag(content, "input[name=csrfmiddlewaretoken]").attr("value");
LOG.debug("csrf token is {}", token);
log.debug("csrf token is {}", token);
RequestBody body = new FormBody.Builder()
.add("username", Config.getInstance().getSettings().chaturbateUsername)
@ -77,30 +75,39 @@ public class ChaturbateHttpClient extends HttpClient {
.post(body)
.build();
response = client.newCall(login).execute();
if (response.isSuccessful()) {
content = response.body().string();
if (content.contains("Login, Chaturbate login")) {
loggedIn = false;
} else {
loggedIn = true;
extractCsrfToken(login);
try (var loginResponse = client.newCall(login).execute()) {
if (loginResponse.isSuccessful()) {
content = loginResponse.body().string();
if (content.contains("Login, Chaturbate login")) {
loggedIn = false;
} else {
loggedIn = true;
extractCsrfToken(login);
}
}
}
} catch (Exception ex) {
LOG.debug("Login failed: {}", ex.getMessage());
log.debug("Login failed: {}", ex.getMessage());
}
return loggedIn;
}
public boolean checkLogin() throws IOException {
public boolean checkLogin() {
String url = "https://chaturbate.com/api/ts/chatmessages/pm_users/?offset=0";
Request req = new Request.Builder()
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response resp = execute(req)) {
return (resp.isSuccessful() && !resp.isRedirect());
try (Response response = execute(req)) {
boolean result = false;
if (response.isSuccessful()) {
String content = response.body().string();
if (content.startsWith("[")) {
result = true;
}
}
log.trace("Chaturbate client login result: {}, {}", result, response.body().string());
return result;
} catch (Exception ex) {
return false;
}