せい v4.7.3版本的 MD5 值为:203275d28cdb30ff42801fbf3f03f780

以下内容为反编译后的 n0.java 源代码,内容仅作参考


package qh;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import androidx.exifinterface.media.ExifInterface;
import com.bumptech.glide.load.Key;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import com.wind.imlib.db.manager.CommSettingManager;
import com.xiaomi.push.service.f0;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class n0 {

    public static Context f14927h;
    public static n0 i;

    public static a f14928j;

    public static String f14929k;

    public final b f14933c;

    public final String f14934d;

    public static final HashMap f14926g = new HashMap();

    public static boolean f14930l = false;

    public final HashMap f14931a = new HashMap();

    public long f14935e = 0;

    public long f14936f = 0;

    public final m0 f14932b = new o0();

    public interface a {
    }

    public interface b {
    }

    public n0(Context context, f0.a aVar, String str, String str2) {
        this.f14934d = PushConstants.PUSH_TYPE_NOTIFY;
        this.f14933c = aVar;
        this.f14934d = PushConstants.PUSH_TYPE_NOTIFY;
        f14929k = str == null ? context.getPackageName() : str;
        if (str2 == null) {
            try {
                f14927h.getPackageManager().getPackageInfo(f14927h.getPackageName(), 16384);
            } catch (Exception unused) {
            }
        }
    }

    public static String a() {
        NetworkInfo activeNetworkInfo;
        Context context = f14927h;
        if (context == null) {
            return "unknown";
        }
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null || (activeNetworkInfo = connectivityManager.getActiveNetworkInfo()) == null) {
                return "unknown";
            }
            if (activeNetworkInfo.getType() == 1) {
                return "WIFI-UNKNOWN";
            }
            return activeNetworkInfo.getTypeName() + "-" + activeNetworkInfo.getSubtypeName();
        } catch (Throwable unused) {
            return "unknown";
        }
    }

    public static synchronized n0 f() {
        n0 n0Var;
        synchronized (n0.class) {
            n0Var = i;
            if (n0Var == null) {
                throw new IllegalStateException("the host manager is not initialized yet.");
            }
        }
        return n0Var;
    }

    public static synchronized void g(Context context, f0.a aVar) {
        synchronized (n0.class) {
            Context applicationContext = context.getApplicationContext();
            f14927h = applicationContext;
            if (applicationContext == null) {
                f14927h = context;
            }
            if (i == null) {
                if (f14928j == null) {
                    i = new n0(context, aVar, CommSettingManager.PUSH_KEY, "2.2");
                } else {
                    i = new f0.b(context, aVar);
                }
            }
        }
    }

    public static synchronized void h(com.xiaomi.push.service.f0 f0Var) {
        synchronized (n0.class) {
            f14928j = f0Var;
            i = null;
        }
    }

    public static void i(String str, String str2) {
        HashMap hashMap = f14926g;
        k0 k0Var = (k0) hashMap.get(str);
        synchronized (hashMap) {
            if (k0Var == null) {
                k0 k0Var2 = new k0(str);
                k0Var2.f(604800000L);
                k0Var2.g(str2);
                hashMap.put(str, k0Var2);
            } else {
                k0Var.g(str2);
            }
        }
    }

    public static String q() {
        Throwable th2;
        BufferedInputStream bufferedInputStream;
        FileInputStream fileInputStream;
        byte[] bArr;
        byte[] digest;
        try {
            File file = new File(f14927h.getFilesDir(), "host_fallbacks");
            if (file.isFile()) {
                fileInputStream = new FileInputStream(file);
                try {
                    bufferedInputStream = new BufferedInputStream(fileInputStream);
                } catch (Throwable th3) {
                    th2 = th3;
                    bufferedInputStream = null;
                }
                try {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] bArr2 = new byte[8192];
                    while (true) {
                        try {
                            try {
                                int read = bufferedInputStream.read(bArr2, 0, 8192);
                                if (read <= 0) {
                                    break;
                                }
                                byteArrayOutputStream.write(bArr2, 0, read);
                            } catch (Exception e10) {
                                e10.printStackTrace();
                                le.c.q(bufferedInputStream);
                                le.c.q(byteArrayOutputStream);
                                bArr = null;
                            }
                        } finally {
                            le.c.q(bufferedInputStream);
                            le.c.q(byteArrayOutputStream);
                        }
                    }
                    bArr = byteArrayOutputStream.toByteArray();
                    String str = f14927h.getPackageName() + "_key_salt";
                    if (!TextUtils.isEmpty(str)) {
                        try {
                            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                            messageDigest.update(str.getBytes());
                            digest = messageDigest.digest();
                        } catch (Exception unused) {
                        }
                        String str2 = new String(cl.d.Q(digest, bArr), StandardCharsets.UTF_8);
                        lh.b.l("load host fallbacks = " + str2);
                        return str2;
                    }
                    digest = null;
                    String str22 = new String(cl.d.Q(digest, bArr), StandardCharsets.UTF_8);
                    lh.b.l("load host fallbacks = " + str22);
                    return str22;
                } catch (Throwable th4) {
                    th2 = th4;
                    try {
                        lh.b.d("load host exception " + th2.getMessage());
                        return null;
                    } finally {
                        le.c.q(bufferedInputStream);
                        le.c.q(fileInputStream);
                    }
                }
            }
        } catch (Throwable th5) {
            th2 = th5;
            bufferedInputStream = null;
            fileInputStream = null;
        }
        return null;
    }

    public String b(ArrayList arrayList, String str, String str2) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList arrayList3 = new ArrayList();
        arrayList3.add(new u("type", str));
        if (str.equals("wap")) {
            String b10 = x.b(f14927h);
            try {
                int length = b10.length();
                byte[] bytes = b10.getBytes(Key.STRING_CHARSET_NAME);
                for (int i2 = 0; i2 < bytes.length; i2++) {
                    byte b11 = bytes[i2];
                    int i10 = b11 & 240;
                    if (i10 != 240) {
                        bytes[i2] = (byte) (((b11 & 15) ^ ((byte) (((b11 >> 4) + length) & 15))) | i10);
                    }
                }
                b10 = new String(bytes);
            } catch (UnsupportedEncodingException unused) {
            }
            arrayList3.add(new u("conpt", b10));
        }
        arrayList3.add(new u("reserved", PushConstants.PUSH_TYPE_THROUGH_MESSAGE));
        arrayList3.add(new u("uuid", str2));
        arrayList3.add(new u("list", le.c.j(arrayList)));
        com.xiaomi.push.service.b a10 = com.xiaomi.push.service.b.a(f14927h);
        if (TextUtils.isEmpty(a10.f8262d)) {
            a10.f8262d = com.xiaomi.push.service.b.c(a10.f8263e, "mipush_country_code", "mipush_country_code.lock", a10.f8260b);
        }
        arrayList3.add(new u("countrycode", a10.f8262d));
        arrayList3.add(new u("push_sdk_vc", String.valueOf(50300)));
        k0 o10 = o("resolver.msg.xiaomi.net");
        String format = String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", "resolver.msg.xiaomi.net");
        if (o10 == null) {
            arrayList2.add(format);
            HashMap hashMap = f14926g;
            synchronized (hashMap) {
                k0 k0Var = (k0) hashMap.get("resolver.msg.xiaomi.net");
                if (k0Var != null) {
                    Iterator<String> it2 = k0Var.d(true).iterator();
                    while (it2.hasNext()) {
                        arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", it2.next()));
                    }
                }
            }
        } else {
            arrayList2 = o10.c(format);
        }
        Iterator<String> it3 = arrayList2.iterator();
        IOException e10 = null;
        while (it3.hasNext()) {
            Uri.Builder buildUpon = Uri.parse(it3.next()).buildUpon();
            Iterator it4 = arrayList3.iterator();
            while (it4.hasNext()) {
                w wVar = (w) it4.next();
                buildUpon.appendQueryParameter(wVar.a(), wVar.b());
            }
            try {
                b bVar = this.f14933c;
                if (bVar == null) {
                    return x.c(f14927h, new URL(buildUpon.toString()));
                }
                return ((f0.a) bVar).a(buildUpon.toString());
            } catch (IOException e11) {
                e10 = e11;
            }
        }
        if (e10 == null) {
            return null;
        }
        lh.b.d("network exception: " + e10.getMessage());
        throw e10;
    }

    public final ArrayList<k0> c(ArrayList<String> arrayList) {
        String str;
        long j10;
        JSONObject jSONObject;
        JSONObject jSONObject2;
        r();
        synchronized (this.f14931a) {
            k();
            for (String str2 : this.f14931a.keySet()) {
                if (!arrayList.contains(str2)) {
                    arrayList.add(str2);
                }
            }
        }
        HashMap hashMap = f14926g;
        synchronized (hashMap) {
            for (Object obj : hashMap.values().toArray()) {
                k0 k0Var = (k0) obj;
                if (!k0Var.k()) {
                    f14926g.remove(k0Var.f14729d);
                }
            }
        }
        if (!arrayList.contains("resolver.msg.xiaomi.net")) {
            arrayList.add("resolver.msg.xiaomi.net");
        }
        ArrayList<k0> arrayList2 = new ArrayList<>(arrayList.size());
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            arrayList2.add(null);
        }
        try {
            if (x.k(f14927h)) {
                str = "wifi";
            } else {
                str = "wap";
            }
            String b10 = b(arrayList, str, this.f14934d);
            if (!TextUtils.isEmpty(b10)) {
                JSONObject jSONObject3 = new JSONObject(b10);
                lh.b.l(b10);
                if ("OK".equalsIgnoreCase(jSONObject3.getString(ExifInterface.LATITUDE_SOUTH))) {
                    JSONObject jSONObject4 = jSONObject3.getJSONObject("R");
                    String string = jSONObject4.getString("province");
                    String string2 = jSONObject4.getString("city");
                    String string3 = jSONObject4.getString("isp");
                    String string4 = jSONObject4.getString(IjkMediaPlayer.OnNativeInvokeListener.ARG_IP);
                    String string5 = jSONObject4.getString("country");
                    JSONObject jSONObject5 = jSONObject4.getJSONObject(str);
                    lh.b.n("get bucket: net=" + string3 + ", hosts=" + jSONObject5.toString());
                    int i10 = 0;
                    while (i10 < arrayList.size()) {
                        String str3 = arrayList.get(i10);
                        JSONArray optJSONArray = jSONObject5.optJSONArray(str3);
                        if (optJSONArray == null) {
                            lh.b.d("no bucket found for " + str3);
                            jSONObject = jSONObject5;
                        } else {
                            k0 k0Var2 = new k0(str3);
                            int i11 = 0;
                            while (i11 < optJSONArray.length()) {
                                String string6 = optJSONArray.getString(i11);
                                if (!TextUtils.isEmpty(string6)) {
                                    jSONObject2 = jSONObject5;
                                    k0Var2.j(new s0(string6, optJSONArray.length() - i11));
                                } else {
                                    jSONObject2 = jSONObject5;
                                }
                                i11++;
                                jSONObject5 = jSONObject2;
                            }
                            jSONObject = jSONObject5;
                            arrayList2.set(i10, k0Var2);
                            k0Var2.i = string5;
                            k0Var2.f14730e = string;
                            k0Var2.f14732g = string3;
                            k0Var2.f14733h = string4;
                            k0Var2.f14731f = string2;
                            if (jSONObject4.has("stat-percent")) {
                                k0Var2.f14736l = jSONObject4.getDouble("stat-percent");
                            }
                            if (jSONObject4.has("stat-domain")) {
                                jSONObject4.getString("stat-domain");
                            }
                            if (jSONObject4.has("ttl")) {
                                k0Var2.f(jSONObject4.getInt("ttl") * 1000);
                            }
                            k0Var2.a();
                        }
                        i10++;
                        jSONObject5 = jSONObject;
                    }
                    JSONObject optJSONObject = jSONObject4.optJSONObject("reserved");
                    if (optJSONObject != null) {
                        if (jSONObject4.has("reserved-ttl")) {
                            j10 = jSONObject4.getInt("reserved-ttl") * 1000;
                        } else {
                            j10 = 604800000;
                        }
                        Iterator<String> keys = optJSONObject.keys();
                        while (keys.hasNext()) {
                            String next = keys.next();
                            JSONArray optJSONArray2 = optJSONObject.optJSONArray(next);
                            if (optJSONArray2 == null) {
                                lh.b.d("no bucket found for " + next);
                            } else {
                                k0 k0Var3 = new k0(next);
                                k0Var3.f(j10);
                                for (int i12 = 0; i12 < optJSONArray2.length(); i12++) {
                                    String string7 = optJSONArray2.getString(i12);
                                    if (!TextUtils.isEmpty(string7)) {
                                        k0Var3.j(new s0(string7, optJSONArray2.length() - i12));
                                    }
                                }
                                HashMap hashMap2 = f14926g;
                                synchronized (hashMap2) {
                                    this.f14932b.getClass();
                                    hashMap2.put(next, k0Var3);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e10) {
            lh.b.d("failed to get bucket " + e10.getMessage());
        }
        for (int i13 = 0; i13 < arrayList.size(); i13++) {
            k0 k0Var4 = arrayList2.get(i13);
            if (k0Var4 != null) {
                j(arrayList.get(i13), k0Var4);
            }
        }
        p();
        return arrayList2;
    }

    public final JSONObject d() {
        JSONObject jSONObject;
        synchronized (this.f14931a) {
            jSONObject = new JSONObject();
            jSONObject.put("ver", 2);
            JSONArray jSONArray = new JSONArray();
            Iterator it2 = this.f14931a.values().iterator();
            while (it2.hasNext()) {
                jSONArray.put(((l0) it2.next()).a());
            }
            jSONObject.put("data", jSONArray);
            JSONArray jSONArray2 = new JSONArray();
            Iterator it3 = f14926g.values().iterator();
            while (it3.hasNext()) {
                jSONArray2.put(((k0) it3.next()).e());
            }
            jSONObject.put("reserved", jSONArray2);
        }
        return jSONObject;
    }

    public final k0 e(String str, boolean z10) {
        k0 k0Var;
        lh.b.m("HostManager", "-->getFallbacksByHost(): host=", str, ", fetchRemoteIfNeed=", Boolean.valueOf(z10));
        if (!TextUtils.isEmpty(str)) {
            this.f14932b.getClass();
            k0 o10 = o(str);
            if (o10 != null && o10.k()) {
                return o10;
            }
            if (z10 && x.h(f14927h)) {
                if (System.currentTimeMillis() - this.f14936f > this.f14935e * 60 * 1000) {
                    this.f14936f = System.currentTimeMillis();
                    ArrayList<String> arrayList = new ArrayList<>();
                    arrayList.add(str);
                    k0Var = c(arrayList).get(0);
                    if (k0Var != null) {
                        this.f14935e = 0L;
                        if (k0Var != null) {
                            return k0Var;
                        }
                    } else {
                        long j10 = this.f14935e;
                        if (j10 < 15) {
                            this.f14935e = j10 + 1;
                        }
                    }
                }
                k0Var = null;
                if (k0Var != null) {
                }
            }
            return new p0(str, o10);
        }
        throw new IllegalArgumentException("the host is empty");
    }

    public final void j(String str, k0 k0Var) {
        if (!TextUtils.isEmpty(str) && k0Var != null) {
            this.f14932b.getClass();
            synchronized (this.f14931a) {
                k();
                if (this.f14931a.containsKey(str)) {
                    ((l0) this.f14931a.get(str)).e(k0Var);
                } else {
                    l0 l0Var = new l0(str);
                    l0Var.e(k0Var);
                    this.f14931a.put(str, l0Var);
                }
            }
            return;
        }
        throw new IllegalArgumentException("the argument is invalid " + str + ", " + k0Var);
    }

    public final void k() {
        synchronized (this.f14931a) {
            if (!f14930l) {
                f14930l = true;
                this.f14931a.clear();
                try {
                    String q = q();
                    if (!TextUtils.isEmpty(q)) {
                        m(q);
                        lh.b.l("loading the new hosts succeed");
                    }
                } catch (Throwable th2) {
                    lh.b.d("load bucket failure: " + th2.getMessage());
                }
            }
        }
    }

    public final void l() {
        ArrayList<String> arrayList;
        synchronized (this.f14931a) {
            k();
            arrayList = new ArrayList<>(this.f14931a.keySet());
            int size = arrayList.size();
            while (true) {
                size--;
                if (size < 0) {
                    break;
                }
                l0 l0Var = (l0) this.f14931a.get(arrayList.get(size));
                if (l0Var != null && l0Var.b() != null) {
                    arrayList.remove(size);
                }
            }
        }
        ArrayList<k0> c10 = c(arrayList);
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            if (c10.get(i2) != null) {
                j(arrayList.get(i2), c10.get(i2));
            }
        }
    }

    public final void m(String str) {
        synchronized (this.f14931a) {
            this.f14931a.clear();
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optInt("ver") == 2) {
                JSONArray optJSONArray = jSONObject.optJSONArray("data");
                if (optJSONArray != null) {
                    for (int i2 = 0; i2 < optJSONArray.length(); i2++) {
                        l0 l0Var = new l0();
                        l0Var.d(optJSONArray.getJSONObject(i2));
                        this.f14931a.put(l0Var.f14814a, l0Var);
                    }
                }
                JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved");
                if (optJSONArray2 != null) {
                    for (int i10 = 0; i10 < optJSONArray2.length(); i10++) {
                        JSONObject jSONObject2 = optJSONArray2.getJSONObject(i10);
                        String optString = jSONObject2.optString("host");
                        if (!TextUtils.isEmpty(optString)) {
                            try {
                                k0 k0Var = new k0(optString);
                                k0Var.i(jSONObject2);
                                f14926g.put(k0Var.f14729d, k0Var);
                                lh.b.d("load local reserved host for " + k0Var.f14729d);
                            } catch (JSONException unused) {
                                lh.b.d("parse reserved host fail.");
                            }
                        }
                    }
                }
            } else {
                throw new JSONException("Bad version");
            }
        }
    }

    public final String n() {
        StringBuilder sb2 = new StringBuilder();
        synchronized (this.f14931a) {
            for (Map.Entry entry : this.f14931a.entrySet()) {
                sb2.append((String) entry.getKey());
                sb2.append(":\n");
                sb2.append(((l0) entry.getValue()).toString());
                sb2.append("\n");
            }
        }
        return sb2.toString();
    }

    public final k0 o(String str) {
        l0 l0Var;
        k0 b10;
        synchronized (this.f14931a) {
            k();
            l0Var = (l0) this.f14931a.get(str);
        }
        if (l0Var != null && (b10 = l0Var.b()) != null) {
            return b10;
        }
        return null;
    }

    public final void p() {
        Throwable th2;
        FileOutputStream fileOutputStream;
        BufferedOutputStream bufferedOutputStream;
        Exception e10;
        synchronized (this.f14931a) {
            BufferedOutputStream bufferedOutputStream2 = null;
            r3 = null;
            byte[] digest = null;
            bufferedOutputStream2 = null;
            BufferedOutputStream bufferedOutputStream3 = null;
            try {
                String jSONObject = d().toString();
                lh.b.l("persist host fallbacks = " + jSONObject);
                if (!TextUtils.isEmpty(jSONObject)) {
                    fileOutputStream = f14927h.openFileOutput("host_fallbacks", 0);
                    try {
                        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                        try {
                            try {
                                String str = f14927h.getPackageName() + "_key_salt";
                                if (!TextUtils.isEmpty(str)) {
                                    try {
                                        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                                        messageDigest.update(str.getBytes());
                                        digest = messageDigest.digest();
                                    } catch (Exception unused) {
                                    }
                                }
                                byte[] bytes = jSONObject.getBytes(StandardCharsets.UTF_8);
                                SecretKeySpec secretKeySpec = new SecretKeySpec(digest, "AES");
                                IvParameterSpec ivParameterSpec = new IvParameterSpec(cl.d.f3669u);
                                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                                cipher.init(1, secretKeySpec, ivParameterSpec);
                                bufferedOutputStream.write(cipher.doFinal(bytes));
                                bufferedOutputStream.flush();
                                bufferedOutputStream3 = bufferedOutputStream;
                            } catch (Exception e11) {
                                e10 = e11;
                                lh.b.d("persist bucket failure: " + e10.getMessage());
                                le.c.q(bufferedOutputStream);
                                le.c.q(fileOutputStream);
                            }
                        } catch (Throwable th3) {
                            th2 = th3;
                            bufferedOutputStream2 = bufferedOutputStream;
                            le.c.q(bufferedOutputStream2);
                            le.c.q(fileOutputStream);
                            throw th2;
                        }
                    } catch (Exception e12) {
                        bufferedOutputStream = null;
                        e10 = e12;
                    } catch (Throwable th4) {
                        th2 = th4;
                        le.c.q(bufferedOutputStream2);
                        le.c.q(fileOutputStream);
                        throw th2;
                    }
                } else {
                    fileOutputStream = null;
                }
                le.c.q(bufferedOutputStream3);
            } catch (Exception e13) {
                bufferedOutputStream = null;
                e10 = e13;
                fileOutputStream = null;
            } catch (Throwable th5) {
                th2 = th5;
                fileOutputStream = null;
            }
            le.c.q(fileOutputStream);
        }
    }

    public final void r() {
        synchronized (this.f14931a) {
            Iterator it2 = this.f14931a.values().iterator();
            while (it2.hasNext()) {
                ((l0) it2.next()).c();
            }
            while (true) {
                for (boolean z10 = false; !z10; z10 = true) {
                    for (String str : this.f14931a.keySet()) {
                        if (((l0) this.f14931a.get(str)).f14815b.isEmpty()) {
                            break;
                        }
                    }
                }
                this.f14931a.remove(str);
            }
        }
    }
}