Add support for fetching remote deprecation.

This commit is contained in:
Greyson Parrelli
2020-09-08 18:03:56 -04:00
committed by GitHub
parent c946a7a1d5
commit 2784285d47
21 changed files with 559 additions and 39 deletions

View File

@@ -0,0 +1,41 @@
package org.thoughtcrime.securesms.net;
import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.logging.Log;
import org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* Disallows network requests when your client has been deprecated. When the client is deprecated,
* we simply fake a 499 response.
*/
public final class DeprecatedClientPreventionInterceptor implements Interceptor {
private static final String TAG = Log.tag(DeprecatedClientPreventionInterceptor.class);
@Override
public @NonNull Response intercept(@NonNull Chain chain) throws IOException {
if (SignalStore.misc().isClientDeprecated()) {
Log.w(TAG, "Preventing request because client is deprecated.");
return new Response.Builder()
.request(chain.request())
.protocol(Protocol.HTTP_1_1)
.receivedResponseAtMillis(System.currentTimeMillis())
.message("")
.body(ResponseBody.create(null, ""))
.code(499)
.build();
} else {
return chain.proceed(chain.request());
}
}
}

View File

@@ -0,0 +1,31 @@
package org.thoughtcrime.securesms.net;
import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.logging.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;
/**
* Marks the client as remotely-deprecated when it receives a 499 response.
*/
public final class RemoteDeprecationDetectorInterceptor implements Interceptor {
private static final String TAG = Log.tag(RemoteDeprecationDetectorInterceptor.class);
@Override
public @NonNull Response intercept(@NonNull Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
if (response.code() == 499 && !SignalStore.misc().isClientDeprecated()) {
Log.w(TAG, "Received 499. Client version is deprecated.");
SignalStore.misc().markDeprecated();
}
return response;
}
}

View File

@@ -10,6 +10,6 @@ import org.thoughtcrime.securesms.BuildConfig;
public class StandardUserAgentInterceptor extends UserAgentInterceptor {
public StandardUserAgentInterceptor() {
super("Signal-Android " + BuildConfig.VERSION_NAME + " (API " + Build.VERSION.SDK_INT + ")");
super("Signal-Android/" + BuildConfig.VERSION_NAME + " Android/" + Build.VERSION.SDK_INT);
}
}