forked from j62/ctbrec
1
0
Fork 0

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