Skip to content

Commit bd2e6eb

Browse files
committed
unfinished play play spoof
1 parent b8e30c1 commit bd2e6eb

File tree

5 files changed

+421
-182
lines changed

5 files changed

+421
-182
lines changed

extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/fix/ClientTokenService.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/fix/Constants.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import androidx.annotation.NonNull;
44

55
class Constants {
6-
static final String CLIENT_TOKEN_API_PATH = "/v1/clienttoken";
7-
static final String CLIENT_TOKEN_API_URL = "https://clienttoken.spotify.com" + CLIENT_TOKEN_API_PATH;
8-
96
// Modified by a patch. Do not touch.
107
@NonNull
118
static String getClientVersion() {

extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/fix/RequestListener.java

Lines changed: 30 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22

33
import androidx.annotation.NonNull;
44
import app.revanced.extension.shared.Logger;
5-
import app.revanced.extension.spotify.misc.fix.clienttoken.data.v0.ClienttokenHttp.ClientTokenResponse;
6-
import com.google.protobuf.MessageLite;
75
import fi.iki.elonen.NanoHTTPD;
86

9-
import java.io.ByteArrayInputStream;
10-
import java.io.FilterInputStream;
11-
import java.io.IOException;
12-
import java.io.InputStream;
13-
import java.util.Objects;
7+
import java.io.*;
8+
import java.util.*;
149

15-
import static app.revanced.extension.spotify.misc.fix.ClientTokenService.serveClientTokenRequest;
16-
import static app.revanced.extension.spotify.misc.fix.Constants.CLIENT_TOKEN_API_PATH;
1710
import static fi.iki.elonen.NanoHTTPD.Response.Status.INTERNAL_ERROR;
1811

1912
class RequestListener extends NanoHTTPD {
13+
private final SpoofClientService service;
14+
15+
private final Set<Map.Entry<String, RouteHandler>> routeHandlers;
16+
2017
RequestListener(int port) {
2118
super(port);
2219

20+
service = new SpoofClientService(port);
21+
routeHandlers = new HashMap<String, RouteHandler>() {
22+
{
23+
put("/ap", service::serveApResolveRequest);
24+
put("/v1/clienttoken", service::serveClientTokenRequest);
25+
put("/playplay", service::servePlayPlayRequest);
26+
}
27+
}.entrySet();
28+
2329
try {
2430
start();
2531
} catch (IOException ex) {
@@ -28,67 +34,29 @@ class RequestListener extends NanoHTTPD {
2834
}
2935
}
3036

37+
interface RouteHandler {
38+
@NonNull
39+
Response serve(@NonNull IHTTPSession session);
40+
}
41+
42+
3143
@NonNull
3244
@Override
3345
public Response serve(@NonNull IHTTPSession session) {
3446
String uri = session.getUri();
35-
if (!uri.equals(CLIENT_TOKEN_API_PATH)) return INTERNAL_ERROR_RESPONSE;
36-
3747
Logger.printInfo(() -> "Serving request for URI: " + uri);
3848

39-
ClientTokenResponse response = serveClientTokenRequest(getInputStream(session));
40-
if (response != null) return newResponse(Response.Status.OK, response);
41-
42-
Logger.printException(() -> "Failed to serve client token request");
43-
return INTERNAL_ERROR_RESPONSE;
44-
}
45-
46-
@NonNull
47-
private static InputStream newLimitedInputStream(InputStream inputStream, long contentLength) {
48-
return new FilterInputStream(inputStream) {
49-
private long remaining = contentLength;
50-
51-
@Override
52-
public int read() throws IOException {
53-
if (remaining <= 0) return -1;
54-
int result = super.read();
55-
if (result != -1) remaining--;
56-
return result;
57-
}
58-
59-
@Override
60-
public int read(byte[] b, int off, int len) throws IOException {
61-
if (remaining <= 0) return -1;
62-
len = (int) Math.min(len, remaining);
63-
int result = super.read(b, off, len);
64-
if (result != -1) remaining -= result;
65-
return result;
66-
}
67-
};
68-
}
69-
70-
@NonNull
71-
private static InputStream getInputStream(@NonNull IHTTPSession session) {
72-
long requestContentLength = Long.parseLong(Objects.requireNonNull(session.getHeaders().get("content-length")));
73-
return newLimitedInputStream(session.getInputStream(), requestContentLength);
74-
}
49+
for (Map.Entry<String, RouteHandler> entry : routeHandlers) {
50+
String pathPrefix = entry.getKey();
51+
if (!uri.startsWith(pathPrefix)) continue;
7552

76-
private static final Response INTERNAL_ERROR_RESPONSE = newResponse(INTERNAL_ERROR);
77-
78-
@SuppressWarnings("SameParameterValue")
79-
@NonNull
80-
private static Response newResponse(Response.Status status) {
81-
return newResponse(status, null);
82-
}
83-
84-
@NonNull
85-
private static Response newResponse(Response.IStatus status, MessageLite messageLite) {
86-
if (messageLite == null) {
87-
return newFixedLengthResponse(status, "application/x-protobuf", null);
53+
RouteHandler handler = entry.getValue();
54+
return handler.serve(session);
8855
}
8956

90-
byte[] messageBytes = messageLite.toByteArray();
91-
InputStream stream = new ByteArrayInputStream(messageBytes);
92-
return newFixedLengthResponse(status, "application/x-protobuf", stream, messageBytes.length);
57+
// All other requests should only be spclient requests.
58+
return service.serveSpClientRequest(session);
9359
}
60+
61+
static final Response INTERNAL_ERROR_RESPONSE = newFixedLengthResponse(INTERNAL_ERROR, "application/x-protobuf", null);
9462
}

0 commit comments

Comments
 (0)