Prepare code to persist http cookies
Save and reload the cookies might help to avoid logins between sessions.
This commit is contained in:
parent
f15b57ce9a
commit
a136c9ccd2
|
@ -4,6 +4,8 @@ import java.util.ArrayList;
|
|||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
@ -78,5 +80,16 @@ public class CookieJarImpl implements CookieJar {
|
|||
return host;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Entry<String, List<Cookie>> entry : cookieStore.entrySet()) {
|
||||
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected Map<String, List<Cookie>> getCookies() {
|
||||
return cookieStore;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package ctbrec.io;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonReader;
|
||||
import com.squareup.moshi.JsonWriter;
|
||||
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.Cookie.Builder;
|
||||
|
||||
public class CookieJsonAdapter extends JsonAdapter<Cookie> {
|
||||
|
||||
@Override
|
||||
public Cookie fromJson(JsonReader reader) throws IOException {
|
||||
Builder builder = new Cookie.Builder();
|
||||
// domain
|
||||
reader.nextName();
|
||||
String domain = reader.nextString();
|
||||
builder.domain(domain);
|
||||
|
||||
// expiresAt
|
||||
reader.nextName();
|
||||
builder.expiresAt(reader.nextLong());
|
||||
|
||||
// host only
|
||||
reader.nextName();
|
||||
if(reader.nextBoolean()) {
|
||||
builder.hostOnlyDomain(domain);
|
||||
}
|
||||
|
||||
// http only
|
||||
reader.nextName();
|
||||
if(reader.nextBoolean()) {
|
||||
builder.httpOnly();
|
||||
}
|
||||
|
||||
// name
|
||||
reader.nextName();
|
||||
builder.name(reader.nextString());
|
||||
|
||||
// path
|
||||
reader.nextName();
|
||||
builder.path(reader.nextString());
|
||||
|
||||
// persistent
|
||||
reader.nextName();
|
||||
if(reader.nextBoolean()) {
|
||||
// noop
|
||||
}
|
||||
|
||||
// secure
|
||||
reader.nextName();
|
||||
if(reader.nextBoolean()) {
|
||||
builder.secure();
|
||||
}
|
||||
|
||||
// value
|
||||
reader.nextName();
|
||||
builder.value(reader.nextString());
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, Cookie cookie) throws IOException {
|
||||
writer.beginObject();
|
||||
writer.name("domain").value(cookie.domain());
|
||||
writer.name("expiresAt").value(cookie.expiresAt());
|
||||
writer.name("hostOnly").value(cookie.hostOnly());
|
||||
writer.name("httpOnly").value(cookie.httpOnly());
|
||||
writer.name("name").value(cookie.name());
|
||||
writer.name("path").value(cookie.path());
|
||||
writer.name("persistent").value(cookie.persistent());
|
||||
writer.name("secure").value(cookie.secure());
|
||||
writer.name("value").value(cookie.value());
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
|
@ -3,11 +3,20 @@ package ctbrec.io;
|
|||
import java.io.IOException;
|
||||
import java.net.Authenticator;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Settings.ProxyType;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.OkHttpClient.Builder;
|
||||
|
@ -16,12 +25,16 @@ import okhttp3.Response;
|
|||
import okhttp3.Route;
|
||||
|
||||
public abstract class HttpClient {
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(HttpClient.class);
|
||||
|
||||
protected OkHttpClient client;
|
||||
protected CookieJarImpl cookieJar = new CookieJarImpl();
|
||||
protected boolean loggedIn = false;
|
||||
protected int loginTries = 0;
|
||||
private String name;
|
||||
|
||||
protected HttpClient() {
|
||||
protected HttpClient(String name) {
|
||||
this.name = name;
|
||||
reconfigure();
|
||||
}
|
||||
|
||||
|
@ -112,10 +125,23 @@ public abstract class HttpClient {
|
|||
}
|
||||
|
||||
public void shutdown() {
|
||||
persistCookies();
|
||||
client.connectionPool().evictAll();
|
||||
client.dispatcher().executorService().shutdown();
|
||||
}
|
||||
|
||||
private void persistCookies() {
|
||||
try {
|
||||
Map<String, List<Cookie>> cookies = cookieJar.getCookies();
|
||||
Moshi moshi = new Moshi.Builder().add(Cookie.class, new CookieJsonAdapter()).build();
|
||||
@SuppressWarnings("rawtypes")
|
||||
JsonAdapter<Map> adapter = moshi.adapter(Map.class).indent(" ");
|
||||
String json = adapter.toJson(cookies);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't persist cookies for {}", name, e);
|
||||
}
|
||||
}
|
||||
|
||||
private okhttp3.Authenticator createHttpProxyAuthenticator(String username, String password) {
|
||||
return new okhttp3.Authenticator() {
|
||||
@Override
|
||||
|
|
|
@ -6,6 +6,10 @@ import ctbrec.io.HttpClient;
|
|||
|
||||
public class RecorderHttpClient extends HttpClient {
|
||||
|
||||
public RecorderHttpClient() {
|
||||
super("recorder");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws IOException {
|
||||
return false;
|
||||
|
|
|
@ -26,6 +26,10 @@ public class BongaCamsHttpClient extends HttpClient {
|
|||
private static final transient Logger LOG = LoggerFactory.getLogger(BongaCamsHttpClient.class);
|
||||
private int userId = 0;
|
||||
|
||||
public BongaCamsHttpClient() {
|
||||
super("bongacams");
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean login() throws IOException {
|
||||
if(loggedIn) {
|
||||
|
|
|
@ -23,6 +23,10 @@ public class Cam4HttpClient extends HttpClient {
|
|||
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(Cam4HttpClient.class);
|
||||
|
||||
public Cam4HttpClient() {
|
||||
super("cam4");
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean login() throws IOException {
|
||||
if(loggedIn) {
|
||||
|
|
|
@ -29,6 +29,10 @@ public class CamsodaHttpClient extends HttpClient {
|
|||
private static final transient Logger LOG = LoggerFactory.getLogger(CamsodaHttpClient.class);
|
||||
private String csrfToken = null;
|
||||
|
||||
public CamsodaHttpClient() {
|
||||
super("camsoda");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws IOException {
|
||||
if(loggedIn) {
|
||||
|
|
|
@ -20,6 +20,10 @@ public class ChaturbateHttpClient extends HttpClient {
|
|||
private static final transient Logger LOG = LoggerFactory.getLogger(ChaturbateHttpClient.class);
|
||||
protected String token;
|
||||
|
||||
public ChaturbateHttpClient() {
|
||||
super("chaturbate");
|
||||
}
|
||||
|
||||
private void extractCsrfToken(Request request) {
|
||||
try {
|
||||
Cookie csrfToken = cookieJar.getCookie(request.url(), "csrftoken");
|
||||
|
|
|
@ -24,6 +24,10 @@ public class MyFreeCamsHttpClient extends HttpClient {
|
|||
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(MyFreeCamsHttpClient.class);
|
||||
|
||||
public MyFreeCamsHttpClient() {
|
||||
super("myfreecams");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws IOException {
|
||||
if(loggedIn) {
|
||||
|
|
|
@ -200,7 +200,7 @@ public class CamrecApplication extends Application {
|
|||
}
|
||||
|
||||
private void createHttpClient() {
|
||||
httpClient = new HttpClient() {
|
||||
httpClient = new HttpClient("camrec") {
|
||||
@Override
|
||||
public boolean login() throws IOException {
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue