From 4fcf61b9210cb42cff7f1d7e33c0e4eefa00ac9f Mon Sep 17 00:00:00 2001 From: reusedname <155286845+reusedname@users.noreply.github.com> Date: Wed, 4 Dec 2024 22:02:48 +0500 Subject: [PATCH] more cookie expiring conditions --- .../sites/chaturbate/ChaturbateHttpClient.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java index 463459ef..038c22b3 100644 --- a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java +++ b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateHttpClient.java @@ -188,13 +188,19 @@ public class ChaturbateHttpClient extends HttpClient { try { 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 - var needRefresh = Optional + var cookie = Optional .ofNullable(cookieJar.getCookies().get(req.url().topPrivateDomain())) - .flatMap(x -> cookieJar.getCookieFromCollection(x, "cf_clearance")) - .map(c -> Instant.ofEpochMilli(c.expiresAt()).isBefore(Instant.now())).orElse(true); - if (needRefresh) { + .flatMap(x -> cookieJar.getCookieFromCollection(x, "cf_clearance")); + + 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 + ).orElse(true); + + if (cookieExpired) { 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