Fix Streamate by removing the use of the XSRF

Apparently the XSRF is not needed anymore
This commit is contained in:
0xb00bface 2021-12-11 14:04:03 +01:00
parent 5f517682b9
commit 68c68a126a
3 changed files with 55 additions and 105 deletions

View File

@ -1,23 +1,5 @@
package ctbrec.ui.sites.streamate;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import static ctbrec.sites.streamate.Streamate.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.io.HttpException;
@ -27,15 +9,26 @@ import ctbrec.sites.streamate.StreamateModel;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import okhttp3.Request;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import static ctbrec.Model.State.OFFLINE;
import static ctbrec.Model.State.ONLINE;
import static ctbrec.io.HttpConstants.*;
import static ctbrec.sites.streamate.Streamate.NAIAD_URL;
public class StreamateFollowedService extends PaginatedScheduledService {
private static final Logger LOG = LoggerFactory.getLogger(StreamateFollowedService.class);
private static final int MODELS_PER_PAGE = 48;
private Streamate streamate;
private StreamateHttpClient httpClient;
private String url;
private final Streamate streamate;
private final StreamateHttpClient httpClient;
private final String url;
private boolean showOnline = true;
public StreamateFollowedService(Streamate streamate) {
@ -46,13 +39,14 @@ public class StreamateFollowedService extends PaginatedScheduledService {
@Override
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
return new Task<>() {
@Override
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
public List<Model> call() throws IOException {
httpClient.login();
String saKey = httpClient.getSaKey();
String pageUrl = url + "&from=" + ((page - 1) * MODELS_PER_PAGE) + "&size=" + MODELS_PER_PAGE;
LOG.debug("Fetching page {}", pageUrl);
var smtid = UUID.randomUUID() + "G0211569057409";
var request = new Request.Builder()
.url(pageUrl)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
@ -62,14 +56,13 @@ public class StreamateFollowedService extends PaginatedScheduledService {
.header(REFERER, streamate.getBaseUrl() + "/view/favorites")
.header("sakey", saKey)
.header("platform", "SCP")
.header("smtid", UUID.randomUUID().toString() + "G0211569057409")
.header("smeid", UUID.randomUUID().toString() + "G0211569057409")
.header("smvid", UUID.randomUUID().toString() + "G0211569057409")
.header("X-XSRF-TOKEN", httpClient.getXsrfToken())
.header("smtid", smtid)
.header("smeid", smtid)
.header("smvid", smtid)
.build();
try(var response = streamate.getHttpClient().execute(request)) {
try (var response = streamate.getHttpClient().execute(request)) {
if (response.isSuccessful()) {
return parseModels(response.body().string());
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -1,21 +1,5 @@
package ctbrec.ui.sites.streamate;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import ctbrec.Model;
import ctbrec.io.HttpException;
import ctbrec.sites.streamate.Streamate;
@ -23,15 +7,27 @@ import ctbrec.sites.streamate.StreamateHttpClient;
import ctbrec.sites.streamate.StreamateModel;
import ctbrec.ui.tabs.PaginatedScheduledService;
import javafx.concurrent.Task;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.ORIGIN;
public class StreamateUpdateService extends PaginatedScheduledService {
private static final Logger LOG = LoggerFactory.getLogger(StreamateUpdateService.class);
private static final int MODELS_PER_PAGE = 48;
private Streamate streamate;
private StreamateHttpClient httpClient;
private String url;
private final Streamate streamate;
private final StreamateHttpClient httpClient;
private final String url;
public StreamateUpdateService(Streamate streamate, String url) {
this.streamate = streamate;
@ -41,25 +37,26 @@ public class StreamateUpdateService extends PaginatedScheduledService {
@Override
protected Task<List<Model>> createTask() {
return new Task<List<Model>>() {
return new Task<>() {
@Override
public List<Model> call() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
public List<Model> call() throws IOException {
int from = (page - 1) * MODELS_PER_PAGE;
String saKey = httpClient.getSaKey();
String pageUrl = url + "&from=" + from + "&size=" + MODELS_PER_PAGE;
LOG.debug("Fetching page {}", pageUrl);
var smtid = UUID.randomUUID() + "G0211569057409";
var request = httpClient.newRequestBuilder()
.url(pageUrl)
.header(ORIGIN, streamate.getBaseUrl())
.header("sakey", saKey)
.header("platform", "SCP")
.header("smtid", UUID.randomUUID().toString() + "G0211569057409")
.header("smeid", UUID.randomUUID().toString() + "G0211569057409")
.header("smvid", UUID.randomUUID().toString() + "G0211569057409")
.header("smtid", smtid)
.header("smeid", smtid)
.header("smvid", smtid)
.build();
try (var response = httpClient.execute(request)) {
if (response.isSuccessful()) {
return parseModels(response.body().string());
return parseModels(Objects.requireNonNull(response.body(), "HTTP response body is null").string());
} else {
throw new HttpException(response.code(), response.message());
}

View File

@ -1,28 +1,19 @@
package ctbrec.sites.streamate;
import static ctbrec.io.HttpConstants.*;
import ctbrec.Config;
import ctbrec.io.HttpClient;
import okhttp3.*;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Objects;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import static ctbrec.io.HttpConstants.*;
public class StreamateHttpClient extends HttpClient {
@ -34,7 +25,6 @@ public class StreamateHttpClient extends HttpClient {
private Long userId;
private String saKey = "";
private String userNickname = "";
private String xsrfToken = null;
public StreamateHttpClient(Config config) {
super("streamate", config);
@ -54,31 +44,6 @@ public class StreamateHttpClient extends HttpClient {
} catch (NoSuchElementException e) {
// ignore
}
loadXsrfToken();
}
private void loadXsrfToken() {
// do a first request to get cookies and stuff
Request req = new Request.Builder() //
.url(Streamate.BASE_URL + "/initialData.js") //
.header(USER_AGENT, config.getSettings().httpUserAgent) //
.header(COOKIE, "smtid="+UUID.randomUUID().toString()+"; Xld_rct=1;") //
.header(REFERER, Streamate.BASE_URL)
.build();
try (Response resp = execute(req)) {
if (resp.code() == 200) {
LOG.info("Initial request was fine, Extracting XSRF token");
Matcher m = Pattern.compile("\"xsrfToken\":\"(.*?)\"").matcher(resp.body().string());
if (m.find()) {
xsrfToken = m.group(1);
}
} else {
throw new HttpException(resp.code(), resp.message());
}
} catch (IOException e) {
LOG.error("Initial request failed", e);
}
}
@Override
@ -113,7 +78,7 @@ public class StreamateHttpClient extends HttpClient {
.post(body)
.build();
try (Response response = client.newCall(login).execute()) {
String content = response.body().string();
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
if(response.isSuccessful()) {
JSONObject json = new JSONObject(content);
loggedIn = json.has(SAKEY_KEY);
@ -135,8 +100,7 @@ public class StreamateHttpClient extends HttpClient {
.header(ACCEPT, MIMETYPE_APPLICATION_JSON)
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
.header(REFERER, Streamate.BASE_URL)
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.header("X-XSRF-TOKEN", getXsrfToken()); // @formatter:on
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST); // @formatter:on
}
/**
@ -148,7 +112,7 @@ public class StreamateHttpClient extends HttpClient {
Request request = newRequestBuilder().url(url).build();
try (Response response = execute(request)) {
if (response.isSuccessful()) {
String content = response.body().string();
String content = Objects.requireNonNull(response.body(), "HTTP response body is null").string();
JSONObject json = new JSONObject(content);
return json.optString("status").equals("SM_OK");
} else {
@ -173,8 +137,4 @@ public class StreamateHttpClient extends HttpClient {
public String getUserNickname() {
return userNickname;
}
public String getXsrfToken() {
return xsrfToken;
}
}