QQ浏览器 v12.2.3.7053版本的 MD5 值为:8285ab3059e5c8b521a264dfbc5c3685
以下内容为反编译后的 DnsManager.java 源代码,内容仅作参考
package com.tencent.common.serverconfig;
import android.content.Context;
import android.text.TextUtils;
import com.tencent.basesupport.FLogger;
import com.tencent.common.connectivitydetect.ConnectivityDetector;
import com.tencent.common.dns.IDnsResolver;
import com.tencent.common.manifest.annotation.CreateMethod;
import com.tencent.common.manifest.annotation.ServiceImpl;
import com.tencent.common.threadpool.BrowserExecutorSupplier;
import com.tencent.common.utils.FileUtilsF;
import com.tencent.common.utils.StringUtils;
import com.tencent.mtt.ContextHolder;
import com.tencent.mtt.compliance.MethodDelegate;
import com.tencent.mtt.network.QBUrl;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.security.Security;
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;
import tmsdk.common.gourd.vine.IActionReportService;
@ServiceImpl(createMethod = CreateMethod.GET, service = IDnsResolver.class)
public class DnsManager implements IDnsResolver {
HashMap<String, Long> f9928a;
Object f9929b;
private HashMap<String, HashSet<DnsData>> f9930c;
private Object f9931d;
private Context e;
private Random f;
private HashMap<String, ReentrantLock> g;
private Pattern h;
public class DnsData {
String f9935a;
public String f9936b;
long f9937c;
long f9938d = 1200000;
public String e = "NULL";
public String f = "";
public int g = 0;
public int h = -1;
public DnsData() {
}
boolean a() {
return System.currentTimeMillis() >= this.f9937c + this.f9938d;
}
public DnsData clone() {
DnsData dnsData = new DnsData();
dnsData.f9935a = this.f9935a;
dnsData.f9936b = this.f9936b;
dnsData.f9937c = this.f9937c;
dnsData.f9938d = this.f9938d;
dnsData.e = this.e;
dnsData.f = this.f;
return dnsData;
}
public boolean equals(Object obj) {
if (!(obj instanceof DnsData)) {
return false;
}
DnsData dnsData = (DnsData) obj;
return StringUtils.a(this.f9936b, dnsData.f9936b) && StringUtils.a(this.f9935a, dnsData.f9935a);
}
public int hashCode() {
return (TextUtils.isEmpty(this.f9936b) ? 0 : this.f9936b.hashCode()) ^ (TextUtils.isEmpty(this.f9935a) ? 0 : this.f9935a.hashCode());
}
public String toString() {
return "[domain=" + this.f9935a + ", ip=" + this.f9936b + ", TTL=" + this.f9938d + ", expired=" + a() + ", type=" + this.e + ", netInfo=" + this.f + ", failTimes=" + this.g + "]";
}
}
public static class DnsManagerHolder {
static final DnsManager f9939a = new DnsManager();
private DnsManagerHolder() {
}
}
private DnsManager() {
this.f9930c = null;
this.f9931d = new Object();
this.f9928a = new HashMap<>();
this.f9929b = new Object();
this.f = new Random();
this.g = new HashMap<>();
this.h = null;
this.e = ContextHolder.getAppContext();
try {
Security.setProperty("networkaddress.cache.ttl", "0");
this.h = Pattern.compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])", 2);
} catch (Exception unused) {
}
}
private DnsData a(String str, boolean z) {
return a(str, z, (String) null);
}
private DnsData a(String str, boolean z, String str2) {
int i;
if (TextUtils.isEmpty(str2)) {
str2 = a();
}
String str3 = str2 + str;
synchronized (this.f9931d) {
DnsData dnsData = null;
if (this.f9930c == null) {
FLogger.d("DnsManager", "getIPAdressFromCache : mIPAdressCache = null, return");
return null;
}
FLogger.d("DnsManager", "getIPAdressFromCache BEGINS check dns: cacheKey = " + str3);
HashSet<DnsData> hashSet = this.f9930c.get(str3);
String str4 = "current cacheKey=" + str3 + ": ";
if (hashSet != null) {
Iterator<DnsData> it = hashSet.iterator();
while (it.hasNext()) {
DnsData next = it.next();
if (a(next)) {
str4 = str4 + next.f9936b + ": " + next.g + "; ";
} else {
FLogger.d("DnsManager", "getIPAdressFromCache, " + next + ", is out of date, remove it");
it.remove();
}
}
FLogger.d("DnsManager", "getIPAdressFromCache, " + str4);
Object[] array = hashSet.toArray();
i = array == null ? 0 : array.length;
if (i > 0) {
dnsData = a(array);
FLogger.d("DnsManager", "getIPAdressFromCache, OK key=" + str3 + ", value=" + dnsData);
}
} else {
i = -1;
}
if (z && i < 2) {
a(str, str2, true);
}
return dnsData;
}
}
private DnsData a(Object[] objArr) {
if (objArr == null || objArr.length == 0) {
return null;
}
DnsData dnsData = (DnsData) objArr[0];
for (int i = 0; i < objArr.length; i++) {
if (((DnsData) objArr[i]).g < dnsData.g) {
dnsData = (DnsData) objArr[i];
}
}
ArrayList arrayList = new ArrayList();
for (int i2 = 0; i2 < objArr.length; i2++) {
if (((DnsData) objArr[i2]).g == dnsData.g) {
arrayList.add((DnsData) objArr[i2]);
}
}
int size = arrayList.size();
return size <= 1 ? dnsData : (DnsData) arrayList.get(this.f.nextInt(size));
}
private void a(final String str, String str2, final boolean z) {
if (!ConnectivityDetector.c()) {
FLogger.d("DnsManager", "tryGetMoreDnsData begins, but current net is not available, ignore");
return;
}
String str3 = str + str2;
if (z) {
synchronized (this.f9929b) {
Long l = this.f9928a.get(str3);
if (l != null) {
if (System.currentTimeMillis() - l.longValue() < 900000) {
FLogger.d("DnsManager", "tryGetMoreDnsData, domain=" + str + ", netInfo=" + str2 + ", req too frequent, ignore");
return;
}
} else {
this.f9928a.put(str3, Long.valueOf(System.currentTimeMillis()));
}
}
}
BrowserExecutorSupplier.postForIoTasks(new BrowserExecutorSupplier.BackgroundRunable() {
@Override
public void doRun() {
FLogger.d("DnsManager", "tryGetMoreDnsData, needTencentDns=" + z);
if (z) {
ArrayList<DnsData> c2 = DnsManager.this.c(str);
FLogger.d("DnsManager", "tryGetMoreDnsData, from tencent dns ok, value=" + c2.toString());
DnsManager.this.a(str, c2);
}
DnsData b2 = DnsManager.this.b(str);
if (DnsManager.this.a(b2)) {
FLogger.d("DnsManager", "tryGetMoreDnsData, from sys dns ok, value=" + b2);
DnsManager.this.a(str, b2);
}
}
});
}
public void a(String str, ArrayList<DnsData> arrayList) {
String str2;
String str3;
String str4;
if (arrayList == null || arrayList.isEmpty() || TextUtils.isEmpty(str)) {
str2 = "DnsManager";
str3 = "updateIPAdressCache: data is null or empty, ignore";
} else {
int i = 0;
while (true) {
if (i >= arrayList.size()) {
str4 = "";
break;
}
DnsData dnsData = arrayList.get(i);
if (dnsData != null && !TextUtils.isEmpty(dnsData.f)) {
str4 = dnsData.f;
break;
}
i++;
}
if (!TextUtils.isEmpty(str4)) {
String str5 = str4 + str;
FLogger.d("DnsManager", "updateIPAdressCache: cacheKey = " + str5);
synchronized (this.f9931d) {
if (this.f9930c == null) {
this.f9930c = new HashMap<>();
}
HashSet<DnsData> hashSet = this.f9930c.get(str5);
if (hashSet == null) {
hashSet = new HashSet<>();
this.f9930c.put(str5, hashSet);
}
for (int i2 = 0; i2 < arrayList.size(); i2++) {
try {
DnsData dnsData2 = arrayList.get(i2);
if (a(dnsData2)) {
DnsData clone = dnsData2.clone();
hashSet.add(clone);
FLogger.d("DnsManager", "updateIPAdressCache, key=" + str5 + ", data=" + clone + ", curr ip size=" + hashSet.size());
}
} catch (Exception unused) {
}
}
}
return;
}
str2 = "DnsManager";
str3 = "updateIPAdressCache: no available network info, ignore";
}
FLogger.d(str2, str3);
}
private DnsData d(String str) {
DnsData dnsData;
ReentrantLock e = e(str);
boolean tryLock = e.tryLock();
if (!tryLock) {
FLogger.d("DnsManager", "syncGetIPAddessFromTencentDNS, lock for " + str + " is already Locked, wait");
e.lock();
}
try {
FLogger.d("DnsManager", "syncGetIPAddessFromTencentDNS, lock for " + str + " acuqired, begin get ips");
if (!tryLock) {
dnsData = a(str, false);
if (!a(dnsData)) {
FLogger.d("DnsManager", "syncGetIPAddessFromTencentDNS, from cache faile, domain=" + str);
}
if (dnsData == null) {
FLogger.d("DnsManager", "syncGetIPAddessFromTencentDNS, dnsData cache not valid, begin real get");
ArrayList<DnsData> c2 = c(str);
if (c2.size() > 0) {
dnsData = c2.get(this.f.nextInt(c2.size()));
if (a(dnsData)) {
FLogger.d("DnsManager", "syncGetIPAddessFromTencentDNS, begin update current cache, value=" + dnsData);
a(str, c2);
} else {
dnsData = null;
}
}
}
return dnsData;
}
dnsData = null;
if (dnsData == null) {
}
return dnsData;
} finally {
e.unlock();
}
}
private ReentrantLock e(String str) {
ReentrantLock reentrantLock = this.g.get(str);
if (reentrantLock != null) {
return reentrantLock;
}
ReentrantLock reentrantLock2 = new ReentrantLock();
this.g.put(str, reentrantLock2);
return reentrantLock2;
}
private boolean f(String str) {
return j(str) && !g(str);
}
private boolean g(String str) {
String[] split;
if (TextUtils.isEmpty(str) || (split = str.split("\\.")) == null || split.length != 4) {
return true;
}
String str2 = split[0];
for (int i = 1; i < split.length; i++) {
if (!StringUtils.a(str2, split[i])) {
return false;
}
str2 = split[i];
}
return true;
}
public static DnsManager getInstance() {
return DnsManagerHolder.f9939a;
}
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 i = 0; i < iArr.length; i++) {
try {
iArr[i] = Integer.valueOf(split[i]).intValue();
} catch (Exception unused) {
}
}
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;
}
private boolean i(String str) {
return !TextUtils.isEmpty(str) && str.contains("127.0.0.1");
}
private boolean j(String str) {
if (TextUtils.isEmpty(str)) {
return false;
}
try {
return this.h.matcher(str).matches();
} catch (Exception unused) {
return false;
}
}
public DnsData a(String str) {
return a(str, (String) null);
}
public DnsData a(String str, String str2) {
if (TextUtils.isEmpty(str)) {
return null;
}
FLogger.d("DnsManager", "getIPAdressSync, from cache, domain=" + str);
DnsData a2 = a(str, true, str2);
if (a2 != null) {
FLogger.d("DnsManager", "getIPAdressSync, from cache SUCC, value=" + a2);
return a2;
}
FLogger.d("DnsManager", "getIPAdressSync, from cache fail, now get from tencent dns, domain=" + str);
DnsData d2 = d(str);
if (d2 != null) {
FLogger.d("DnsManager", "getIPAdressSync, from tencent dns SUCC, value=" + d2);
a(str, a(), false);
return d2;
}
FLogger.d("DnsManager", "getIPAdressSync, from tencent dns fail, now get from sys dns, domain=" + str);
DnsData b2 = b(str);
if (!a(b2)) {
FLogger.d("DnsManager", "getIPAdressSync, from sys dns fail, all fail, domain=" + str);
return null;
}
FLogger.d("DnsManager", "getIPAdressSync, from sys dns SUCC, value=" + b2);
a(str, b2);
return b2;
}
String a() {
return IPListUtils.c(this.e).toString();
}
void a(String str, DnsData dnsData) {
ArrayList<DnsData> arrayList = new ArrayList<>();
arrayList.add(dnsData);
a(str, arrayList);
}
boolean a(DnsData dnsData) {
return (dnsData == null || dnsData.a() || !f(dnsData.f9936b) || h(dnsData.f9936b) || i(dnsData.f9936b)) ? false : true;
}
DnsData b(String str) {
String a2;
InetAddress[] allByName;
ArrayList arrayList = new ArrayList();
try {
a2 = a();
FLogger.d("DnsManager", "getIPAdressFromSystemDNS, domain=" + str);
allByName = InetAddress.getAllByName(str);
} catch (Throwable unused) {
}
if (allByName == null) {
return null;
}
if (!StringUtils.a(a2, a())) {
FLogger.d("DnsManager", "network has changed, before = " + a2 + ", ignore these ips");
return null;
}
for (InetAddress inetAddress : allByName) {
if (inetAddress == null) {
return null;
}
if (inetAddress instanceof Inet6Address) {
FLogger.d("DnsManager", "updateIPAdressCache, is ipv6, value=" + MethodDelegate.getHostAddress(inetAddress));
} else {
String hostAddress = MethodDelegate.getHostAddress(inetAddress);
if (f(hostAddress)) {
DnsData dnsData = new DnsData();
dnsData.f9935a = str;
dnsData.f9936b = hostAddress;
dnsData.f9937c = System.currentTimeMillis();
dnsData.e = "SYS";
dnsData.f9938d = 1200000L;
dnsData.f = new String(a2);
FLogger.d("DnsManager", "updateIPAdressCache, is ipv4, value=" + dnsData);
arrayList.add(dnsData);
}
}
}
if (arrayList.isEmpty()) {
return null;
}
int nextInt = this.f.nextInt(arrayList.size());
FLogger.d("DnsManager", "getIPAdressFromSystemDNS, finally return ip = " + ((DnsData) arrayList.get(nextInt)).f9936b + ", domain=" + str);
return (DnsData) arrayList.get(nextInt);
}
ArrayList<DnsData> c(String str) {
String a2;
int i;
String str2;
ArrayList<DnsData> arrayList = new ArrayList<>();
FLogger.d("DnsManager", "getIPAdressFromTencentDnsServer, domain=" + str);
try {
a2 = a();
HttpURLConnection httpURLConnection = (HttpURLConnection) new QBUrl("http://119.29.29.29/d?dn=" + str).a(false).a("dns").e();
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 inputStream = httpURLConnection.getInputStream();
ByteBuffer a3 = FileUtilsF.a(inputStream);
str2 = new String(a3.array(), 0, a3.position(), "UTF-8");
FileUtilsF.f().a(a3);
inputStream.close();
httpURLConnection.disconnect();
} catch (Throwable th) {
FLogger.d("DnsManager", "getIPAdressFromTencentDnsServer, Failed error = " + th.getMessage());
}
if (!StringUtils.a(a2, a())) {
FLogger.d("DnsManager", "network has changed, before = " + a2 + ", ignore these ips");
return arrayList;
}
String[] split = str2.split(IActionReportService.COMMON_SEPARATOR);
for (i = 0; i < split.length; i++) {
if (f(split[i])) {
DnsData dnsData = new DnsData();
dnsData.f9935a = str;
dnsData.f9936b = split[i];
dnsData.f9937c = System.currentTimeMillis();
dnsData.e = "TENCENT";
dnsData.f = new String(a2);
arrayList.add(dnsData);
FLogger.d("DnsManager", "getIPAdressFromTencentDnsServer, ok, domain=" + str + ", value=" + dnsData);
}
}
return arrayList;
}
@Override
public String resolveDomain(String str) {
DnsData a2 = a(str);
if (a2 != null) {
return a2.f9936b;
}
return null;
}
}