Try to restore HTTP session with cookies before login in

All the HTTP clients now check, if loading the cookies was sufficient
to restore the HTTP session. If yes, we assume that we are logged in.
If not, the normal login procedure is done.
This commit is contained in:
0xboobface 2018-11-06 22:17:30 +01:00
parent 75351cedb5
commit 1b11af8872
5 changed files with 76 additions and 0 deletions

View File

@ -36,6 +36,13 @@ public class BongaCamsHttpClient extends HttpClient {
return true;
}
boolean cookiesWorked = checkLoginSuccess();
if(cookiesWorked) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
Runnable showDialog = () -> {

View File

@ -33,6 +33,13 @@ public class Cam4HttpClient extends HttpClient {
return true;
}
boolean cookiesWorked = checkLoginSuccess();
if(cookiesWorked) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
BlockingQueue<Boolean> queue = new LinkedBlockingQueue<>();
Runnable showDialog = () -> {

View File

@ -39,6 +39,13 @@ public class CamsodaHttpClient extends HttpClient {
return true;
}
// persisted cookies might let us log in
if(checkLoginSuccess()) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
String url = Camsoda.BASE_URI + "/api/v1/auth/login";
FormBody body = new FormBody.Builder()
.add("username", Config.getInstance().getSettings().camsodaUsername)

View File

@ -42,6 +42,16 @@ public class ChaturbateHttpClient extends HttpClient {
@Override
public boolean login() throws IOException {
if(loggedIn) {
return true;
}
if(checkLogin()) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
try {
Request login = new Request.Builder()
.url(Chaturbate.BASE_URI + "/auth/login/")
@ -86,6 +96,24 @@ public class ChaturbateHttpClient extends HttpClient {
return loggedIn;
}
private boolean checkLogin() throws IOException {
String url = "https://chaturbate.com/p/" + Config.getInstance().getSettings().username + "/";
Request req = new Request.Builder().url(url).build();
Response resp = execute(req);
if (resp.isSuccessful()) {
String profilePage = resp.body().string();
try {
HtmlParser.getText(profilePage, "span.tokencount");
return true;
} catch(Exception e) {
LOG.debug("Token tag not found. Login failed");
return false;
}
} else {
throw new IOException("HTTP response: " + resp.code() + " - " + resp.message());
}
}
@Override
public Response execute(Request req, boolean requiresLogin) throws IOException {
Response resp = super.execute(req, requiresLogin);

View File

@ -5,11 +5,13 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.io.HttpClient;
import ctbrec.ui.HtmlParser;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
@ -34,6 +36,12 @@ public class MyFreeCamsHttpClient extends HttpClient {
return true;
}
if(checkLogin()) {
loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
String username = Config.getInstance().getSettings().mfcUsername;
String password = Config.getInstance().getSettings().mfcPassword;
RequestBody body = new FormBody.Builder()
@ -65,6 +73,25 @@ public class MyFreeCamsHttpClient extends HttpClient {
}
}
private boolean checkLogin() throws IOException {
Request req = new Request.Builder().url(MyFreeCams.BASE_URI + "/php/account.php?request=status").build();
Response resp = execute(req);
if(resp.isSuccessful()) {
String content = resp.body().string();
try {
Elements tags = HtmlParser.getTags(content, "div.content > p > b");
tags.get(2).text();
return true;
} catch(Exception e) {
LOG.debug("Token tag not found. Login failed");
return false;
}
} else {
resp.close();
throw new IOException(resp.code() + " " + resp.message());
}
}
public WebSocket newWebSocket(Request req, WebSocketListener webSocketListener) {
return client.newWebSocket(req, webSocketListener);
}