From 70a9d65e4896c268192bba16735072ec066edd16 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sun, 27 Jan 2019 14:05:45 +0100 Subject: [PATCH] Fix login for FC2Live FC2Live sends cookies with value "deleted" to invalidate previously set values. In the same request they send new values, too. That confused OkHttp. We now use a custom cookie jar for FC2Live, which ignores cookies with the value "deleted" --- .../fc2live/Fc2FollowedUpdateService.java | 2 +- .../src/main/java/ctbrec/io/HttpClient.java | 5 ++++ .../ctbrec/sites/fc2live/Fc2CookieJar.java | 25 ++++++++++++++++ .../ctbrec/sites/fc2live/Fc2HttpClient.java | 29 ++++++++++++++++--- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/ctbrec/sites/fc2live/Fc2CookieJar.java diff --git a/client/src/main/java/ctbrec/ui/sites/fc2live/Fc2FollowedUpdateService.java b/client/src/main/java/ctbrec/ui/sites/fc2live/Fc2FollowedUpdateService.java index 5db22826..c97ce4d2 100644 --- a/client/src/main/java/ctbrec/ui/sites/fc2live/Fc2FollowedUpdateService.java +++ b/client/src/main/java/ctbrec/ui/sites/fc2live/Fc2FollowedUpdateService.java @@ -67,7 +67,7 @@ public class Fc2FollowedUpdateService extends PaginatedScheduledService { } return models; } else { - throw new IOException("Request was not successful: " + content); + throw new IOException("Request was not successful: " + json.toString()); } } else { throw new HttpException(resp.code(), resp.message()); diff --git a/common/src/main/java/ctbrec/io/HttpClient.java b/common/src/main/java/ctbrec/io/HttpClient.java index 5da3d0b9..e65e8983 100644 --- a/common/src/main/java/ctbrec/io/HttpClient.java +++ b/common/src/main/java/ctbrec/io/HttpClient.java @@ -43,9 +43,14 @@ public abstract class HttpClient { protected HttpClient(String name) { this.name = name; + cookieJar = createCookieJar(); reconfigure(); } + protected CookieJarImpl createCookieJar() { + return new CookieJarImpl(); + } + private void loadProxySettings() { ProxyType proxyType = Config.getInstance().getSettings().proxyType; switch (proxyType) { diff --git a/common/src/main/java/ctbrec/sites/fc2live/Fc2CookieJar.java b/common/src/main/java/ctbrec/sites/fc2live/Fc2CookieJar.java new file mode 100644 index 00000000..0331ce99 --- /dev/null +++ b/common/src/main/java/ctbrec/sites/fc2live/Fc2CookieJar.java @@ -0,0 +1,25 @@ +package ctbrec.sites.fc2live; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import ctbrec.io.CookieJarImpl; +import okhttp3.Cookie; +import okhttp3.HttpUrl; + +public class Fc2CookieJar extends CookieJarImpl { + + @Override + public void saveFromResponse(HttpUrl url, List cookies) { + List sanitizedCookies = new ArrayList<>(cookies); + for (Iterator iterator = sanitizedCookies.iterator(); iterator.hasNext();) { + Cookie cookie = iterator.next(); + if(cookie.value().equalsIgnoreCase("deleted")) { + // ignore and remove from list + iterator.remove(); + } + } + super.saveFromResponse(url, sanitizedCookies); + } +} diff --git a/common/src/main/java/ctbrec/sites/fc2live/Fc2HttpClient.java b/common/src/main/java/ctbrec/sites/fc2live/Fc2HttpClient.java index 8a70a70b..62e342cd 100644 --- a/common/src/main/java/ctbrec/sites/fc2live/Fc2HttpClient.java +++ b/common/src/main/java/ctbrec/sites/fc2live/Fc2HttpClient.java @@ -7,6 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ctbrec.Config; +import ctbrec.io.CookieJarImpl; import ctbrec.io.HttpClient; import ctbrec.io.HttpException; import okhttp3.FormBody; @@ -24,9 +25,15 @@ public class Fc2HttpClient extends HttpClient { super("fc2live"); } + @Override + protected CookieJarImpl createCookieJar() { + return new Fc2CookieJar(); + } + @Override public boolean login() throws IOException { - if(loggedIn) { + LOG.debug("Login"); + if (loggedIn) { return true; } @@ -54,14 +61,28 @@ public class Fc2HttpClient extends HttpClient { try(Response resp = execute(req)) { if(resp.isSuccessful()) { String page = resp.body().string(); + LOG.debug(page); if(page.contains("Invalid e-mail address or password")) { return false; } else { - loggedIn = true; - return true; + LOG.debug("Calling https://secure.id.fc2.com/?login=done"); + req = new Request.Builder() + .url("https://secure.id.fc2.com/?login=done") + .header("Referer", "https://secure.id.fc2.com/index.php?mode=login&switch_language=en") + .build(); + try (Response resp2 = execute(req)) { + if (resp.isSuccessful()) { + LOG.debug("Login complete"); + loggedIn = true; + return true; + } else { + LOG.debug("Login failed"); + loggedIn = false; + return false; + } + } } } else { - resp.close(); LOG.error("Login failed {} {}", resp.code(), resp.message()); return false; }