Skip to content

Commit

Permalink
Merge pull request #304 from North-Two-Five/bugfix-tlsversion
Browse files Browse the repository at this point in the history
Bugfix for TLS version error (protocol not supported error)
  • Loading branch information
pooyaj authored Jan 5, 2022
2 parents 71be693 + 7da597e commit a9810cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 24 additions & 3 deletions analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import com.segment.analytics.messages.Message;
import com.segment.analytics.messages.MessageBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionSpec;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
Expand Down Expand Up @@ -145,6 +148,7 @@ public static class Builder {
private long flushIntervalInMillis;
private List<Callback> callbacks;
private int queueCapacity;
private boolean forceTlsV1 = false;

Builder(String writeKey) {
if (writeKey == null || writeKey.trim().length() == 0) {
Expand Down Expand Up @@ -329,6 +333,12 @@ public Builder plugin(Plugin plugin) {
return this;
}

/** Specify if need TlsV1 */
public Builder forceTlsVersion1() {
forceTlsV1 = true;
return this;
}

/** Create a {@link Analytics} client. */
public Analytics build() {
Gson gson =
Expand Down Expand Up @@ -400,12 +410,23 @@ public void log(String message) {

interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

client =
OkHttpClient.Builder builder =
client
.newBuilder()
.addInterceptor(new AnalyticsRequestInterceptor(writeKey, userAgent))
.addInterceptor(interceptor)
.build();
.addInterceptor(interceptor);

if (forceTlsV1) {
ConnectionSpec connectionSpec =
new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(
TlsVersion.TLS_1_0, TlsVersion.TLS_1_1, TlsVersion.TLS_1_2, TlsVersion.TLS_1_3)
.build();

builder = builder.connectionSpecs(Arrays.asList(connectionSpec));
}

client = builder.build();

Retrofit restAdapter =
new Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ public void buildsWithValidCallback() {
assertThat(analytics).isNotNull();
}

@Test
public void buildsWithForceTlsV1() {
Analytics analytics = builder.forceTlsVersion1().build();
assertThat(analytics).isNotNull();
}

@Test
public void multipleCallbacks() {
Analytics analytics =
Expand Down

0 comments on commit a9810cc

Please sign in to comment.