From bf6b7156428e79a650851f93a453624ae432a6f1 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Wed, 9 Jan 2019 20:35:31 +0100 Subject: [PATCH] Login every 30 min to LiveJasmin so that the session does not expire --- .../jasmin/LiveJasminElectronLoginDialog.java | 4 +- .../ui/sites/jasmin/LiveJasminSiteUi.java | 13 ++- .../sites/jasmin/LiveJasminUpdateService.java | 16 ++- .../ui/sites/jasmin/NotLoggedInExcetion.java | 5 + .../sites/jasmin/LiveJasminHttpClient.java | 106 ------------------ 5 files changed, 34 insertions(+), 110 deletions(-) create mode 100644 client/src/main/java/ctbrec/ui/sites/jasmin/NotLoggedInExcetion.java diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminElectronLoginDialog.java b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminElectronLoginDialog.java index 86879d9e..4efd2d23 100644 --- a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminElectronLoginDialog.java +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminElectronLoginDialog.java @@ -24,7 +24,7 @@ public class LiveJasminElectronLoginDialog { private CookieJar cookieJar; private ExternalBrowser browser; - public LiveJasminElectronLoginDialog(CookieJar cookieJar) throws IOException { + public LiveJasminElectronLoginDialog(CookieJar cookieJar) throws Exception { this.cookieJar = cookieJar; browser = ExternalBrowser.getInstance(); try { @@ -37,6 +37,8 @@ public class LiveJasminElectronLoginDialog { browser.run(msg.toString(), msgHandler); } catch (InterruptedException e) { throw new IOException("Couldn't wait for login dialog", e); + } catch (IOException e) { + LOG.debug("Error while starting the browser or communication to it", e); } finally { browser.close(); browser.release(); diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminSiteUi.java b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminSiteUi.java index 94634972..e017e4aa 100644 --- a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminSiteUi.java +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminSiteUi.java @@ -3,6 +3,7 @@ package ctbrec.ui.sites.jasmin; import java.io.IOException; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +21,7 @@ public class LiveJasminSiteUi implements SiteUI { private LiveJasmin liveJasmin; private LiveJasminTabProvider tabProvider; private LiveJasminConfigUi configUi; + private long lastLoginTime = 0; public LiveJasminSiteUi(LiveJasmin liveJasmin) { this.liveJasmin = liveJasmin; @@ -39,16 +41,23 @@ public class LiveJasminSiteUi implements SiteUI { @Override public synchronized boolean login() throws IOException { + // renew login every 30 min + long now = System.currentTimeMillis(); + boolean renew = false; + if((now - lastLoginTime) > TimeUnit.MINUTES.toMillis(30)) { + renew = true; + } + boolean automaticLogin = liveJasmin.login(); - if(automaticLogin) { + if(automaticLogin && !renew) { return true; } else { + lastLoginTime = System.currentTimeMillis(); BlockingQueue queue = new LinkedBlockingQueue<>(); new Thread (() -> { // login with external browser window try { - //LiveJasminElectronLoginDialog dialog = new LiveJasminElectronLoginDialog(liveJasmin.getHttpClient().getCookieJar()); } catch (Exception e1) { LOG.error("Error logging in with external browser", e1); diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminUpdateService.java b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminUpdateService.java index e19a3b3d..69017169 100644 --- a/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/LiveJasminUpdateService.java @@ -17,6 +17,8 @@ import ctbrec.io.HttpException; import ctbrec.sites.jasmin.LiveJasmin; import ctbrec.sites.jasmin.LiveJasminModel; import ctbrec.ui.PaginatedScheduledService; +import ctbrec.ui.SiteUI; +import ctbrec.ui.SiteUiFactory; import javafx.concurrent.Task; import okhttp3.Cookie; import okhttp3.HttpUrl; @@ -38,8 +40,11 @@ public class LiveJasminUpdateService extends PaginatedScheduledService { protected Task> createTask() { return new Task>() { @Override - public List call() throws IOException { + public List call() throws IOException, NotLoggedInExcetion { //String _url = url + ((page-1) * 36); // TODO find out how to switch pages + if(!SiteUiFactory.getUi(liveJasmin).login()) { + throw new NotLoggedInExcetion(); + } // sort by popularity CookieJarImpl cookieJar = liveJasmin.getHttpClient().getCookieJar(); @@ -60,6 +65,7 @@ public class LiveJasminUpdateService extends PaginatedScheduledService { .addHeader("X-Requested-With", "XMLHttpRequest") .build(); try (Response response = liveJasmin.getHttpClient().execute(request)) { + LOG.debug("Response {} {}", response.code(), response.message()); if (response.isSuccessful()) { String body = response.body().string(); List models = new ArrayList<>(); @@ -81,6 +87,14 @@ public class LiveJasminUpdateService extends PaginatedScheduledService { model.setOnlineState(LiveJasminModel.mapStatus(m.optInt("status"))); models.add(model); } + } else if(json.optString("error").equals("Please login.")) { + SiteUI siteUI = SiteUiFactory.getUi(liveJasmin); + if(siteUI.login()) { + return call(); + } else { + LOG.error("Request failed:\n{}", body); + throw new IOException("Response was not successful"); + } } else { LOG.error("Request failed:\n{}", body); throw new IOException("Response was not successful"); diff --git a/client/src/main/java/ctbrec/ui/sites/jasmin/NotLoggedInExcetion.java b/client/src/main/java/ctbrec/ui/sites/jasmin/NotLoggedInExcetion.java new file mode 100644 index 00000000..57b528b7 --- /dev/null +++ b/client/src/main/java/ctbrec/ui/sites/jasmin/NotLoggedInExcetion.java @@ -0,0 +1,5 @@ +package ctbrec.ui.sites.jasmin; + +public class NotLoggedInExcetion extends Exception { + +} diff --git a/common/src/main/java/ctbrec/sites/jasmin/LiveJasminHttpClient.java b/common/src/main/java/ctbrec/sites/jasmin/LiveJasminHttpClient.java index 1d495599..3991c3d8 100644 --- a/common/src/main/java/ctbrec/sites/jasmin/LiveJasminHttpClient.java +++ b/common/src/main/java/ctbrec/sites/jasmin/LiveJasminHttpClient.java @@ -28,52 +28,6 @@ public class LiveJasminHttpClient extends HttpClient { return true; } - // loadMainPage(); // to get initial cookies - // Cookie captchaCookie = new Cookie.Builder() - // .domain("livejasmin.com") - // .name("captchaRequired") - // .value("0") - // .build(); - // getCookieJar().saveFromResponse(HttpUrl.parse("https://livejasmin.com"), Collections.singletonList(captchaCookie)); - // getCookieJar().saveFromResponse(HttpUrl.parse("https://www.livejasmin.com"), Collections.singletonList(captchaCookie)); - // getCookieJar().saveFromResponse(HttpUrl.parse("https://m.livejasmin.com"), Collections.singletonList(captchaCookie)); - // Map formParams = getLoginFormParameters(); - // getCookieJar().saveFromResponse(HttpUrl.parse("https://livejasmin.com"), Collections.singletonList(captchaCookie)); - // getCookieJar().saveFromResponse(HttpUrl.parse("https://m.livejasmin.com"), Collections.singletonList(captchaCookie)); - // String action = formParams.get("action"); - // formParams.remove("action"); - // Builder formBuilder = new FormBody.Builder(); - // for (Entry param : formParams.entrySet()) { - // formBuilder.add(param.getKey(), param.getValue()); - // } - // formBuilder.add("username", Config.getInstance().getSettings().livejasminUsername); - // formBuilder.add("password", Config.getInstance().getSettings().livejasminPassword); - // FormBody form = formBuilder.build(); - // Buffer b = new Buffer(); - // form.writeTo(b); - // LOG.debug("Form: {}", b.readUtf8()); - // Map> cookies = getCookieJar().getCookies(); - // for (Entry> domain : cookies.entrySet()) { - // LOG.debug("{}", domain.getKey()); - // List cks = domain.getValue(); - // for (Cookie cookie : cks) { - // LOG.debug(" {}", cookie); - // } - // } - // Request request = new Request.Builder() - // .url(LiveJasmin.BASE_URL + action) - // .header("User-Agent", USER_AGENT) - // .header("Accept", "*/*") - // .header("Accept-Language", "en") - // .header("Referer", LiveJasmin.BASE_URL + "/en/girls/") - // .header("X-Requested-With", "XMLHttpRequest") - // .post(form) - // .build(); - // try(Response response = execute(request)) { - // System.out.println("login " + response.code() + " - " + response.message()); - // System.out.println("login " + response.body().string()); - // } - boolean cookiesWorked = checkLoginSuccess(); if (cookiesWorked) { loggedIn = true; @@ -84,67 +38,12 @@ public class LiveJasminHttpClient extends HttpClient { return false; } - // private void loadMainPage() throws IOException { - // Request request = new Request.Builder() - // .url(LiveJasmin.BASE_URL) - // .header("User-Agent", USER_AGENT) - // .build(); - // try(Response response = execute(request)) { - // } - // } - // - // private Map getLoginFormParameters() throws IOException { - // long ts = System.currentTimeMillis(); - // String url = LiveJasmin.BASE_URL + "/en/auth/overlay/get-login-block?_dc="+ts; - // Request request = new Request.Builder() - // .url(url) - // .addHeader("User-Agent", USER_AGENT) - // .addHeader("Accept", "application/json, text/javascript, */*") - // .addHeader("Accept-Language", "en") - // .addHeader("Referer", LiveJasmin.BASE_URL) - // .addHeader("X-Requested-With", "XMLHttpRequest") - // .build(); - // try(Response response = execute(request)) { - // if(response.isSuccessful()) { - // String body = response.body().string(); - // JSONObject json = new JSONObject(body); - // if(json.optBoolean("success")) { - // JSONObject data = json.getJSONObject("data"); - // String content = data.getString("content"); - // Map params = new HashMap<>(); - // Element form = HtmlParser.getTag(content, "form"); - // params.put("action", form.attr("action")); - // Elements hiddenInputs = HtmlParser.getTags(content, "input[type=hidden]"); - // for (Element input : hiddenInputs) { - // String name = input.attr("name"); - // String value = input.attr("value"); - // params.put(name, value); - // } - // params.put("keepmeloggedin", "1"); - // params.put("captcha", ""); - // params.remove("captcha_needed"); - // return params; - // } else { - // throw new IOException("Request was not successful: " + body); - // } - // } else { - // throw new HttpException(response.code(), response.message()); - // } - // } - // } - public boolean checkLoginSuccess() throws IOException { OkHttpClient temp = client.newBuilder() .followRedirects(false) .followSslRedirects(false) .build(); - // getCookieJar().getCookies().entrySet().forEach((e) -> { - // e.getValue().forEach(c -> { - // LOG.debug("LOGIN CHECK: Cookie {} {}={}", e.getKey(), c.name(), c.value()); - // }); - // }); - String url = "https://m.livejasmin.com/en/member/favourite/get-favourite-list?ajax=1"; Request request = new Request.Builder() .url(url) @@ -159,11 +58,6 @@ public class LiveJasminHttpClient extends HttpClient { if(response.isSuccessful()) { return true; } else { - // if(response.code() >= 300 && response.code() < 400) { - // for (String name : response.headers().names()) { - // LOG.debug("{}: {}", name, response.header(name)); - // } - // } return false; } }