Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ServiceGenerator.java
package com.wifi.reader.network.service;
public class ServiceGenerator {
private static final java.math.BigDecimal BD32 = new java.math.BigDecimal(32);
private static final okhttp3.Cache CACHE = new okhttp3.Cache(com.wifi.reader.application.WKRApplication.c().getCacheDir(), 10485760);
public static final okhttp3.OkHttpClient HTTP_CLIENT = new okhttp3.OkHttpClient.Builder().connectTimeout(5, java.util.concurrent.TimeUnit.SECONDS).readTimeout(60, java.util.concurrent.TimeUnit.SECONDS).addInterceptor(REQUEST_INTERCEPTOR).addNetworkInterceptor(RESPONSE_INTERCEPTOR).sslSocketFactory(com.wifi.reader.glide.https.SSLSocketClient.getSSLSocketFactory()).hostnameVerifier(com.wifi.reader.glide.https.SSLSocketClient.getHostnameVerifier()).cache(CACHE).retryOnConnectionFailure(true).build();
private static final okhttp3.logging.HttpLoggingInterceptor LOGGING = new okhttp3.logging.HttpLoggingInterceptor().setLevel(okhttp3.logging.HttpLoggingInterceptor.Level.HEADERS);
private static final okhttp3.Interceptor REQUEST_INTERCEPTOR = new okhttp3.Interceptor() {
public okhttp3.Response intercept(okhttp3.Interceptor.Chain chain) throws java.io.IOException {
java.lang.CharSequence charSequence;
java.lang.Object obj = null;
okhttp3.Request request = chain.request();
if (request == null || request.url() == null || android.text.TextUtils.isEmpty(request.url().host())) {
charSequence = null;
} else {
charSequence = request.url().host();
}
if (android.text.TextUtils.isEmpty(charSequence)) {
return chain.proceed(request);
}
okhttp3.Request.Builder newBuilder = request.newBuilder();
try {
org.json.JSONObject jSONObject = new org.json.JSONObject();
jSONObject.put("net_type", com.wifi.reader.util.y.b(com.wifi.reader.application.WKRApplication.c()));
jSONObject.put("net_operator", com.wifi.reader.util.y.b());
newBuilder.addHeader("common", com.wifi.reader.crypto.Rsa.encryptN(jSONObject.toString()));
} catch (java.lang.Exception e) {
}
java.lang.String str = "";
java.lang.String str2;
java.lang.String requestBodyString;
if (android.text.TextUtils.isEmpty("http://adx.readdsp.com/") || !"http://adx.readdsp.com/".contains(charSequence)) {
java.lang.String httpUrl = request.url().toString();
if (!com.wifi.reader.util.af.d(request.url().toString())) {
if (httpUrl.contains("http://readfree.zhulang.com/")) {
str = httpUrl.replace("http://readfree.zhulang.com/", "");
} else if (httpUrl.contains("http://106.75.34.36/")) {
str = httpUrl.replace("http://106.75.34.36/", "");
}
}
newBuilder.addHeader("accept", "application/json");
newBuilder.addHeader("User-Agent", "official.f1.2.5.0370c92.20190610091415");
newBuilder.addHeader("channel", com.wifi.reader.config.e.m());
android.util.Log.d(com.wifi.reader.network.service.ServiceGenerator.TAG, "current channel: " + com.wifi.reader.config.e.m());
newBuilder.addHeader("X-Version", "0370c92");
newBuilder.addHeader("version-code", "190521");
if (com.wifi.reader.config.User.a().p() != null) {
obj = com.wifi.reader.config.User.a().p().id;
}
if (obj == null) {
str2 = cm.pass.sdk.UMCSDK.AUTH_TYPE_NONE;
} else {
java.lang.Object a = com.wifi.reader.util.CryptoUtils.a(java.lang.String.valueOf(obj));
if (android.text.TextUtils.isEmpty(a)) {
str2 = cm.pass.sdk.UMCSDK.AUTH_TYPE_NONE;
} else {
str2 = a.substring(0, 2);
}
}
newBuilder.addHeader("umd", str2);
com.wifi.reader.util.x.b(com.wifi.reader.network.service.ServiceGenerator.TAG, "header token: " + com.wifi.reader.config.User.a().c() + ", url: " + request.url().toString());
newBuilder.addHeader("X-Token", com.wifi.reader.config.User.a().c());
newBuilder.addHeader("package", com.wifi.reader.application.WKRApplication.c().getPackageName());
if (com.wifi.reader.config.a.a().b() && httpUrl.startsWith("http://106.75.34.36/")) {
newBuilder.addHeader("host", "readfree.zhulang.com");
}
if (!"freerelease".isEmpty() && "freerelease".contains("stable")) {
newBuilder.addHeader("host", "readfree.zhulang.com");
}
okhttp3.CacheControl cacheControl = request.cacheControl();
if (cacheControl == null || cacheControl.maxAgeSeconds() <= 0) {
newBuilder.cacheControl(okhttp3.CacheControl.FORCE_NETWORK);
}
if (request.method().equals("POST")) {
okhttp3.RequestBody body = request.body();
okhttp3.MediaType contentType = body.contentType();
if (contentType == null || !contentType.type().equals("multipart")) {
requestBodyString = getRequestBodyString(body);
if (!android.text.TextUtils.isEmpty(requestBodyString)) {
requestBodyString = com.wifi.reader.crypto.Rsa.encryptN(requestBodyString);
}
newBuilder.addHeader("X-Validate", getValidateCode(requestBodyString));
newBuilder.post(okhttp3.RequestBody.create(body.contentType(), requestBodyString));
} else {
java.util.List<okhttp3.MultipartBody.Part> parts = ((okhttp3.MultipartBody) body).parts();
java.util.List<java.lang.String> arrayList = new java.util.ArrayList();
for (okhttp3.MultipartBody.Part headers : parts) {
okhttp3.Headers headers2 = headers.headers();
if (headers2 != null) {
arrayList.add(getFileNameFromHeaders(headers2.get("Content-Disposition")));
}
}
java.util.Collections.sort(arrayList);
java.lang.StringBuilder stringBuilder = new java.lang.StringBuilder();
for (java.lang.String str22 : arrayList) {
stringBuilder.append(str22);
}
newBuilder.addHeader("X-Validate", getValidateCode(stringBuilder.toString()));
}
} else if (request.method().equals("GET")) {
newBuilder.addHeader("X-Validate", getValidateCode(request.url().encodedPath()));
}
} else {
newBuilder.addHeader("Content-Type", "application/json");
if (com.wifi.reader.util.af.d(request.url().toString())) {
str22 = str;
} else {
str22 = request.url().toString().replace("http://adx.readdsp.com/", "");
}
if (request.method().equals("POST")) {
okhttp3.RequestBody body2 = request.body();
requestBodyString = getRequestBodyString(body2);
if (!android.text.TextUtils.isEmpty(requestBodyString)) {
requestBodyString = com.wifi.reader.crypto.Rsa.encryptNAD(requestBodyString);
}
newBuilder.post(okhttp3.RequestBody.create(body2.contentType(), requestBodyString));
str = str22;
} else {
str = str22;
}
}
okhttp3.Request build = newBuilder.build();
long nanoTime = java.lang.System.nanoTime();
okhttp3.Response proceed = chain.proceed(build);
nanoTime = java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(java.lang.System.nanoTime() - nanoTime);
if (proceed == null) {
return proceed;
}
try {
if (com.wifi.reader.util.af.d(str) || str.contains("v1/stat")) {
return proceed;
}
org.json.JSONObject jSONObject2 = new org.json.JSONObject();
jSONObject2.put("request_path", str);
jSONObject2.put("consume_time", nanoTime);
jSONObject2.put("response_code", proceed.code());
com.wifi.reader.h.e.a().a(null, null, null, "wkr2701084", -1, null, java.lang.System.currentTimeMillis(), jSONObject2);
return proceed;
} catch (java.lang.Exception e2) {
return proceed;
}
}
private java.lang.String getRequestBodyString(okhttp3.RequestBody requestBody) {
if (requestBody == null) {
return "";
}
java.lang.Object buffer = new okio.Buffer();
try {
requestBody.writeTo(buffer);
return buffer.readUtf8();
} catch (java.io.IOException e) {
return "";
}
}
private java.lang.String getFileNameFromHeaders(java.lang.String str) {
if (android.text.TextUtils.isEmpty(str)) {
return "";
}
java.lang.String[] split = str.replace(" ", "").split(";");
if (split == null) {
return "";
}
for (java.lang.String str2 : split) {
if (!android.text.TextUtils.isEmpty(str2) && str2.startsWith("filename=")) {
return str2.replace("\"", "").substring(9);
}
}
return "";
}
private java.lang.String getValidateCode(java.lang.String str) {
java.lang.Object c = com.wifi.reader.config.User.a().c();
java.lang.Object e = com.wifi.reader.config.User.a().e();
try {
e = c32to10(e);
} catch (java.lang.Throwable e2) {
android.util.Log.e(com.wifi.reader.network.service.ServiceGenerator.TAG, "decode device id failed", e2);
}
java.lang.StringBuilder stringBuilder = new java.lang.StringBuilder();
if (!android.text.TextUtils.isEmpty(c)) {
stringBuilder.append(c);
}
if (!android.text.TextUtils.isEmpty(e)) {
stringBuilder.append(e);
}
if (!android.text.TextUtils.isEmpty(str)) {
stringBuilder.append(str);
}
return com.wifi.reader.util.CryptoUtils.a(stringBuilder.toString());
}
private java.lang.String c32to10(java.lang.String str) {
int i = 0;
if (android.text.TextUtils.isEmpty(str)) {
return "";
}
java.lang.StringBuilder stringBuilder = new java.lang.StringBuilder(str);
stringBuilder.reverse();
char[] toCharArray = stringBuilder.toString().toCharArray();
java.math.BigDecimal bigDecimal = new java.math.BigDecimal(0);
int length = toCharArray.length;
int i2 = 0;
while (i < length) {
bigDecimal = bigDecimal.add(new java.math.BigDecimal(com.wifi.reader.network.service.ServiceGenerator.TABLE_32.indexOf(toCharArray[i])).multiply(com.wifi.reader.network.service.ServiceGenerator.BD32.pow(i2)));
i2++;
i++;
}
return java.lang.String.valueOf(bigDecimal.longValue());
}
};
private static final okhttp3.Interceptor RESPONSE_INTERCEPTOR = new okhttp3.Interceptor() {
public okhttp3.Response intercept(okhttp3.Interceptor.Chain chain) throws java.io.IOException {
okhttp3.Request request = chain.request();
java.lang.CharSequence charSequence = null;
if (!(request == null || request.url() == null)) {
charSequence = request.url().host();
}
if (android.text.TextUtils.isEmpty(charSequence) || ((android.text.TextUtils.isEmpty("http://readfree.zhulang.com/") || !"http://readfree.zhulang.com/".contains(charSequence)) && (android.text.TextUtils.isEmpty("http://106.75.34.36/") || !"http://106.75.34.36/".contains(charSequence)))) {
return chain.proceed(request);
}
java.lang.String header = request.header("Cache-Control");
okhttp3.Response proceed = chain.proceed(request);
if (proceed.header("Cache-Control") == null && proceed.code() == 200) {
return proceed.newBuilder().removeHeader("Pragma").removeHeader("Cache-Control").header("Cache-Control", header).header("Cache-Intercept", "local").build();
}
return proceed.newBuilder().addHeader("Cache-Intercept", "none").build();
}
};
public static final okhttp3.OkHttpClient SHORTHTTP_CLIENT = new okhttp3.OkHttpClient.Builder().connectTimeout(5, java.util.concurrent.TimeUnit.SECONDS).readTimeout(20, java.util.concurrent.TimeUnit.SECONDS).addInterceptor(REQUEST_INTERCEPTOR).addNetworkInterceptor(RESPONSE_INTERCEPTOR).sslSocketFactory(com.wifi.reader.glide.https.SSLSocketClient.getSSLSocketFactory()).hostnameVerifier(com.wifi.reader.glide.https.SSLSocketClient.getHostnameVerifier()).cache(CACHE).retryOnConnectionFailure(true).build();
private static final java.lang.String TABLE_32 = "0123456789abcdefghijklmnopqrstuv";
private static final java.lang.String TAG = "ServiceGenerator";
private static retrofit2.Retrofit adRetrofit;
private static retrofit2.Retrofit ipRetrofit;
private static retrofit2.Retrofit retrofit;
private static final okhttp3.OkHttpClient sSimpleOkHttpClient = new okhttp3.OkHttpClient.Builder().connectTimeout(5, java.util.concurrent.TimeUnit.SECONDS).readTimeout(60, java.util.concurrent.TimeUnit.SECONDS).build();
private static retrofit2.Retrofit shortRetrofit;
private ServiceGenerator() {
}
public static okhttp3.OkHttpClient getAdSimpleOkHttpClient() {
return sSimpleOkHttpClient;
}
protected static <S> S createService(java.lang.Class<S> cls) {
if (com.wifi.reader.config.a.a().b()) {
initIpRetrofit();
return ipRetrofit.create(cls);
}
initRetrofit();
return retrofit.create(cls);
}
protected static <S> S createService(java.lang.Class<S> cls, boolean z) {
if (!z) {
return createService(cls);
}
if (com.wifi.reader.config.a.a().b()) {
initIpRetrofit();
return ipRetrofit.create(cls);
}
initShortRetrofit();
return shortRetrofit.create(cls);
}
private static void initShortRetrofit() {
if (shortRetrofit == null) {
shortRetrofit = new retrofit2.Retrofit.Builder().baseUrl("http://readfree.zhulang.com/").client(SHORTHTTP_CLIENT).addConverterFactory(com.wifi.reader.f.e.a()).build();
}
}
private static void initRetrofit() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder().baseUrl("http://readfree.zhulang.com/").client(HTTP_CLIENT).addConverterFactory(com.wifi.reader.f.e.a()).build();
}
}
private static void initIpRetrofit() {
if (ipRetrofit == null) {
ipRetrofit = new retrofit2.Retrofit.Builder().baseUrl("http://106.75.34.36/").client(HTTP_CLIENT).addConverterFactory(com.wifi.reader.f.e.a()).build();
}
}
protected static <S> S createAdService(java.lang.Class<S> cls) {
if (adRetrofit != null) {
return adRetrofit.create(cls);
}
adRetrofit = new retrofit2.Retrofit.Builder().baseUrl("http://adx.readdsp.com/").client(HTTP_CLIENT).addConverterFactory(com.wifi.reader.f.a.a.a()).build();
return adRetrofit.create(cls);
}
public static void clearCache() {
try {
CACHE.evictAll();
} catch (java.io.IOException e) {
}
}
public static long getCacheSize() {
try {
return CACHE.size();
} catch (java.io.IOException e) {
return 0;
}
}
public static void evict(java.lang.String str) {
com.wifi.reader.util.x.b(TAG, "evict: " + str);
if (!android.text.TextUtils.isEmpty(str)) {
try {
java.util.Iterator urls = CACHE.urls();
while (urls.hasNext()) {
android.net.Uri parse = android.net.Uri.parse((java.lang.String) urls.next());
if (str.equals(parse.getPath())) {
urls.remove();
com.wifi.reader.util.x.b(TAG, parse + "has removed");
return;
}
}
} catch (java.lang.Throwable e) {
android.util.Log.e(TAG, "evict cache failed, url: " + str, e);
}
}
}
public static boolean hasCache(java.lang.String str) {
if (android.text.TextUtils.isEmpty(str)) {
return false;
}
try {
java.util.Iterator urls = CACHE.urls();
while (urls.hasNext()) {
if (str.equals(android.net.Uri.parse((java.lang.String) urls.next()).getPath())) {
return true;
}
}
} catch (java.lang.Exception e) {
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.