Skip to content

Commit e10cb6a

Browse files
committed
Initialise sts with script.
1 parent 08c9dd4 commit e10cb6a

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

common/src/main/java/dev/lavalink/youtube/cipher/CipherManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ default CachedPlayerScript getPlayerScript(@NotNull HttpInterface httpInterface)
4848
throw new ExceptionWithResponseBody("no jsUrl found", responseText);
4949
}
5050

51-
return new CachedPlayerScript(scriptUrl);
51+
return new CachedPlayerScript(scriptUrl, getTimestamp(httpInterface, scriptUrl));
5252
} catch (IOException e) {
5353
throw ExceptionTools.toRuntimeException(e);
5454
}
@@ -57,10 +57,12 @@ default CachedPlayerScript getPlayerScript(@NotNull HttpInterface httpInterface)
5757

5858
class CachedPlayerScript {
5959
public final String url;
60+
public final String signatureTimestamp;
6061
public final long expireTimestampMs;
6162

62-
protected CachedPlayerScript(@NotNull String url) {
63+
protected CachedPlayerScript(@NotNull String url, @NotNull String signatureTimestamp) {
6364
this.url = url;
65+
this.signatureTimestamp = signatureTimestamp;
6466
this.expireTimestampMs = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1);
6567
}
6668
}

common/src/main/java/dev/lavalink/youtube/cipher/LocalSignatureCipherManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public String getTimestamp(HttpInterface httpInterface, String sourceUrl) throws
265265
throw new IOException("Received non-success response code " + statusCode + " from script url " +
266266
sourceUrl + " ( " + CipherUtils.parseTokenScriptUrl(sourceUrl) + " )");
267267
}
268+
268269
return getScriptTimestamp(httpInterface, EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8), sourceUrl);
269270
}
270271
}

common/src/main/java/dev/lavalink/youtube/cipher/RemoteCipherManager.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,32 @@ public CachedPlayerScript getCachedPlayerScript(@NotNull HttpInterface httpInter
9595

9696
public String getTimestamp(HttpInterface httpInterface, String sourceUrl) throws IOException {
9797
synchronized (this) {
98-
log.debug("Timestamp from script {}", sourceUrl);
99-
return getTimestampFromScript(httpInterface, sourceUrl);
100-
}
101-
}
98+
HttpPost request = new HttpPost(getRemoteEndpoint("get_sts"));
10299

103-
private String getRemoteEndpoint(String path) {
104-
return remoteUrl.endsWith("/") ? remoteUrl + path : remoteUrl + "/" + path;
105-
}
100+
log.debug("Getting timestamp for script: {}", sourceUrl);
106101

107-
private String getTimestampFromScript(HttpInterface httpInterface, String playerScript) throws IOException {
108-
HttpPost request = new HttpPost(getRemoteEndpoint("get_sts"));
102+
String requestBody = JsonWriter.string()
103+
.object()
104+
.value("player_url", sourceUrl)
105+
.end()
106+
.done();
107+
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
109108

110-
log.debug("Getting timestamp for script: {}", playerScript);
109+
try (CloseableHttpResponse response = configureHttpInterface(httpInterface).execute(request)) {
110+
String responseBody = validateAndGetResponseBody(response);
111111

112-
String requestBody = JsonWriter.string()
113-
.object()
114-
.value("player_url", playerScript)
115-
.end()
116-
.done();
117-
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
112+
log.debug("Received response from remote cipher service: {}", responseBody);
118113

119-
try (CloseableHttpResponse response = configureHttpInterface(httpInterface).execute(request)) {
120-
String responseBody = validateAndGetResponseBody(response);
121-
122-
log.debug("Received response from remote cipher service: {}", responseBody);
123-
124-
JsonBrowser json = JsonBrowser.parse(responseBody);
125-
return json.get("sts").text();
114+
JsonBrowser json = JsonBrowser.parse(responseBody);
115+
return json.get("sts").text();
116+
}
126117
}
127118
}
128119

120+
private String getRemoteEndpoint(String path) {
121+
return remoteUrl.endsWith("/") ? remoteUrl + path : remoteUrl + "/" + path;
122+
}
123+
129124
public HttpInterface configureHttpInterface(HttpInterface httpInterface) {
130125
httpInterface.getContext().setAttribute(YoutubeHttpContextFilter.ATTRIBUTE_CIPHER_REQUEST_SPECIFIED, true);
131126
return httpInterface;

common/src/main/java/dev/lavalink/youtube/clients/skeleton/NonMusicClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ protected JsonBrowser loadTrackInfoFromInnertube(@NotNull YoutubeAudioSourceMana
125125
}
126126

127127
String payload = config.setAttributes(httpInterface).toJsonString();
128+
128129
if (requirePlayerScript()) {
129130
CachedPlayerScript playerScript = cipherManager.getCachedPlayerScript(httpInterface);
130-
String timestamp = cipherManager.getTimestamp(httpInterface, playerScript.url);
131-
payload = config.withPlaybackSignatureTimestamp(timestamp)
131+
132+
payload = config.withPlaybackSignatureTimestamp(playerScript.signatureTimestamp)
132133
.setAttributes(httpInterface)
133134
.toJsonString();
134135
}

0 commit comments

Comments
 (0)