Implement login for Flirt4Free

This commit is contained in:
0xboobface 2019-04-15 19:16:04 +02:00
parent 8910dd8c4a
commit 50c38d16a7
1 changed files with 20 additions and 41 deletions

View File

@ -1,18 +1,14 @@
package ctbrec.sites.flirt4free; package ctbrec.sites.flirt4free;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.List;
import org.json.JSONObject;
import org.jsoup.nodes.Element;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.io.HtmlParser;
import ctbrec.io.HttpClient; import ctbrec.io.HttpClient;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.sites.camsoda.Camsoda;
import okhttp3.FormBody; import okhttp3.FormBody;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
@ -20,7 +16,6 @@ import okhttp3.Response;
public class Flirt4FreeHttpClient extends HttpClient { public class Flirt4FreeHttpClient extends HttpClient {
private static final transient Logger LOG = LoggerFactory.getLogger(Flirt4FreeHttpClient.class); private static final transient Logger LOG = LoggerFactory.getLogger(Flirt4FreeHttpClient.class);
private String csrfToken = null;
public Flirt4FreeHttpClient() { public Flirt4FreeHttpClient() {
super("flirt4free"); super("flirt4free");
@ -39,29 +34,26 @@ public class Flirt4FreeHttpClient extends HttpClient {
return true; return true;
} }
String url = Flirt4Free.BASE_URI + "/api/v1/auth/login"; String url = Flirt4Free.BASE_URI + "/my-account/";
FormBody body = new FormBody.Builder() FormBody body = new FormBody.Builder()
.add("username", Config.getInstance().getSettings().camsodaUsername) .add("a", "login")
.add("password", Config.getInstance().getSettings().camsodaPassword) .add("small", "0")
.add("acctCheck", "1")
.add("login[remember_me]", "1")
.add("send_to_page", Flirt4Free.BASE_URI)
.add("query_string", "")
.add("login[username]", Config.getInstance().getSettings().flirt4freeUsername)
.add("login[password]", Config.getInstance().getSettings().flirt4freePassword)
.build(); .build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.post(body) .post(body)
.build(); .build();
try (Response response = execute(request)) { try (Response response = execute(request)) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string()); List<String> cookies = response.headers("Set-Cookie");
if (resp.has("error")) { return cookies.toString().contains("has_logged_in=1") && cookies.toString().contains("CHAT_USER=" + Config.getInstance().getSettings().flirt4freeUsername);
String error = resp.getString("error");
if (Objects.equals(error, "Please confirm that you are not a robot.")) {
// return loginWithDialog();
throw new IOException("CamSoda requested to solve a captcha. Please try again in a while (maybe 15 min).");
} else {
throw new IOException(resp.getString("error"));
}
} else {
return true;
}
} else { } else {
throw new HttpException(response.code(), response.message()); throw new HttpException(response.code(), response.message());
} }
@ -73,31 +65,18 @@ public class Flirt4FreeHttpClient extends HttpClient {
* @throws IOException * @throws IOException
*/ */
public boolean checkLoginSuccess() throws IOException { public boolean checkLoginSuccess() throws IOException {
String url = Camsoda.BASE_URI + "/api/v1/user/current"; String url = Flirt4Free.BASE_URI + "/my-account/";
Request request = new Request.Builder().url(url).build(); Request request = new Request.Builder()
.url(url)
.header("User-Agent", Config.getInstance().getSettings().httpUserAgent)
.build();
try(Response response = execute(request)) { try(Response response = execute(request)) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string()); String body = response.body().string();
return resp.optBoolean("status"); return body.contains("username-logged-in");
} else { } else {
return false; return false;
} }
} }
} }
protected String getCsrfToken() throws IOException {
if(csrfToken == null) {
String url = Camsoda.BASE_URI;
Request request = new Request.Builder().url(url).build();
try(Response response = execute(request)) {
if(response.isSuccessful()) {
Element meta = HtmlParser.getTag(response.body().string(), "meta[name=\"_token\"]");
csrfToken = meta.attr("content");
} else {
throw new HttpException(response.code(), response.message());
}
}
}
return csrfToken;
}
} }