refresh cookie on at error threshold
This commit is contained in:
parent
4fcf61b921
commit
6fd3f97e4b
|
@ -213,7 +213,7 @@ public abstract class HttpClient {
|
|||
client.dispatcher().executorService().shutdown();
|
||||
}
|
||||
|
||||
private void persistCookies() {
|
||||
protected void persistCookies() {
|
||||
try {
|
||||
List<CookieContainer> 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()) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue