WeChat.apk(点击下载) / ConfigStorage.java


package com.tencent.qqvideo.proxy.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import com.tencent.qqvideo.proxy.httpproxy.HttpproxyFacade;
import com.tencent.thumbplayer.api.TPPlayerMgr;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.json.JSONException;
import org.json.JSONObject;

public class ConfigStorage {
    private static String Config_Version = "";
    private static final long GET_CONFIG_INTERVAL = 600000;
    private static long LastGetConfigTime = 0;
    private static final String PREFERENCE = "_httpproxy_preferences";
    public static final String PREF_DOWNPROXY_CONFIG = "DOWNPROXY_CONFIG";
    public static final String PREF_DOWNPROXY_CONFIG_VERSION = "DOWNPROXY_GUID_CONFIG_VERSION";
    private static String TAG = "TV_Httpproxy";
    private static final String TVHttpProxy_Version = "V1.0.186.0011";
    private static final String TVHttpProxy_Version_Code = "101860011";
    private static final String TVHttpProxy_channel_id = "186";
    private static final int[] connectTimeOut = {5000, 5000, 10000};
    private static String guid = "123";
    private static final String httpsCrtFileName = "videosdkcrt/sec.video.qq.com.crt";
    private static final String ipflag = "1";
    private static final String otype = "json";
    private static final String platform = "aphone";
    private static final int[] readTimeOut = {10000, 10000, 15000};
    private static final int[] retryTimes = {3, 2};
    private static String sharedPreferencesName = null;
    private static final String url = "https://sec.video.qq.com/p/wxconf/getmfomat";
    private Thread thread = null;

