package com.tencent.klevin.download.b.s; import android.text.TextUtils; import com.mbridge.msdk.playercommon.exoplayer2.C; import com.tencent.klevin.download.b.u.f; import com.tencent.klevin.download.b.w.e; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.Inet6Address; import java.net.InetAddress; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Random; import java.util.concurrent.locks.ReentrantLock; import java.util.regex.Pattern; public class a implements b { /* renamed from: f reason: collision with root package name */ private static final Pattern f16343f = Pattern.compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])", 2); private HashMap<String, HashSet<C0498a>> a = null; private final Object b = new Object(); private Random c = new Random(); private final HashMap<String, ReentrantLock> d = new HashMap<>(); /* renamed from: e reason: collision with root package name */ private final f f16344e; /* renamed from: com.tencent.klevin.download.b.s.a$a reason: collision with other inner class name */ public class C0498a { String a; String b; long c; long d = 120000; /* renamed from: e reason: collision with root package name */ String f16345e = "NULL"; /* renamed from: f reason: collision with root package name */ String f16346f = ""; /* renamed from: g reason: collision with root package name */ int f16347g = 0; public C0498a() { } /* access modifiers changed from: package-private */ public boolean a() { return System.currentTimeMillis() >= this.c + this.d; } public boolean equals(Object obj) { if (!(obj instanceof C0498a)) { return false; } C0498a aVar = (C0498a) obj; if (!e.b(this.b, aVar.b) || !e.b(this.a, aVar.a)) { return false; } return true; } public int hashCode() { int i2 = 0; int hashCode = TextUtils.isEmpty(this.b) ? 0 : this.b.hashCode(); if (!TextUtils.isEmpty(this.a)) { i2 = this.a.hashCode(); } return hashCode ^ i2; } public String toString() { return "[domain=" + this.a + ", ip=" + this.b + ", TTL=" + this.d + ", expired=" + a() + ", type=" + this.f16345e + ", netInfo=" + this.f16346f + ", failTimes=" + this.f16347g + "]"; } public C0498a clone() { C0498a aVar = new C0498a(); aVar.a = this.a; aVar.b = this.b; aVar.c = this.c; aVar.d = this.d; aVar.f16345e = this.f16345e; aVar.f16346f = this.f16346f; return aVar; } } public a(f fVar) { this.f16344e = fVar; } /* JADX WARNING: Removed duplicated region for block: B:60:0x014d A[SYNTHETIC, Splitter:B:60:0x014d] */ /* JADX WARNING: Removed duplicated region for block: B:65:0x0157 A[SYNTHETIC, Splitter:B:65:0x0157] */ /* JADX WARNING: Removed duplicated region for block: B:70:0x0161 */ private ArrayList<C0498a> d(String str) { Throwable th; InputStream inputStream; HttpURLConnection httpURLConnection; ArrayList<C0498a> arrayList = new ArrayList<>(); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, domain=" + str); ByteArrayOutputStream byteArrayOutputStream = null; try { httpURLConnection = (HttpURLConnection) new URL("http://119.29.29.29/d?dn=" + str).openConnection(); try { httpURLConnection.setRequestProperty("User-Agent", "Tencent QQBrowser X5"); httpURLConnection.setRequestProperty("Cache-Control", "no-cache"); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setReadTimeout(5000); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setUseCaches(false); httpURLConnection.connect(); inputStream = httpURLConnection.getInputStream(); try { byte[] bArr = new byte[4096]; ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream(); while (true) { try { int read = inputStream.read(bArr, 0, 4096); if (read == -1) { break; } byteArrayOutputStream2.write(bArr, 0, read); } catch (Throwable th2) { th = th2; byteArrayOutputStream = byteArrayOutputStream2; try { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, Failed error = " + th.getMessage()); th.printStackTrace(); if (byteArrayOutputStream != null) { try { byteArrayOutputStream.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (Exception e3) { e3.printStackTrace(); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } return arrayList; } catch (Throwable th3) { if (byteArrayOutputStream != null) { try { byteArrayOutputStream.close(); } catch (Exception e4) { e4.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (Exception e5) { e5.printStackTrace(); } } if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e6) { e6.printStackTrace(); } } throw th3; } } } String str2 = new String(byteArrayOutputStream2.toByteArray(), 0, byteArrayOutputStream2.size(), C.UTF8_NAME); String a2 = a(); if (!e.b(a2, a())) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "network has changed, before = " + a2 + ", ignore these ips"); try { byteArrayOutputStream2.close(); } catch (Exception e7) { e7.printStackTrace(); } if (inputStream != null) { try { inputStream.close(); } catch (Exception e8) { e8.printStackTrace(); } } if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e9) { e9.printStackTrace(); } } return arrayList; } String[] split = str2.split(";"); for (int i2 = 0; i2 < split.length; i2++) { if (i(split[i2])) { C0498a aVar = new C0498a(); aVar.a = str; aVar.b = split[i2]; aVar.c = System.currentTimeMillis(); aVar.f16345e = "TENCENT"; aVar.f16346f = a2; arrayList.add(aVar); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, ok, domain=" + str + ", value=" + aVar); } } try { byteArrayOutputStream2.close(); } catch (Exception e10) { e10.printStackTrace(); } if (inputStream != null) { try { inputStream.close(); } catch (Exception e11) { e11.printStackTrace(); } } if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e12) { e12.printStackTrace(); } } return arrayList; } catch (Throwable th4) { th = th4; com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, Failed error = " + th.getMessage()); th.printStackTrace(); if (byteArrayOutputStream != null) { } if (inputStream != null) { } if (httpURLConnection != null) { } return arrayList; } } catch (Throwable th5) { th = th5; inputStream = null; com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, Failed error = " + th.getMessage()); th.printStackTrace(); if (byteArrayOutputStream != null) { } if (inputStream != null) { } if (httpURLConnection != null) { } return arrayList; } } catch (Throwable th6) { th = th6; httpURLConnection = null; inputStream = null; com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromTencentDnsServer, Failed error = " + th.getMessage()); th.printStackTrace(); if (byteArrayOutputStream != null) { } if (inputStream != null) { } if (httpURLConnection != null) { } return arrayList; } } private C0498a e(String str) { if (TextUtils.isEmpty(str)) { return null; } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from cache, domain=" + str); C0498a b2 = b(str); if (b2 != null) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from cache SUCC, value=" + b2); return b2; } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from cache fail, now get from tencent dns, domain=" + str); C0498a l = l(str); if (l != null) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from tencent dns SUCC, value=" + l); return l; } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from tencent dns fail, now get from sys dns, domain=" + str); C0498a c2 = c(str); if (a(c2)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from sys dns SUCC, value=" + c2); a(str, c2); return c2; } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressSync, from sys dns fail, all fail, domain=" + str); return null; } private ReentrantLock f(String str) { ReentrantLock reentrantLock = this.d.get(str); if (reentrantLock != null) { return reentrantLock; } ReentrantLock reentrantLock2 = new ReentrantLock(); this.d.put(str, reentrantLock2); return reentrantLock2; } private boolean g(String str) { if (!TextUtils.isEmpty(str) && str.contains("127.0.0.1")) { return true; } return false; } private boolean h(String str) { String[] split; if (!TextUtils.isEmpty(str) && (split = str.split("\\.")) != null && split.length == 4) { int[] iArr = {0, 0, 0, 0}; for (int i2 = 0; i2 < 4; i2++) { try { iArr[i2] = Integer.valueOf(split[i2]).intValue(); } catch (Exception e2) { e2.printStackTrace(); return false; } } if (iArr[0] == 10) { return true; } if (iArr[0] == 172 && 16 <= iArr[1] && iArr[1] <= 31) { return true; } if (iArr[0] == 192 && iArr[1] == 168) { return true; } return false; } return false; } private boolean i(String str) { return j(str) && !k(str); } private boolean j(String str) { if (TextUtils.isEmpty(str)) { return false; } try { return f16343f.matcher(str).matches(); } catch (Exception e2) { e2.printStackTrace(); return false; } } private boolean k(String str) { String[] split; if (TextUtils.isEmpty(str) || (split = str.split("\\.")) == null || split.length != 4) { return true; } String str2 = split[0]; for (int i2 = 1; i2 < split.length; i2++) { if (!e.b(str2, split[i2])) { return false; } str2 = split[i2]; } return true; } /* JADX WARNING: Removed duplicated region for block: B:11:0x0063 A[Catch:{ all -> 0x00a4 }] */ private C0498a l(String str) { C0498a aVar; ReentrantLock f2 = f(str); boolean tryLock = f2.tryLock(); if (!tryLock) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "syncGetIPAddessFromTencentDNS, lock for " + str + " is already Locked, wait"); f2.lock(); } try { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "syncGetIPAddessFromTencentDNS, lock for " + str + " acuqired, begin get ips"); C0498a aVar2 = null; if (!tryLock) { aVar = b(str); if (!a(aVar)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "syncGetIPAddessFromTencentDNS, from cache faile, domain=" + str); } if (aVar == null) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "syncGetIPAddessFromTencentDNS, dnsData cache not valid, begin real get"); ArrayList<C0498a> d2 = d(str); if (d2.size() > 0) { aVar = d2.get(this.c.nextInt(d2.size())); if (a(aVar)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "syncGetIPAddessFromTencentDNS, begin update current cache, value=" + aVar); a(str, d2); } return aVar2; } } aVar2 = aVar; return aVar2; } aVar = null; if (aVar == null) { } aVar2 = aVar; return aVar2; } finally { f2.unlock(); } } @Override // com.tencent.klevin.download.b.s.b public String a(String str) { C0498a e2 = e(str); if (e2 != null) { return e2.b; } return null; } /* access modifiers changed from: package-private */ public C0498a b(String str) { int i2; String str2 = a() + str; synchronized (this.b) { C0498a aVar = null; if (this.a == null) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromCache : mIPAddressCache = null, return"); return null; } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromCache BEGINS check dns: cacheKey = " + str2); HashSet<C0498a> hashSet = this.a.get(str2); String str3 = "current cacheKey=" + str2 + ": "; if (hashSet != null) { Iterator<C0498a> it = hashSet.iterator(); while (it.hasNext()) { C0498a next = it.next(); if (!a(next)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromCache, " + next + ", is out of date, remove it"); it.remove(); } else { str3 = str3 + next.b + ": " + next.f16347g + "; "; } } com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromCache, " + str3); Object[] array = hashSet.toArray(); if (array == null) { i2 = 0; } else { i2 = array.length; } if (i2 > 0) { aVar = a(array); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromCache, OK key=" + str2 + ", value=" + aVar); } } return aVar; } } /* access modifiers changed from: package-private */ public C0498a c(String str) { ArrayList arrayList = new ArrayList(); try { String a2 = a(); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromSystemDNS, domain=" + str); InetAddress[] allByName = InetAddress.getAllByName(str); if (allByName == null) { return null; } if (!e.b(a2, a())) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "network has changed, before = " + a2 + ", ignore these ips"); return null; } for (InetAddress inetAddress : allByName) { if (inetAddress == null) { return null; } if (inetAddress instanceof Inet6Address) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache, is ipv6, value=" + inetAddress.getHostAddress()); } else { String hostAddress = inetAddress.getHostAddress(); if (i(hostAddress)) { C0498a aVar = new C0498a(); aVar.a = str; aVar.b = hostAddress; aVar.c = System.currentTimeMillis(); aVar.f16345e = "SYS"; aVar.d = 120000; aVar.f16346f = a2; com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache, is ipv4, value=" + aVar); arrayList.add(aVar); } } } if (arrayList.isEmpty()) { return null; } int nextInt = this.c.nextInt(arrayList.size()); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "getIPAddressFromSystemDNS, finally return ip = " + ((C0498a) arrayList.get(nextInt)).b + ", domain=" + str); return (C0498a) arrayList.get(nextInt); } catch (Throwable th) { th.printStackTrace(); } } private C0498a a(Object[] objArr) { if (objArr == null || objArr.length == 0) { return null; } C0498a aVar = (C0498a) objArr[0]; for (int i2 = 0; i2 < objArr.length; i2++) { if (((C0498a) objArr[i2]).f16347g < aVar.f16347g) { aVar = (C0498a) objArr[i2]; } } ArrayList arrayList = new ArrayList(); for (int i3 = 0; i3 < objArr.length; i3++) { if (((C0498a) objArr[i3]).f16347g == aVar.f16347g) { arrayList.add((C0498a) objArr[i3]); } } int size = arrayList.size(); if (size <= 1) { return aVar; } return (C0498a) arrayList.get(this.c.nextInt(size)); } /* access modifiers changed from: package-private */ public void a(String str, C0498a aVar) { ArrayList<C0498a> arrayList = new ArrayList<>(); arrayList.add(aVar); a(str, arrayList); } private void a(String str, ArrayList<C0498a> arrayList) { String str2; if (arrayList == null || arrayList.isEmpty() || TextUtils.isEmpty(str)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache: data is null or empty, ignore"); return; } int i2 = 0; while (true) { if (i2 >= arrayList.size()) { str2 = ""; break; } C0498a aVar = arrayList.get(i2); if (!(aVar == null || TextUtils.isEmpty(aVar.f16346f))) { str2 = aVar.f16346f; break; } i2++; } if (TextUtils.isEmpty(str2)) { com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache: no available network info, ignore"); return; } String str3 = str2 + str; com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache: cacheKey = " + str3); synchronized (this.b) { if (this.a == null) { this.a = new HashMap<>(); } HashSet<C0498a> hashSet = this.a.get(str3); if (hashSet == null) { hashSet = new HashSet<>(); this.a.put(str3, hashSet); } for (int i3 = 0; i3 < arrayList.size(); i3++) { try { C0498a aVar2 = arrayList.get(i3); if (a(aVar2)) { C0498a clone = aVar2.clone(); hashSet.add(clone); com.tencent.klevin.download.b.w.a.a("KLEVINSDK_DOWNLOAD::DefaultDNS", "updateIPAddressCache, key=" + str3 + ", data=" + clone + ", curr ip size=" + hashSet.size()); } } catch (Exception e2) { e2.printStackTrace(); } } } } /* access modifiers changed from: package-private */ public boolean a(C0498a aVar) { if (aVar != null && !aVar.a() && i(aVar.b) && !h(aVar.b) && !g(aVar.b)) { return true; } return false; } private String a() { return this.f16344e.b(); } }