forked from j62/ctbrec
1
0
Fork 0

Use a different HTTP client for each MVLive download

MVLive only allows recording of mor than one stream, if the recorder looks like different HTTP sessions.
That's why we use a different HTTP client with its own cookie jar for each recording.
This commit is contained in:
0xb00bface 2020-08-18 14:39:48 +02:00
parent 7015c3173f
commit 2455797fb2
4 changed files with 68 additions and 22 deletions

View File

@ -59,7 +59,7 @@ public class MVLiveClient {
running = true;
if (ws == null && !connecting) {
fetchAuthenticationCookies();
httpClient.fetchAuthenticationCookies();
JSONObject response = getRoomLocation(model);
roomNumber = response.optString("floorId");
roomId = response.optString("roomId");
@ -88,18 +88,6 @@ public class MVLiveClient {
}
}
private void fetchAuthenticationCookies() throws IOException {
Request req = new Request.Builder()
.url("https://www.manyvids.com/tak-live-redirect.php")
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response response = httpClient.execute(req)) {
if (!response.isSuccessful()) {
throw new HttpException(response.code(), response.message());
}
}
}
private String getPhpSessionIdCookie() {
List<Cookie> cookies = httpClient.getCookiesByName("PHPSESSID");
return cookies.stream().map(c -> c.name() + "=" + c.value()).findFirst().orElse("");

View File

@ -1,8 +1,14 @@
package ctbrec.sites.manyvids;
import static ctbrec.io.HttpConstants.*;
import java.io.IOException;
import ctbrec.Config;
import ctbrec.io.HttpClient;
import ctbrec.io.HttpException;
import okhttp3.Request;
import okhttp3.Response;
public class MVLiveHttpClient extends HttpClient {
@ -14,4 +20,26 @@ public class MVLiveHttpClient extends HttpClient {
public boolean login() throws IOException {
return false;
}
public MVLiveHttpClient newSession() {
MVLiveHttpClient newClient = new MVLiveHttpClient();
newClient.client = newClient.client.newBuilder()
.cookieJar(createCookieJar())
.build();
newClient.reconfigure();
newClient.cookieJar.clear();
return newClient;
}
public void fetchAuthenticationCookies() throws IOException {
Request req = new Request.Builder()
.url("https://www.manyvids.com/tak-live-redirect.php")
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response response = execute(req)) {
if (!response.isSuccessful()) {
throw new HttpException(response.code(), response.message());
}
}
}
}

View File

@ -32,6 +32,15 @@ public class MVLiveMergedHlsDownload extends MergedFfmpegHlsDownload {
return t;
});
scheduler.scheduleAtFixedRate(() -> updateCloudFlareCookies(), 2, 2, TimeUnit.MINUTES);
try {
((MVLiveModel)getModel()).getRoomLocation();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateCloudFlareCookies();
super.start();
} finally {
scheduler.shutdown();

View File

@ -39,6 +39,7 @@ public class MVLiveModel extends AbstractModel {
private static final transient Logger LOG = LoggerFactory.getLogger(MVLiveModel.class);
private MVLiveHttpClient httpClient;
private MVLiveClient client;
private String roomNumber;
@ -87,7 +88,7 @@ public class MVLiveModel extends AbstractModel {
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response response = getSite().getHttpClient().execute(req)) {
try (Response response = getHttpClient().execute(req)) {
if (response.isSuccessful()) {
String body = response.body().string();
LOG.debug(body);
@ -109,7 +110,7 @@ public class MVLiveModel extends AbstractModel {
.url(url)
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response response = site.getHttpClient().execute(req)) {
try (Response response = getHttpClient().execute(req)) {
if (!response.isSuccessful()) {
throw new HttpException(response.code(), response.message());
}
@ -128,7 +129,21 @@ public class MVLiveModel extends AbstractModel {
return roomNumber;
}
private JSONObject getRoomLocation() throws IOException {
private void fetchGeneralCookies() throws IOException {
Request req = new Request.Builder()
.url(getSite().getBaseUrl())
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.build();
try (Response response = getHttpClient().execute(req)) {
if (!response.isSuccessful()) {
throw new HttpException(response.code(), response.message());
}
}
}
public JSONObject getRoomLocation() throws IOException {
fetchGeneralCookies();
httpClient.fetchAuthenticationCookies();
String url = MVLive.WS_ORIGIN + "/api/roomlocation/" + getName() + "?private=false";
Request req = new Request.Builder()
.url(url)
@ -137,7 +152,7 @@ public class MVLiveModel extends AbstractModel {
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(REFERER, MVLive.WS_ORIGIN + "/stream/" + getName())
.build();
try (Response response = site.getHttpClient().execute(req)) {
try (Response response = getHttpClient().execute(req)) {
if (response.isSuccessful()) {
JSONObject json = new JSONObject(response.body().string());
return json;
@ -149,9 +164,7 @@ public class MVLiveModel extends AbstractModel {
private synchronized MVLiveClient getClient() {
if (client == null) {
MVLive site = (MVLive) getSite();
MVLiveHttpClient httpClient = (MVLiveHttpClient) site.getHttpClient();
client = new MVLiveClient(httpClient);
client = new MVLiveClient(getHttpClient());
}
return client;
}
@ -183,9 +196,17 @@ public class MVLiveModel extends AbstractModel {
@Override
public Download createDownload() {
if (Config.isServerMode() && !Config.getInstance().getSettings().recordSingleFile) {
return new HlsDownload(getSite().getHttpClient());
return new HlsDownload(getHttpClient());
} else {
return new MVLiveMergedHlsDownload(getSite().getHttpClient());
return new MVLiveMergedHlsDownload(getHttpClient());
}
}
private synchronized MVLiveHttpClient getHttpClient() {
if (httpClient == null) {
MVLiveHttpClient siteHttpClient = (MVLiveHttpClient) getSite().getHttpClient();
httpClient = siteHttpClient.newSession();
}
return httpClient;
}
}