|
| 1 | +package app.revanced.extension.spotify.misc.fix; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.annotation.Nullable; |
| 5 | +import app.revanced.extension.shared.Logger; |
| 6 | +import app.revanced.extension.spotify.login5.v4.proto.Login5.*; |
| 7 | +import com.google.protobuf.ByteString; |
| 8 | +import com.google.protobuf.MessageLite; |
| 9 | +import fi.iki.elonen.NanoHTTPD; |
| 10 | + |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.FilterInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +import static fi.iki.elonen.NanoHTTPD.Response.Status.INTERNAL_ERROR; |
| 18 | + |
| 19 | +class LoginRequestListener extends NanoHTTPD { |
| 20 | + LoginRequestListener(int port) { |
| 21 | + super(port); |
| 22 | + } |
| 23 | + |
| 24 | + @NonNull |
| 25 | + @Override |
| 26 | + public Response serve(IHTTPSession request) { |
| 27 | + Logger.printInfo(() -> "Serving request for URI: " + request.getUri()); |
| 28 | + |
| 29 | + InputStream requestBodyInputStream = getRequestBodyInputStream(request); |
| 30 | + |
| 31 | + LoginRequest loginRequest; |
| 32 | + try { |
| 33 | + loginRequest = LoginRequest.parseFrom(requestBodyInputStream); |
| 34 | + } catch (IOException e) { |
| 35 | + Logger.printException(() -> "Failed to parse LoginRequest", e); |
| 36 | + return newResponse(INTERNAL_ERROR); |
| 37 | + } |
| 38 | + |
| 39 | + MessageLite loginResponse; |
| 40 | + |
| 41 | + // A request may be made concurrently by Spotify, |
| 42 | + // however a webview can only handle one request at a time due to singleton cookie manager. |
| 43 | + // Therefore, synchronize to ensure that only one webview handles the request at a time. |
| 44 | + synchronized (this) { |
| 45 | + loginResponse = getLoginResponse(loginRequest); |
| 46 | + } |
| 47 | + |
| 48 | + if (loginResponse != null) { |
| 49 | + return newResponse(Response.Status.OK, loginResponse); |
| 50 | + } |
| 51 | + |
| 52 | + return newResponse(INTERNAL_ERROR); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Nullable |
| 57 | + private static LoginResponse getLoginResponse(@NonNull LoginRequest loginRequest) { |
| 58 | + Session session; |
| 59 | + |
| 60 | + boolean isInitialLogin = !loginRequest.hasStoredCredential(); |
| 61 | + if (isInitialLogin) { |
| 62 | + Logger.printInfo(() -> "Received request for initial login"); |
| 63 | + session = WebApp.currentSession; // Session obtained from WebApp.login. |
| 64 | + } else { |
| 65 | + Logger.printInfo(() -> "Received request to restore saved session"); |
| 66 | + session = Session.read(loginRequest.getStoredCredential().getUsername()); |
| 67 | + } |
| 68 | + |
| 69 | + return toLoginResponse(session, isInitialLogin); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private static LoginResponse toLoginResponse(Session session, boolean isInitialLogin) { |
| 74 | + LoginResponse.Builder builder = LoginResponse.newBuilder(); |
| 75 | + |
| 76 | + if (session == null) { |
| 77 | + if (isInitialLogin) { |
| 78 | + Logger.printInfo(() -> "Session is null, returning try again later error for initial login"); |
| 79 | + builder.setError(LoginError.TRY_AGAIN_LATER); |
| 80 | + } else { |
| 81 | + Logger.printInfo(() -> "Session is null, returning invalid credentials error for stored credential login"); |
| 82 | + builder.setError(LoginError.INVALID_CREDENTIALS); |
| 83 | + } |
| 84 | + } else if (session.username == null) { |
| 85 | + Logger.printInfo(() -> "Session username is null, returning invalid credentials error"); |
| 86 | + builder.setError(LoginError.INVALID_CREDENTIALS); |
| 87 | + } else if (session.accessTokenExpired()) { |
| 88 | + Logger.printInfo(() -> "Access token has expired, renewing session"); |
| 89 | + WebApp.renewSession(session.cookies); |
| 90 | + return toLoginResponse(WebApp.currentSession, isInitialLogin); |
| 91 | + } else { |
| 92 | + session.save(); |
| 93 | + Logger.printInfo(() -> "Returning session for username: " + session.username); |
| 94 | + builder.setOk(LoginOk.newBuilder() |
| 95 | + .setUsername(session.username) |
| 96 | + .setAccessToken(session.accessToken) |
| 97 | + .setStoredCredential(ByteString.fromHex("00")) // Placeholder, as it cannot be null or empty. |
| 98 | + .setAccessTokenExpiresIn(session.accessTokenExpiresInSeconds()) |
| 99 | + .build()); |
| 100 | + } |
| 101 | + |
| 102 | + return builder.build(); |
| 103 | + } |
| 104 | + |
| 105 | + @NonNull |
| 106 | + private static InputStream limitedInputStream(InputStream inputStream, long contentLength) { |
| 107 | + return new FilterInputStream(inputStream) { |
| 108 | + private long remaining = contentLength; |
| 109 | + |
| 110 | + @Override |
| 111 | + public int read() throws IOException { |
| 112 | + if (remaining <= 0) return -1; |
| 113 | + int result = super.read(); |
| 114 | + if (result != -1) remaining--; |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public int read(byte[] b, int off, int len) throws IOException { |
| 120 | + if (remaining <= 0) return -1; |
| 121 | + len = (int) Math.min(len, remaining); |
| 122 | + int result = super.read(b, off, len); |
| 123 | + if (result != -1) remaining -= result; |
| 124 | + return result; |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + @NonNull |
| 130 | + private static InputStream getRequestBodyInputStream(@NonNull IHTTPSession request) { |
| 131 | + long requestContentLength = |
| 132 | + Long.parseLong(Objects.requireNonNull(request.getHeaders().get("content-length"))); |
| 133 | + return limitedInputStream(request.getInputStream(), requestContentLength); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @SuppressWarnings("SameParameterValue") |
| 138 | + @NonNull |
| 139 | + private static Response newResponse(Response.Status status) { |
| 140 | + return newResponse(status, null); |
| 141 | + } |
| 142 | + |
| 143 | + @NonNull |
| 144 | + private static Response newResponse(Response.IStatus status, MessageLite messageLite) { |
| 145 | + if (messageLite == null) { |
| 146 | + return newFixedLengthResponse(status, "application/x-protobuf", null); |
| 147 | + } |
| 148 | + |
| 149 | + byte[] messageBytes = messageLite.toByteArray(); |
| 150 | + InputStream stream = new ByteArrayInputStream(messageBytes); |
| 151 | + return newFixedLengthResponse(status, "application/x-protobuf", stream, messageBytes.length); |
| 152 | + } |
| 153 | +} |
0 commit comments