Add a custom User-Agent that contains Tusky & Android Version to all requests

This commit is contained in:
Konrad Pozniak 2017-04-20 18:45:36 +02:00
parent a33c259760
commit ea2018cf02

View file

@ -37,7 +37,10 @@ import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import okhttp3.ConnectionSpec; import okhttp3.ConnectionSpec;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
class OkHttpUtils { class OkHttpUtils {
static final String TAG = "OkHttpUtils"; // logging tag static final String TAG = "OkHttpUtils"; // logging tag
@ -69,6 +72,7 @@ class OkHttpUtils {
specList.add(ConnectionSpec.CLEARTEXT); specList.add(ConnectionSpec.CLEARTEXT);
OkHttpClient.Builder builder = new OkHttpClient.Builder() OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(getUserAgentInterceptor())
.connectionSpecs(specList); .connectionSpecs(specList);
return enableHigherTlsOnPreLollipop(builder); return enableHigherTlsOnPreLollipop(builder);
@ -79,6 +83,26 @@ class OkHttpUtils {
return getCompatibleClientBuilder().build(); return getCompatibleClientBuilder().build();
} }
/**
* Add a custom User-Agent that contains Tusky & Android Version to all requests
* Example:
* User-Agent: Tusky/1.1.2 Android/5.0.2
*/
@NonNull
private static Interceptor getUserAgentInterceptor() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", "Tusky/"+BuildConfig.VERSION_NAME+" Android/"+Build.VERSION.RELEASE)
.build();
return chain.proceed(requestWithUserAgent);
}
};
}
/** /**
* Android version Nougat has a regression where elliptic curve cipher suites are supported, but * Android version Nougat has a regression where elliptic curve cipher suites are supported, but
* only the curve secp256r1 is allowed. So, first it's best to just disable all elliptic * only the curve secp256r1 is allowed. So, first it's best to just disable all elliptic
@ -194,7 +218,7 @@ class OkHttpUtils {
@Override @Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
int localPort) throws IOException { int localPort) throws IOException {
return patch(delegate.createSocket(address, port, localAddress, localPort)); return patch(delegate.createSocket(address, port, localAddress, localPort));
} }