Search in sources :

Example 21 with Dns

use of okhttp3.Dns in project Signal-Android by signalapp.

the class SubmitDebugLogRepository method uploadContent.

@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
    try {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
        Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
        ResponseBody body = response.body();
        if (!response.isSuccessful() || body == null) {
            throw new IOException("Unsuccessful response: " + response);
        }
        JSONObject json = new JSONObject(body.string());
        String url = json.getString("url");
        JSONObject fields = json.getJSONObject("fields");
        String item = fields.getString("key");
        MultipartBody.Builder post = new MultipartBody.Builder();
        Iterator<String> keys = fields.keys();
        post.addFormDataPart("Content-Type", contentType);
        while (keys.hasNext()) {
            String key = keys.next();
            post.addFormDataPart(key, fields.getString(key));
        }
        post.addFormDataPart("file", "file", requestBody);
        Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
        if (!postResponse.isSuccessful()) {
            throw new IOException("Bad response: " + postResponse);
        }
        return API_ENDPOINT + "/" + item;
    } catch (JSONException e) {
        Log.w(TAG, "Error during upload.", e);
        throw new IOException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) JSONObject(org.json.JSONObject) StandardUserAgentInterceptor(org.thoughtcrime.securesms.net.StandardUserAgentInterceptor) MultipartBody(okhttp3.MultipartBody) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 22 with Dns

use of okhttp3.Dns in project Signal-Android by signalapp.

the class PushServiceSocket method createConnectionClient.

private static OkHttpClient createConnectionClient(SignalUrl url, List<Interceptor> interceptors, Optional<Dns> dns, Optional<SignalProxy> proxy) {
    try {
        TrustManager[] trustManagers = BlacklistingTrustManager.createFor(url.getTrustStore());
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, null);
        OkHttpClient.Builder builder = new OkHttpClient.Builder().sslSocketFactory(new Tls12SocketFactory(context.getSocketFactory()), (X509TrustManager) trustManagers[0]).connectionSpecs(url.getConnectionSpecs().or(Util.immutableList(ConnectionSpec.RESTRICTED_TLS))).dns(dns.or(Dns.SYSTEM));
        if (proxy.isPresent()) {
            builder.socketFactory(new TlsProxySocketFactory(proxy.get().getHost(), proxy.get().getPort(), dns));
        }
        builder.sslSocketFactory(new Tls12SocketFactory(context.getSocketFactory()), (X509TrustManager) trustManagers[0]).connectionSpecs(url.getConnectionSpecs().or(Util.immutableList(ConnectionSpec.RESTRICTED_TLS))).build();
        builder.connectionPool(new ConnectionPool(5, 45, TimeUnit.SECONDS));
        for (Interceptor interceptor : interceptors) {
            builder.addInterceptor(interceptor);
        }
        return builder.build();
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new AssertionError(e);
    }
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) SSLContext(javax.net.ssl.SSLContext) Tls12SocketFactory(org.whispersystems.signalservice.api.util.Tls12SocketFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) X509TrustManager(javax.net.ssl.X509TrustManager) BlacklistingTrustManager(org.whispersystems.signalservice.internal.util.BlacklistingTrustManager) TrustManager(javax.net.ssl.TrustManager) Interceptor(okhttp3.Interceptor) TlsProxySocketFactory(org.whispersystems.signalservice.api.util.TlsProxySocketFactory)

Example 23 with Dns

use of okhttp3.Dns in project Signal-Android by signalapp.

the class WebSocketConnection method connect.

public synchronized Observable<WebSocketConnectionState> connect() {
    log("connect()");
    if (client == null) {
        String filledUri;
        if (credentialsProvider.isPresent()) {
            String identifier = Objects.requireNonNull(credentialsProvider.get().getAci()).toString();
            if (credentialsProvider.get().getDeviceId() != SignalServiceAddress.DEFAULT_DEVICE_ID) {
                identifier += "." + credentialsProvider.get().getDeviceId();
            }
            filledUri = String.format(wsUri, identifier, credentialsProvider.get().getPassword());
        } else {
            filledUri = wsUri;
        }
        Pair<SSLSocketFactory, X509TrustManager> socketFactory = createTlsSocketFactory(trustStore);
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().sslSocketFactory(new Tls12SocketFactory(socketFactory.first()), socketFactory.second()).connectionSpecs(Util.immutableList(ConnectionSpec.RESTRICTED_TLS)).readTimeout(KEEPALIVE_TIMEOUT_SECONDS + 10, TimeUnit.SECONDS).dns(dns.or(Dns.SYSTEM)).connectTimeout(KEEPALIVE_TIMEOUT_SECONDS + 10, TimeUnit.SECONDS);
        for (Interceptor interceptor : interceptors) {
            clientBuilder.addInterceptor(interceptor);
        }
        if (signalProxy.isPresent()) {
            clientBuilder.socketFactory(new TlsProxySocketFactory(signalProxy.get().getHost(), signalProxy.get().getPort(), dns));
        }
        OkHttpClient okHttpClient = clientBuilder.build();
        Request.Builder requestBuilder = new Request.Builder().url(filledUri);
        if (signalAgent != null) {
            requestBuilder.addHeader("X-Signal-Agent", signalAgent);
        }
        webSocketState.onNext(WebSocketConnectionState.CONNECTING);
        this.client = okHttpClient.newWebSocket(requestBuilder.build(), this);
    }
    return webSocketState;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) Request(okhttp3.Request) ByteString(okio.ByteString) Tls12SocketFactory(org.whispersystems.signalservice.api.util.Tls12SocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Interceptor(okhttp3.Interceptor) TlsProxySocketFactory(org.whispersystems.signalservice.api.util.TlsProxySocketFactory)

Example 24 with Dns

use of okhttp3.Dns in project okhttp by square.

the class AddressTest method differentProxySelectorsAreDifferent.

@Test
public void differentProxySelectorsAreDifferent() throws Exception {
    Address a = new Address("square.com", 80, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, new RecordingProxySelector());
    Address b = new Address("square.com", 80, dns, socketFactory, null, null, null, authenticator, null, protocols, connectionSpecs, new RecordingProxySelector());
    assertFalse(a.equals(b));
}
Also used : RecordingProxySelector(okhttp3.internal.http.RecordingProxySelector) Test(org.junit.Test)

Example 25 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method recoverFromTlsHandshakeFailure.

@Test
public void recoverFromTlsHandshakeFailure() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).dns(new SingleInetAddressDns()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
    executeSynchronously("/").assertBody("abc");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 MockResponse (okhttp3.mockwebserver.MockResponse)13 OkHttpClient (okhttp3.OkHttpClient)12 DoubleInetAddressDns (okhttp3.internal.DoubleInetAddressDns)10 Test (org.junit.jupiter.api.Test)9 IOException (java.io.IOException)8 Request (okhttp3.Request)8 SingleInetAddressDns (okhttp3.internal.SingleInetAddressDns)8 InetAddress (java.net.InetAddress)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 MockResponse (mockwebserver3.MockResponse)6 Response (okhttp3.Response)6 HttpURLConnection (java.net.HttpURLConnection)5 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 List (java.util.List)4 X509TrustManager (javax.net.ssl.X509TrustManager)4 Dns (okhttp3.Dns)4 Interceptor (okhttp3.Interceptor)4 HandshakeCertificates (okhttp3.tls.HandshakeCertificates)4