ctbrec-5.3.2-experimental/common/src/main/java/ctbrec/sites/stripchat/StripchatHttpClient.java

255 lines
10 KiB
Java

package ctbrec.sites.stripchat;
import ctbrec.Config;
import ctbrec.StringUtil;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
// import ctbrec.sites.stripchat.Stripchat;
// import ctbrec.sites.stripchat.StripchatUtil;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StripchatHttpClient
extends HttpClient {
private static final Logger LOG = LoggerFactory.getLogger(StripchatHttpClient.class);
public static final MediaType JSON = MediaType.parse((String)"application/json; charset=utf-8");
private static long userId = 0L;
private static String csrfToken;
private static String csrfTimestamp;
private static String csrfNotifyTimestamp;
private static String jwtToken;
private static Instant jwtTokenExp;
public StripchatHttpClient(Config config) {
super("stripchat", config);
}
@Override
public boolean login() throws IOException {
this.getCsrfToken();
if (this.loggedIn && this.isJwtTokenValid()) {
return true;
}
if (this.checkLoginSuccess()) {
this.loggedIn = true;
LOG.debug("Logged in with cookies");
return true;
}
String url = Stripchat.getBaseUri() + "/api/front/auth/login";
JSONObject requestParams = new JSONObject();
requestParams.put("loginOrEmail", config.getSettings().stripchatUsername);
requestParams.put("password", config.getSettings().stripchatPassword);
requestParams.put("remember", true).put("csrfToken", csrfToken);
requestParams.put("csrfTimestamp", csrfTimestamp);
requestParams.put("csrfNotifyTimestamp", csrfNotifyTimestamp);
RequestBody body = RequestBody.Companion.create(requestParams.toString(), JSON);
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.header("User-Agent", config.getSettings().httpUserAgent)
.header("Origin", Stripchat.getBaseUri()).header("Referer", Stripchat.getBaseUri())
.header("Content-Type", "application/json")
.post(body)
.build();
try (Response response = this.execute(request);){
if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
if (resp.has("user")) {
JSONObject user = resp.getJSONObject("user");
userId = user.optLong("id");
boolean bl = true;
return bl;
}
boolean bl = false;
return bl;
}
LOG.info("Auto-Login failed: {} {} {}", new Object[]{response.code(), response.message(), url});
boolean bl = false;
return bl;
}
}
private void loadCsrfToken() throws IOException {
block9: {
csrfToken = "";
csrfTimestamp = "";
csrfNotifyTimestamp = "";
String url = Stripchat.getBaseUri() + "/api/front/v3/config/initial?requestPath=%2F&timezoneOffset=0&disableClient=0&uniq=" + StripchatUtil.getUniq();
Request request = new Request.Builder().url(url).header("Accept", "application/json")
.header("User-Agent", config.getSettings().httpUserAgent)
.header("Origin", Stripchat.getBaseUri())
.header("Referer", Stripchat.getBaseUri())
.header("Content-Type", "application/json")
.build();
try (Response response = this.execute(request);){
if (response.isSuccessful()) {
JSONObject initial;
JSONObject resp = new JSONObject(response.body().string());
if (resp.has("initial") && (initial = resp.getJSONObject("initial")).has("client")) {
JSONObject client = initial.getJSONObject("client");
csrfToken = client.optString("csrfToken");
csrfTimestamp = client.optString("csrfTimestamp");
csrfNotifyTimestamp = client.optString("csrfNotifyTimestamp");
LOG.debug("Stripchat CSRF token: {} [{}]", (Object)csrfToken, (Object)csrfNotifyTimestamp);
}
if (StringUtil.isBlank(csrfToken)) {
LOG.debug("Stripchat CSRF token not found");
}
break block9;
}
throw new HttpException(response.code(), response.message());
}
}
}
private void loadJwtToken() throws IOException {
block11: {
jwtToken = "";
String url = Stripchat.getBaseUri() + "/api/front/v3/config/dynamic?uniq=" + StripchatUtil.getUniq();
Request request = new Request.Builder().url(url)
.header("Accept", "application/json")
.header("User-Agent", config.getSettings().httpUserAgent)
.header("Origin", Stripchat.getBaseUri())
.header("Referer", Stripchat.getBaseUri())
.header("Content-Type", "application/json")
.build();
try (Response response = execute(request);){
if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
if (resp.has("dynamic")) {
JSONObject dynamic = resp.getJSONObject("dynamic");
jwtToken = dynamic.optString("jwtToken");
}
if (StringUtil.isNotBlank(jwtToken)) {
String[] parts = jwtToken.split("\\.");
if (parts.length > 1) {
String decString = new String(Base64.getDecoder().decode(parts[1]), StandardCharsets.UTF_8);
JSONObject body = new JSONObject(decString);
jwtTokenExp = Instant.ofEpochSecond(body.optLong("exp"));
}
LOG.debug("Stripchat JWT token: {} [{}]", (Object)jwtToken, (Object)jwtTokenExp);
} else {
LOG.debug("Stripchat JWT token not found");
}
break block11;
}
throw new HttpException(response.code(), response.message());
}
}
}
public boolean checkLoginSuccess() throws IOException {
getJwtToken();
getCsrfToken();
return StringUtil.isNotBlank(jwtToken) && StringUtil.isNotBlank(csrfToken);
}
public long getUserId() throws JSONException, IOException {
block8: {
if (userId == 0L) {
String url = Stripchat.getBaseUri() + "/api/front/users/username/" + config.getSettings().stripchatUsername;
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.header("User-Agent", config.getSettings().httpUserAgent)
.header("Origin", Stripchat.getBaseUri())
.header("Referer", Stripchat.getBaseUri())
.header("Content-Type", "application/json")
.build();
try (Response response = execute(request);){
if (response.isSuccessful()) {
JSONObject resp = new JSONObject(response.body().string());
JSONObject user = resp.getJSONObject("user");
userId = user.optLong("id");
break block8;
}
throw new HttpException(url, response.code(), response.message());
}
}
}
return userId;
}
public JSONObject getAmpl() {
try {
Cookie cookie = getCookieJar().getCookie(HttpUrl.parse((String)Stripchat.getBaseUri()), "baseAmpl");
String json = URLDecoder.decode(cookie.value(), "utf-8");
JSONObject ampl = new JSONObject(json);
ampl.put("ep", (Object)new JSONObject());
return ampl;
}
catch (Exception ex) {
return new JSONObject();
}
}
public String getCsrfNotifyTimestamp() {
return csrfNotifyTimestamp;
}
public String getCsrfTimestamp() {
return csrfTimestamp;
}
public String getCsrfToken() {
try {
if (!isTokenValid()) {
loadCsrfToken();
}
}
catch (Exception e) {
LOG.debug("Invalid CSRF Token {}: {}", (Object)csrfToken, (Object)e.getMessage());
csrfToken = "";
}
return csrfToken;
}
public String getJwtToken() {
try {
if (!isJwtTokenValid()) {
loadJwtToken();
}
}
catch (Exception e) {
LOG.debug("Invalid JWT Token {}: {}", (Object)jwtToken, (Object)e.getMessage());
jwtToken = "";
}
return jwtToken;
}
private boolean isJwtTokenValid() {
if (StringUtil.isBlank(jwtToken) || jwtTokenExp == null) {
return false;
}
return jwtTokenExp.isAfter(Instant.now().minus(3L, ChronoUnit.HOURS));
}
private boolean isTokenValid() {
if (StringUtil.isBlank(csrfToken) || StringUtil.isBlank(csrfTimestamp) || StringUtil.isBlank(csrfNotifyTimestamp)) {
return false;
}
try {
Instant notifyTime = Instant.parse(csrfNotifyTimestamp);
return notifyTime.isAfter(Instant.now().minus(3L, ChronoUnit.HOURS));
}
catch (Exception e) {
return false;
}
}
}