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();
|
client.dispatcher().executorService().shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void persistCookies() {
|
protected void persistCookies() {
|
||||||
try {
|
try {
|
||||||
List<CookieContainer> containers = new ArrayList<>();
|
List<CookieContainer> containers = new ArrayList<>();
|
||||||
cookieJar.getCookies().forEach((domain, cookieList) -> {
|
cookieJar.getCookies().forEach((domain, cookieList) -> {
|
||||||
|
@ -231,7 +231,7 @@ public abstract class HttpClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadCookies() {
|
protected void loadCookies() {
|
||||||
try {
|
try {
|
||||||
File cookieFile = new File(config.getConfigDir(), "cookies-" + name + ".json");
|
File cookieFile = new File(config.getConfigDir(), "cookies-" + name + ".json");
|
||||||
if (!cookieFile.exists()) {
|
if (!cookieFile.exists()) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import java.io.InterruptedIOException;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
|
||||||
import static ctbrec.io.HttpConstants.REFERER;
|
import static ctbrec.io.HttpConstants.REFERER;
|
||||||
|
@ -30,6 +31,7 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
|
|
||||||
// a lock to prevent multiple requests from
|
// a lock to prevent multiple requests from
|
||||||
ReentrantReadWriteLock cookieRefreshLock = new ReentrantReadWriteLock();
|
ReentrantReadWriteLock cookieRefreshLock = new ReentrantReadWriteLock();
|
||||||
|
AtomicInteger cookieErrorCounter = new AtomicInteger(0);
|
||||||
|
|
||||||
public ChaturbateHttpClient(Config config) {
|
public ChaturbateHttpClient(Config config) {
|
||||||
super("chaturbate", config);
|
super("chaturbate", config);
|
||||||
|
@ -188,7 +190,6 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
try {
|
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
|
// 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
|
// maybe this can be done with some syncronization primitive, or maybe an expiresAt() check is enough
|
||||||
var cookie = Optional
|
var cookie = Optional
|
||||||
|
@ -197,10 +198,12 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
|
|
||||||
var cookieExpired = cookie.map(c ->
|
var cookieExpired = cookie.map(c ->
|
||||||
Instant.ofEpochMilli(c.expiresAt()).isBefore(Instant.now()) // by time
|
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);
|
).orElse(true);
|
||||||
|
|
||||||
if (cookieExpired) {
|
if (cookieExpired || cookieErrorCounter.incrementAndGet() >= 5) {
|
||||||
|
cookieErrorCounter.set(0);
|
||||||
|
|
||||||
var apiResponse = flaresolverr.getCookies(req.url().toString()).get();
|
var apiResponse = flaresolverr.getCookies(req.url().toString()).get();
|
||||||
if (apiResponse.getStatus().equals("ok")) {
|
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
|
// update user agent. It should be the same for all sites, assuming we use the same api address every time
|
||||||
|
@ -210,6 +213,7 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
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()));
|
log.debug("Cookies successfully refreshed with Flaresolverr in {}", Duration.between(apiResponse.getStartTimestamp(), apiResponse.getEndTimestamp()));
|
||||||
} else {
|
} else {
|
||||||
log.debug("Unsuccessful attempt to refresh cookies. Response from Flaresolverr: {}", apiResponse);
|
log.debug("Unsuccessful attempt to refresh cookies. Response from Flaresolverr: {}", apiResponse);
|
||||||
|
|
Loading…
Reference in New Issue