2
2
3
3
import androidx .annotation .NonNull ;
4
4
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 ;
7
5
import fi .iki .elonen .NanoHTTPD ;
8
6
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 .*;
14
9
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 ;
17
10
import static fi .iki .elonen .NanoHTTPD .Response .Status .INTERNAL_ERROR ;
18
11
19
12
class RequestListener extends NanoHTTPD {
13
+ private final SpoofClientService service ;
14
+
15
+ private final Set <Map .Entry <String , RouteHandler >> routeHandlers ;
16
+
20
17
RequestListener (int port ) {
21
18
super (port );
22
19
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
+
23
29
try {
24
30
start ();
25
31
} catch (IOException ex ) {
@@ -28,67 +34,29 @@ class RequestListener extends NanoHTTPD {
28
34
}
29
35
}
30
36
37
+ interface RouteHandler {
38
+ @ NonNull
39
+ Response serve (@ NonNull IHTTPSession session );
40
+ }
41
+
42
+
31
43
@ NonNull
32
44
@ Override
33
45
public Response serve (@ NonNull IHTTPSession session ) {
34
46
String uri = session .getUri ();
35
- if (!uri .equals (CLIENT_TOKEN_API_PATH )) return INTERNAL_ERROR_RESPONSE ;
36
-
37
47
Logger .printInfo (() -> "Serving request for URI: " + uri );
38
48
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 ;
75
52
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 );
88
55
}
89
56
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 );
93
59
}
60
+
61
+ static final Response INTERNAL_ERROR_RESPONSE = newFixedLengthResponse (INTERNAL_ERROR , "application/x-protobuf" , null );
94
62
}
0 commit comments