package eg; import ag.c; import android.content.Context; import android.content.pm.PackageInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.text.TextUtils; import com.meizu.cloud.pushsdk.constants.PushConstants; import com.pichillilorenzo.flutter_inappwebview.credential_database.URLProtectionSpaceContract; import com.tencent.imsdk.BaseConstants; import com.tencent.thumbplayer.tplayer.plugins.report.TPReportParams; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class v1 { /* renamed from: i reason: collision with root package name */ protected static final Map<String, r1> f15485i = new HashMap(); /* renamed from: j reason: collision with root package name */ protected static Context f15486j; /* renamed from: k reason: collision with root package name */ private static v1 f15487k; /* renamed from: l reason: collision with root package name */ private static a f15488l; /* renamed from: m reason: collision with root package name */ private static String f15489m; /* renamed from: n reason: collision with root package name */ private static String f15490n; /* renamed from: o reason: collision with root package name */ protected static boolean f15491o = false; /* renamed from: a reason: collision with root package name */ protected final Map<String, s1> f15492a; /* renamed from: b reason: collision with root package name */ private u1 f15493b; /* renamed from: c reason: collision with root package name */ protected b f15494c; /* renamed from: d reason: collision with root package name */ private String f15495d; /* renamed from: e reason: collision with root package name */ private long f15496e; /* renamed from: f reason: collision with root package name */ private final long f15497f; /* renamed from: g reason: collision with root package name */ private long f15498g; /* renamed from: h reason: collision with root package name */ private String f15499h; public interface a { v1 a(Context context, u1 u1Var, b bVar, String str); } public interface b { String a(String str); } protected v1(Context context, u1 u1Var, b bVar, String str) { this(context, u1Var, bVar, str, null, null); } protected v1(Context context, u1 u1Var, b bVar, String str, String str2, String str3) { this.f15492a = new HashMap(); this.f15495d = "0"; this.f15496e = 0; this.f15497f = 15; this.f15498g = 0; this.f15499h = "isp_prov_city_country_ip"; this.f15494c = bVar; this.f15493b = u1Var == null ? new w1(this) : u1Var; this.f15495d = str; f15489m = str2 == null ? context.getPackageName() : str2; f15490n = str3 == null ? B() : str3; } private String B() { try { PackageInfo packageInfo = f15486j.getPackageManager().getPackageInfo(f15486j.getPackageName(), 16384); return packageInfo != null ? packageInfo.versionName : "0"; } catch (Exception unused) { return "0"; } } public static synchronized v1 c() { v1 v1Var; synchronized (v1.class) { v1Var = f15487k; if (v1Var == null) { throw new IllegalStateException("the host manager is not initialized yet."); } } return v1Var; } static String d() { NetworkInfo activeNetworkInfo; Context context = f15486j; 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"; } } static String e(String str) { try { int length = str.length(); byte[] bytes = str.getBytes("UTF-8"); for (int i10 = 0; i10 < bytes.length; i10++) { byte b10 = bytes[i10]; int i11 = b10 & 240; if (i11 != 240) { bytes[i10] = (byte) (((b10 & 15) ^ ((byte) (((b10 >> 4) + length) & 15))) | i11); } } return new String(bytes); } catch (UnsupportedEncodingException unused) { return str; } } private ArrayList<r1> g(ArrayList<String> arrayList) { JSONObject jSONObject; JSONObject jSONObject2; y(); synchronized (this.f15492a) { o(); for (String str : this.f15492a.keySet()) { if (!arrayList.contains(str)) { arrayList.add(str); } } } Map<String, r1> map = f15485i; synchronized (map) { for (Object obj : map.values().toArray()) { r1 r1Var = (r1) obj; if (!r1Var.u()) { f15485i.remove(r1Var.f15342d); } } } if (!arrayList.contains(q())) { arrayList.add(q()); } ArrayList<r1> arrayList2 = new ArrayList<>(arrayList.size()); for (int i10 = 0; i10 < arrayList.size(); i10++) { arrayList2.add(null); } try { String str2 = k0.s(f15486j) ? "wifi" : "wap"; String f10 = f(arrayList, str2, this.f15495d, true); if (!TextUtils.isEmpty(f10)) { JSONObject jSONObject3 = new JSONObject(f10); c.s(f10); if ("OK".equalsIgnoreCase(jSONObject3.getString("S"))) { JSONObject jSONObject4 = jSONObject3.getJSONObject("R"); String string = jSONObject4.getString("province"); String string2 = jSONObject4.getString("city"); String string3 = jSONObject4.getString("isp"); String string4 = jSONObject4.getString("ip"); String string5 = jSONObject4.getString("country"); JSONObject jSONObject5 = jSONObject4.getJSONObject(str2); c.t("get bucket: net=" + string3 + ", hosts=" + jSONObject5.toString()); int i11 = 0; while (i11 < arrayList.size()) { String str3 = arrayList.get(i11); JSONArray optJSONArray = jSONObject5.optJSONArray(str3); if (optJSONArray == null) { c.m("no bucket found for " + str3); jSONObject = jSONObject5; } else { r1 r1Var2 = new r1(str3); int i12 = 0; while (i12 < optJSONArray.length()) { String string6 = optJSONArray.getString(i12); if (!TextUtils.isEmpty(string6)) { jSONObject2 = jSONObject5; r1Var2.i(new a2(string6, optJSONArray.length() - i12)); } else { jSONObject2 = jSONObject5; } i12++; jSONObject5 = jSONObject2; } jSONObject = jSONObject5; arrayList2.set(i11, r1Var2); r1Var2.f15347i = string5; r1Var2.f15343e = string; r1Var2.f15345g = string3; r1Var2.f15346h = string4; r1Var2.f15344f = string2; if (jSONObject4.has("stat-percent")) { r1Var2.g(jSONObject4.getDouble("stat-percent")); } if (jSONObject4.has("stat-domain")) { r1Var2.r(jSONObject4.getString("stat-domain")); } if (jSONObject4.has("ttl")) { r1Var2.h(((long) jSONObject4.getInt("ttl")) * 1000); } l(r1Var2.b()); } i11++; jSONObject5 = jSONObject; } JSONObject optJSONObject = jSONObject4.optJSONObject("reserved"); if (optJSONObject != null) { long j10 = 604800000; if (jSONObject4.has("reserved-ttl")) { j10 = ((long) jSONObject4.getInt("reserved-ttl")) * 1000; } Iterator<String> keys = optJSONObject.keys(); while (keys.hasNext()) { String next = keys.next(); JSONArray optJSONArray2 = optJSONObject.optJSONArray(next); if (optJSONArray2 == null) { c.m("no bucket found for " + next); } else { r1 r1Var3 = new r1(next); r1Var3.h(j10); for (int i13 = 0; i13 < optJSONArray2.length(); i13++) { String string7 = optJSONArray2.getString(i13); if (!TextUtils.isEmpty(string7)) { r1Var3.i(new a2(string7, optJSONArray2.length() - i13)); } } Map<String, r1> map2 = f15485i; synchronized (map2) { if (this.f15493b.a(next)) { map2.put(next, r1Var3); } } } } } } } } catch (Exception e10) { c.m("failed to get bucket " + e10.getMessage()); } for (int i14 = 0; i14 < arrayList.size(); i14++) { r1 r1Var4 = arrayList2.get(i14); if (r1Var4 != null) { m(arrayList.get(i14), r1Var4); } } v(); return arrayList2; } public static synchronized void j(Context context, u1 u1Var, b bVar, String str, String str2, String str3) { synchronized (v1.class) { Context applicationContext = context.getApplicationContext(); f15486j = applicationContext; if (applicationContext == null) { f15486j = context; } if (f15487k == null) { a aVar = f15488l; if (aVar == null) { f15487k = new v1(context, u1Var, bVar, str, str2, str3); } else { f15487k = aVar.a(context, u1Var, bVar, str); } } } } public static synchronized void k(a aVar) { synchronized (v1.class) { f15488l = aVar; f15487k = null; } } public static void n(String str, String str2) { Map<String, r1> map = f15485i; r1 r1Var = map.get(str); synchronized (map) { if (r1Var == null) { r1 r1Var2 = new r1(str); r1Var2.h(604800000); r1Var2.j(str2); map.put(str, r1Var2); } else { r1Var.j(str2); } } } /* access modifiers changed from: protected */ public String A() { if ("com.xiaomi.xmsf".equals(f15489m)) { return f15489m; } return f15489m + ":pushservice"; } public r1 a(String str) { if (!TextUtils.isEmpty(str)) { return b(new URL(str).getHost(), true); } throw new IllegalArgumentException("the url is empty"); } public r1 b(String str, boolean z10) { r1 z11; if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("the host is empty"); } else if (!this.f15493b.a(str)) { return null; } else { r1 t10 = t(str); return (t10 == null || !t10.u()) ? (!z10 || !k0.p(f15486j) || (z11 = z(str)) == null) ? new x1(this, str, t10) : z11 : t10; } } /* access modifiers changed from: protected */ public String f(ArrayList<String> arrayList, String str, String str2, boolean z10) { ArrayList<String> arrayList2 = new ArrayList<>(); ArrayList<j0> arrayList3 = new ArrayList(); arrayList3.add(new h0("type", str)); if (str.equals("wap")) { arrayList3.add(new h0("conpt", e(k0.g(f15486j)))); } if (z10) { arrayList3.add(new h0("reserved", PushConstants.PUSH_TYPE_THROUGH_MESSAGE)); } arrayList3.add(new h0("uuid", str2)); arrayList3.add(new h0("list", q0.d(arrayList, ","))); arrayList3.add(new h0("countrycode", com.xiaomi.push.service.b.a(f15486j).f())); arrayList3.add(new h0("push_sdk_vc", String.valueOf((int) BaseConstants.ERR_SVR_CONV_NET_TIMEOUT))); String q10 = q(); r1 t10 = t(q10); String format = String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", q10); if (t10 == null) { arrayList2.add(format); Map<String, r1> map = f15485i; synchronized (map) { r1 r1Var = map.get(q10); if (r1Var != null) { Iterator<String> it = r1Var.e(true).iterator(); while (it.hasNext()) { arrayList2.add(String.format(Locale.US, "https://%1$s/gslb/?ver=5.0", it.next())); } } } } else { arrayList2 = t10.d(format); } Iterator<String> it2 = arrayList2.iterator(); IOException e10 = null; while (it2.hasNext()) { Uri.Builder buildUpon = Uri.parse(it2.next()).buildUpon(); for (j0 j0Var : arrayList3) { buildUpon.appendQueryParameter(j0Var.a(), j0Var.b()); } try { b bVar = this.f15494c; return bVar == null ? k0.h(f15486j, new URL(buildUpon.toString())) : bVar.a(buildUpon.toString()); } catch (IOException e11) { e10 = e11; } } if (e10 == null) { return null; } c.m("network exception: " + e10.getMessage()); throw e10; } /* access modifiers changed from: protected */ public JSONObject h() { JSONObject jSONObject; synchronized (this.f15492a) { jSONObject = new JSONObject(); jSONObject.put("ver", 2); JSONArray jSONArray = new JSONArray(); for (s1 s1Var : this.f15492a.values()) { jSONArray.put(s1Var.e()); } jSONObject.put(TPReportParams.PROP_KEY_DATA, jSONArray); JSONArray jSONArray2 = new JSONArray(); for (r1 r1Var : f15485i.values()) { jSONArray2.put(r1Var.f()); } jSONObject.put("reserved", jSONArray2); } return jSONObject; } public void i() { synchronized (this.f15492a) { this.f15492a.clear(); } } public void l(String str) { this.f15499h = str; } public void m(String str, r1 r1Var) { if (TextUtils.isEmpty(str) || r1Var == null) { throw new IllegalArgumentException("the argument is invalid " + str + ", " + r1Var); } else if (this.f15493b.a(str)) { synchronized (this.f15492a) { o(); if (this.f15492a.containsKey(str)) { this.f15492a.get(str).f(r1Var); } else { s1 s1Var = new s1(str); s1Var.f(r1Var); this.f15492a.put(str, s1Var); } } } } /* access modifiers changed from: protected */ public boolean o() { synchronized (this.f15492a) { if (f15491o) { return true; } f15491o = true; this.f15492a.clear(); try { String x10 = x(); if (!TextUtils.isEmpty(x10)) { s(x10); c.s("loading the new hosts succeed"); return true; } } catch (Throwable th2) { c.m("load bucket failure: " + th2.getMessage()); } return false; } } public r1 p(String str) { return b(str, true); } /* access modifiers changed from: protected */ public String q() { return "resolver.msg.xiaomi.net"; } public void r() { ArrayList<String> arrayList; synchronized (this.f15492a) { o(); arrayList = new ArrayList<>(this.f15492a.keySet()); for (int size = arrayList.size() - 1; size >= 0; size--) { s1 s1Var = this.f15492a.get(arrayList.get(size)); if (!(s1Var == null || s1Var.a() == null)) { arrayList.remove(size); } } } ArrayList<r1> g10 = g(arrayList); for (int i10 = 0; i10 < arrayList.size(); i10++) { if (g10.get(i10) != null) { m(arrayList.get(i10), g10.get(i10)); } } } /* access modifiers changed from: protected */ public void s(String str) { synchronized (this.f15492a) { this.f15492a.clear(); JSONObject jSONObject = new JSONObject(str); if (jSONObject.optInt("ver") == 2) { JSONArray optJSONArray = jSONObject.optJSONArray(TPReportParams.PROP_KEY_DATA); if (optJSONArray != null) { for (int i10 = 0; i10 < optJSONArray.length(); i10++) { s1 b10 = new s1().b(optJSONArray.getJSONObject(i10)); this.f15492a.put(b10.c(), b10); } } JSONArray optJSONArray2 = jSONObject.optJSONArray("reserved"); if (optJSONArray2 != null) { for (int i11 = 0; i11 < optJSONArray2.length(); i11++) { JSONObject jSONObject2 = optJSONArray2.getJSONObject(i11); String optString = jSONObject2.optString(URLProtectionSpaceContract.FeedEntry.COLUMN_NAME_HOST); if (!TextUtils.isEmpty(optString)) { try { r1 a10 = new r1(optString).a(jSONObject2); f15485i.put(a10.f15342d, a10); c.m("load local reserved host for " + a10.f15342d); } catch (JSONException unused) { c.m("parse reserved host fail."); } } } } } else { throw new JSONException("Bad version"); } } } /* access modifiers changed from: protected */ public r1 t(String str) { s1 s1Var; r1 a10; synchronized (this.f15492a) { o(); s1Var = this.f15492a.get(str); } if (s1Var == null || (a10 = s1Var.a()) == null) { return null; } return a10; } public String u() { StringBuilder sb2 = new StringBuilder(); synchronized (this.f15492a) { for (Map.Entry<String, s1> entry : this.f15492a.entrySet()) { sb2.append(entry.getKey()); sb2.append(":\n"); sb2.append(entry.getValue().toString()); sb2.append("\n"); } } return sb2.toString(); } public void v() { synchronized (this.f15492a) { try { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(f15486j.openFileOutput(A(), 0))); String jSONObject = h().toString(); if (!TextUtils.isEmpty(jSONObject)) { bufferedWriter.write(jSONObject); } bufferedWriter.close(); } catch (Exception e10) { c.m("persist bucket failure: " + e10.getMessage()); } } } public r1 w(String str) { r1 r1Var; Map<String, r1> map = f15485i; synchronized (map) { r1Var = map.get(str); } return r1Var; } /* access modifiers changed from: protected */ public String x() { BufferedReader bufferedReader; Throwable th2; try { File file = new File(f15486j.getFilesDir(), A()); if (file.isFile()) { bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); try { StringBuilder sb2 = new StringBuilder(); while (true) { String readLine = bufferedReader.readLine(); if (readLine != null) { sb2.append(readLine); } else { String sb3 = sb2.toString(); s9.b(bufferedReader); return sb3; } } } catch (Throwable th3) { th2 = th3; try { c.m("load host exception " + th2.getMessage()); return null; } finally { s9.b(bufferedReader); } } } else { s9.b(null); return null; } } catch (Throwable th4) { th2 = th4; bufferedReader = null; c.m("load host exception " + th2.getMessage()); return null; } } public void y() { synchronized (this.f15492a) { for (s1 s1Var : this.f15492a.values()) { s1Var.g(true); } while (true) { for (boolean z10 = false; !z10; z10 = true) { for (String str : this.f15492a.keySet()) { if (this.f15492a.get(str).d().isEmpty()) { this.f15492a.remove(str); } } } } } } /* access modifiers changed from: protected */ public r1 z(String str) { if (System.currentTimeMillis() - this.f15498g <= this.f15496e * 60 * 1000) { return null; } this.f15498g = System.currentTimeMillis(); ArrayList<String> arrayList = new ArrayList<>(); arrayList.add(str); r1 r1Var = g(arrayList).get(0); if (r1Var != null) { this.f15496e = 0; return r1Var; } long j10 = this.f15496e; if (j10 >= 15) { return null; } this.f15496e = j10 + 1; return null; } }