    /* JADX WARNING: Can't wrap try/catch for region: R(5:73|(2:26|27)|28|29|30) */
    /* JADX WARNING: Can't wrap try/catch for region: R(6:(2:33|34)|35|36|37|75|61) */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:28:0x00d6 */
    /* JADX WARNING: Missing exception handler attribute for start block: B:35:0x00df */
    /* JADX WARNING: Missing exception handler attribute for start block: B:44:0x00ec */
    /* JADX WARNING: Removed duplicated region for block: B:47:0x00f5 A[SYNTHETIC, Splitter:B:47:0x00f5] */
    /* JADX WARNING: Removed duplicated region for block: B:57:0x0117 A[SYNTHETIC, Splitter:B:57:0x0117] */
    /* JADX WARNING: Removed duplicated region for block: B:76:0x011d A[SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:77:0x011d A[SYNTHETIC] */
    private String fetchTextFromUrl(String str, Context context) {
        String str2;
        SSLContext httpsSSLContext = getHttpsSSLContext(context);
        if (httpsSSLContext == null) {
            return "";
        }
        InputStream inputStream = 0;
        InputStream inputStream2 = inputStream;
        for (int i15 = 0; i15 < 2; i15++) {
            if (i15 == 1) {
                str2 = getBkDomain(str);
                String str3 = TAG;
                HttpproxyFacade.print(4, str3, "请求备份域名:" + str2);
            } else {
                str2 = str;
            }
            for (int i16 = 0; i16 < retryTimes[i15]; i16++) {
                try {
                    String str4 = TAG;
                    HttpproxyFacade.print(4, str4, " 第" + (i16 + 1) + " 次CGI请求:" + str2);
                    long currentTimeMillis = System.currentTimeMillis();
                    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(str2).openConnection();
                    httpsURLConnection.setConnectTimeout(connectTimeOut[i16]);
                    httpsURLConnection.setReadTimeout(readTimeOut[i16]);
                    httpsURLConnection.setSSLSocketFactory(httpsSSLContext.getSocketFactory());
                    inputStream = httpsURLConnection.getInputStream();
                    StringBuilder sb5 = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                    while (true) {
                        try {
                            String readLine = bufferedReader.readLine();
                            if (readLine == null) {
                                break;
                            }
                            sb5.append(readLine);
                        } catch (IOException unused) {
                            inputStream2 = bufferedReader;
                            String str5 = TAG;
                            HttpproxyFacade.print(6, str5, "IO Exception" + System.err);
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (IOException unused2) {
                                }
                            }
                            if (inputStream2 == null) {
                            }
                        } catch (Throwable unused3) {
                            inputStream2 = bufferedReader;
                            try {
                                HttpproxyFacade.print(6, TAG, "unknow exception");
                                if (inputStream2 == null) {
                                }
                            } finally {
                                if (inputStream != null) {
                                    try {
                                        inputStream.close();
                                    } catch (IOException unused4) {
                                    }
                                }
                                if (inputStream2 != null) {
                                    try {
                                        inputStream2.close();
                                    } catch (IOException unused5) {
                                    }
                                }
                            }
                        }
                    }
                    String sb6 = sb5.toString();
                    String str6 = TAG;
                    HttpproxyFacade.print(4, str6, "【connect】连接时间:" + (System.currentTimeMillis() - currentTimeMillis) + "返回数据:" + sb6);
                    if (TextUtils.isEmpty(sb6) || sb6.trim().isEmpty()) {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        bufferedReader.close();
                        inputStream2 = bufferedReader;
                    } else {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        bufferedReader.close();
                        return sb6;
                    }
                } catch (IOException unused6) {
                    String str52 = TAG;
                    HttpproxyFacade.print(6, str52, "IO Exception" + System.err);
                    if (inputStream != null) {
                    }
                    if (inputStream2 == null) {
                    }
                } catch (Throwable unknown) {
                    HttpproxyFacade.print(6, TAG, "unknow exception");
                }
            }
        }
        return "";
    }

    public static SharedPreferences getAppSharedPreferences(Context context) {
        if (sharedPreferencesName == null) {
            sharedPreferencesName = String.valueOf(context.getPackageName()) + PREFERENCE;
        }
        return getSharedPreferences(context, sharedPreferencesName, 0);
    }

    private String getBkDomain(String str) {
        return TextUtils.isEmpty(str) ? "" : str;
    }

    public static String getConfigVersion() {
        return Config_Version;
    }

    private static String getConfigVersionFromSharedPreference(Context context) {
        return context == null ? "" : getAppSharedPreferences(context).getString(PREF_DOWNPROXY_CONFIG_VERSION, "");
    }

    public static String getDownProxyConfig(Context context) {
        if (context == null) {
            return "";
        }
        Config_Version = getConfigVersionFromSharedPreference(context);
        return getAppSharedPreferences(context).getString(PREF_DOWNPROXY_CONFIG, "");
    }

    public static String getHttpProxyVersion() {
        return TVHttpProxy_Version;
    }

    public static String getHttpProxyVersionCode() {
        return TVHttpProxy_Version_Code;
    }

    /* JADX WARNING: Removed duplicated region for block: B:17:0x0062 A[SYNTHETIC, Splitter:B:17:0x0062] */
    private SSLContext getHttpsSSLContext(Context context) {
        InputStream inputStream;
        Throwable th5;
        try {
            CertificateFactory instance = CertificateFactory.getInstance("X.509");
            inputStream = context.getAssets().open(httpsCrtFileName);
            try {
                Certificate generateCertificate = instance.generateCertificate(inputStream);
                KeyStore instance2 = KeyStore.getInstance(KeyStore.getDefaultType());
                instance2.load(null, null);
                instance2.setCertificateEntry("ca", generateCertificate);
                TrustManagerFactory instance3 = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                instance3.init(instance2);
                SSLContext instance4 = SSLContext.getInstance("TLS");
                instance4.init(null, instance3.getTrustManagers(), null);
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException unused) {
                    }
                }
                return instance4;
            } catch (Throwable th6) {
                th5 = th6;
                try {
                    HttpproxyFacade.print(6, TAG, "getHttpsSSLContext unknow exception:" + Log.getStackTraceString(th5));
                    return null;
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException unused2) {
                        }
                    }
                }
            }
        } catch (Throwable th7) {
            th5 = th7;
            inputStream = null;
            HttpproxyFacade.print(6, TAG, "getHttpsSSLContext unknow exception:" + Log.getStackTraceString(th5));
            return null;
        }
    }

    public static SharedPreferences getSharedPreferences(Context context, String str, int i15) {
        return context.getSharedPreferences(str, i15);
    }

    private String makeGetServerConfigUrl(Context context) {
        return String.format("%s?platform=%s&appver=%s&player_channel_id=%s&otype=%s&ipflag=%s&guid=%s", url, platform, TVHttpProxy_Version, TVHttpProxy_channel_id, otype, "1", guid);
    }

    private boolean needGetConfig() {
        if (LastGetConfigTime == 0) {
            LastGetConfigTime = System.currentTimeMillis();
            return true;
        }
        long currentTimeMillis = System.currentTimeMillis();
        long j15 = LastGetConfigTime;
        if (currentTimeMillis - j15 <= GET_CONFIG_INTERVAL && currentTimeMillis >= j15) {
            return false;
        }
        LastGetConfigTime = System.currentTimeMillis();
        return true;
    }

    private static void setConfigVersionToSharedPreference(Context context, String str) {
        if (context != null && str != null) {
            getAppSharedPreferences(context).edit().putString(PREF_DOWNPROXY_CONFIG_VERSION, str).commit();
        }
    }

    public static void setDownProxyConfig(Context context, String str) {
        getAppSharedPreferences(context).edit().putString(PREF_DOWNPROXY_CONFIG, str).commit();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void synGetConfig(Context context) {
        if (!needGetConfig()) {
            String str = TAG;
            HttpproxyFacade.print(4, str, "last get config time:" + System.currentTimeMillis() + ",so get config just return");
            return;
        }
        int i15 = -1;
        try {
            JSONObject jSONObject = new JSONObject(fetchTextFromUrl(makeGetServerConfigUrl(context), context));
            if (jSONObject.has(TPPlayerMgr.PROXY_HOST_KEY)) {
                i15 = 0;
                String str2 = TAG;
                HttpproxyFacade.print(3, str2, "get httpproxy_config:" + jSONObject.getString(TPPlayerMgr.PROXY_HOST_KEY));
                setDownProxyConfig(context, jSONObject.getString(TPPlayerMgr.PROXY_HOST_KEY));
            }
            if (jSONObject.has("config_ver")) {
                setConfigVersionToSharedPreference(context, jSONObject.getString("config_ver"));
            }
        } catch (Throwable unused) {
            HttpproxyFacade.print(6, TAG, "get config exception");
        }
        try {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("error_code", i15);
            jSONObject2.put("report_type", 90);
            HttpproxyFacade.jsonReport(jSONObject2.toString());
        } catch (JSONException unused2) {
        }
    }

    public void stopGetServerConfig() {
        try {
            Thread thread2 = this.thread;
            if (thread2 != null) {
                thread2.interrupt();
            }
        } catch (Throwable unused) {
            HttpproxyFacade.print(6, TAG, "thread create exception");
        }
    }

    public void synGetServerConfig(final Context context) {
        if (context == null) {
            HttpproxyFacade.print(6, TAG, "synGetServerConfig context is null");
            return;
        }
        try {
            Thread thread2 = new Thread(new Runnable() {
                /* class com.tencent.qqvideo.proxy.common.ConfigStorage.AnonymousClass1 */

                public void run() {
                    ConfigStorage.this.synGetConfig(context);
                }
            });
            this.thread = thread2;
            thread2.start();
        } catch (Throwable unused) {
            HttpproxyFacade.print(6, TAG, "thread create exception");
        }
    }
}