2017-05-05 08:55:34 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* Lesser General Public License as published by the Free Software Foundation, either version 3 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If
|
|
|
|
* not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.util;
|
|
|
|
|
2018-04-23 01:20:01 +10:00
|
|
|
import android.content.Context;
|
2017-12-27 07:45:08 +11:00
|
|
|
import android.content.SharedPreferences;
|
2017-05-05 08:55:34 +10:00
|
|
|
import android.os.Build;
|
2018-04-23 01:20:01 +10:00
|
|
|
import android.preference.PreferenceManager;
|
2017-05-05 08:55:34 +10:00
|
|
|
|
|
|
|
import com.keylesspalace.tusky.BuildConfig;
|
|
|
|
|
2017-12-27 07:45:08 +11:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.Proxy;
|
2017-11-07 23:27:35 +11:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-05-05 08:55:34 +10:00
|
|
|
|
2019-03-05 05:25:51 +11:00
|
|
|
import androidx.annotation.NonNull;
|
2018-04-23 01:20:01 +10:00
|
|
|
import okhttp3.Cache;
|
2017-05-05 08:55:34 +10:00
|
|
|
import okhttp3.Interceptor;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
|
|
|
public class OkHttpUtils {
|
|
|
|
|
|
|
|
@NonNull
|
2018-04-23 01:20:01 +10:00
|
|
|
public static OkHttpClient.Builder getCompatibleClientBuilder(@NonNull Context context) {
|
|
|
|
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
2017-12-27 07:45:08 +11:00
|
|
|
boolean httpProxyEnabled = preferences.getBoolean("httpProxyEnabled", false);
|
|
|
|
String httpServer = preferences.getString("httpProxyServer", "");
|
2018-04-30 16:32:43 +10:00
|
|
|
int httpPort;
|
|
|
|
try {
|
|
|
|
httpPort = Integer.parseInt(preferences.getString("httpProxyPort", "-1"));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
// user has entered wrong port, fall back to no proxy
|
|
|
|
httpPort = -1;
|
|
|
|
}
|
2017-12-27 07:45:08 +11:00
|
|
|
|
2018-07-15 01:12:57 +10:00
|
|
|
int cacheSize = 25*1024*1024; // 25 MiB
|
2018-04-23 01:20:01 +10:00
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
|
|
|
.addInterceptor(getUserAgentInterceptor())
|
2017-11-07 23:27:35 +11:00
|
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
2019-03-05 05:25:51 +11:00
|
|
|
.cache(new Cache(context.getCacheDir(), cacheSize));
|
2017-05-05 08:55:34 +10:00
|
|
|
|
2017-12-27 07:45:08 +11:00
|
|
|
if (httpProxyEnabled && !httpServer.isEmpty() && (httpPort > 0) && (httpPort < 65535)) {
|
|
|
|
InetSocketAddress address = InetSocketAddress.createUnresolved(httpServer, httpPort);
|
|
|
|
builder.proxy(new Proxy(Proxy.Type.HTTP, address));
|
|
|
|
}
|
|
|
|
|
2018-09-17 02:54:12 +10:00
|
|
|
return builder;
|
2017-05-05 08:55:34 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2018-02-01 07:57:50 +11:00
|
|
|
return chain -> {
|
|
|
|
Request originalRequest = chain.request();
|
|
|
|
Request requestWithUserAgent = originalRequest.newBuilder()
|
|
|
|
.header("User-Agent", "Tusky/"+ BuildConfig.VERSION_NAME+" Android/"+Build.VERSION.RELEASE)
|
|
|
|
.build();
|
|
|
|
return chain.proceed(requestWithUserAgent);
|
2017-05-05 08:55:34 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-05 05:25:51 +11:00
|
|
|
|
|
|
|
|