Update okhttp and support for transmitting messages over websocket

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2017-01-09 07:49:37 -08:00
parent ac2626437e
commit ec5ac44cd3
7 changed files with 50 additions and 35 deletions

View File

@@ -1,11 +1,5 @@
package org.thoughtcrime.redphone.signaling;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.whispersystems.libsignal.util.guava.Optional;
@@ -22,6 +16,13 @@ import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class RedPhoneAccountManager {
@@ -39,7 +40,9 @@ public class RedPhoneAccountManager {
this.baseUrl = baseUrl;
this.login = login;
this.password = password;
this.client = new OkHttpClient().setSslSocketFactory(context.getSocketFactory());
this.client = new OkHttpClient.Builder()
.sslSocketFactory(context.getSocketFactory(), (X509TrustManager)trustManagers[0])
.build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new AssertionError(e);
}

View File

@@ -86,6 +86,7 @@ public class SignalCommunicationModule {
TextSecurePreferences.getPushServerPassword(context),
new SignalProtocolStoreImpl(context),
BuildConfig.USER_AGENT,
Optional.fromNullable(MessageRetrievalService.getPipe()),
Optional.<SignalServiceMessageSender.EventListener>of(new SecurityEventListener(context)));
}
};

View File

@@ -8,9 +8,6 @@ import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.thoughtcrime.securesms.giph.model.GiphyImage;
import org.thoughtcrime.securesms.giph.model.GiphyResponse;
@@ -21,6 +18,10 @@ import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
private static final String TAG = GiphyLoader.class.getName();
@@ -29,12 +30,12 @@ public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
@Nullable private String searchString;
private final OkHttpClient client = new OkHttpClient();
private final OkHttpClient client;
protected GiphyLoader(@NonNull Context context, @Nullable String searchString) {
super(context);
this.searchString = searchString;
this.client.setProxySelector(new GiphyProxySelector());
this.client = new OkHttpClient.Builder().proxySelector(new GiphyProxySelector()).build();
}
@Override

View File

@@ -4,23 +4,27 @@ import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* Fetches an {@link InputStream} using the okhttp library.
*/
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private static final String TAG = OkHttpStreamFetcher.class.getName();
private final OkHttpClient client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
this.client = client;
@@ -37,10 +41,11 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
responseBody = response.body();
if (!response.isSuccessful()) {
throw new IOException("Request failed with code: " + response.code());
}
@@ -60,11 +65,7 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
}
}
if (responseBody != null) {
try {
responseBody.close();
} catch (IOException e) {
// Ignored.
}
responseBody.close();
}
}

View File

@@ -7,12 +7,13 @@ import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.squareup.okhttp.OkHttpClient;
import org.thoughtcrime.securesms.giph.net.GiphyProxySelector;
import java.io.InputStream;
import okhttp3.OkHttpClient;
/**
* A simple model loader for fetching media over http/https using OkHttp.
*/
@@ -29,8 +30,9 @@ public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
if (internalClient == null) {
synchronized (Factory.class) {
if (internalClient == null) {
internalClient = new OkHttpClient();
internalClient.setProxySelector(new GiphyProxySelector());
internalClient = new OkHttpClient.Builder()
.proxySelector(new GiphyProxySelector())
.build();
}
}
}

View File

@@ -4,6 +4,7 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import org.thoughtcrime.securesms.ApplicationContext;
@@ -44,6 +45,8 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
private int activeActivities = 0;
private List<Intent> pushPending = new LinkedList<>();
public static SignalServiceMessagePipe pipe = null;
@Override
public void onCreate() {
super.onCreate();
@@ -73,7 +76,7 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
waitForConnectionNecessary();
Log.w(TAG, "Making websocket connection....");
SignalServiceMessagePipe pipe = receiver.createMessagePipe();
pipe = receiver.createMessagePipe();
try {
while (isConnectionNecessary()) {
@@ -181,4 +184,8 @@ public class MessageRetrievalService extends Service implements Runnable, Inject
intent.setAction(MessageRetrievalService.ACTION_ACTIVITY_FINISHED);
activity.startService(intent);
}
public static @Nullable SignalServiceMessagePipe getPipe() {
return pipe;
}
}