From 6fd3f97e4b6b9b09bf56e152a96f5d2762ce5837 Mon Sep 17 00:00:00 2001 From: reusedname <155286845+reusedname@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:12:16 +0500 Subject: [PATCH] refresh cookie on at error threshold --- common/src/main/java/ctbrec/io/HttpClient.java | 4 ++-- .../sites/chaturbate/ChaturbateHttpClient.java | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/ctbrec/io/HttpClient.java b/common/src/main/java/ctbrec/io/HttpClient.java index a198bb45..da70cc89 100644 --- a/common/src/main/java/ctbrec/io/HttpClient.java +++ b/common/src/main/java/ctbrec/io/HttpClient.java @@ -213,7 +213,7 @@ public abstract class HttpClient { client.dispatcher().executorService().shutdown(); } - private void persistCookies() { + protected void persistCookies() { try { List containers = new ArrayList<>(); cookieJar.getCookies().forEach((domain, cookieList) -> { @@ -231,7 +231,7 @@ public abstract class HttpClient { } } - private void loadCookies() { + protected void loadCookies() { try { File cookieFile = new File(config.getConfigDir(), "cookies-" + name + ".json"); if (!cookieFile.exists()) { diff --git a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java index 038c22b3..87d02625 100644 --- a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java +++ b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java @@ -13,6 +13,7 @@ import java.io.InterruptedIOException; import java.util.NoSuchElementException; import java.util.Optional; import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantReadWriteLock; import static ctbrec.io.HttpConstants.REFERER; @@ -30,6 +31,7 @@ public class ChaturbateHttpClient extends HttpClient { // a lock to prevent multiple requests from ReentrantReadWriteLock cookieRefreshLock = new ReentrantReadWriteLock(); + AtomicInteger cookieErrorCounter = new AtomicInteger(0); public ChaturbateHttpClient(Config config) { super("chaturbate", config); @@ -186,8 +188,7 @@ public class ChaturbateHttpClient extends HttpClient { log.debug("403 received from {}. Trying to refresh cookies with Flaresolverr", req.url().host()); try { - cookieRefreshLock.writeLock().lock(); - + cookieRefreshLock.writeLock().lock(); // we need to prevent repeated challenge requests from multiple threads, so we check if the clearance cookie needs updating // maybe this can be done with some syncronization primitive, or maybe an expiresAt() check is enough @@ -197,10 +198,12 @@ public class ChaturbateHttpClient extends HttpClient { var cookieExpired = cookie.map(c -> Instant.ofEpochMilli(c.expiresAt()).isBefore(Instant.now()) // by time - || req.headers("Set-Cookie").stream().anyMatch(headerCookie -> headerCookie.contains(c.value())) // we got 403 with current cookie present + || req.headers("Cookie").stream().anyMatch(headerCookie -> headerCookie.contains(c.value())) // we got 403 with current cookie present ).orElse(true); - if (cookieExpired) { + if (cookieExpired || cookieErrorCounter.incrementAndGet() >= 5) { + cookieErrorCounter.set(0); + var apiResponse = flaresolverr.getCookies(req.url().toString()).get(); if (apiResponse.getStatus().equals("ok")) { // update user agent. It should be the same for all sites, assuming we use the same api address every time @@ -209,7 +212,8 @@ public class ChaturbateHttpClient extends HttpClient { config.save(); } - cookieJar.saveFromResponse(req.url(), apiResponse.getCookies()); + cookieJar.saveFromResponse(req.url(), apiResponse.getCookies()); + persistCookies(); log.debug("Cookies successfully refreshed with Flaresolverr in {}", Duration.between(apiResponse.getStartTimestamp(), apiResponse.getEndTimestamp())); } else { log.debug("Unsuccessful attempt to refresh cookies. Response from Flaresolverr: {}", apiResponse);