Skip to content

Commit

Permalink
Merge pull request #25 from lemondark/release/v4.6.0
Browse files Browse the repository at this point in the history
release/v4.6.0
  • Loading branch information
ilovetochangetheworld authored Aug 28, 2023
2 parents 629c728 + d99219c commit 9d1be27
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 101 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ DnsConfig dnsConfigBuilder = DnsConfig.Builder()
.preLookupDomains("baidu.com", "qq.com")
//(可选)手动指定网络栈支持情况,仅进行 IPv4 解析传 1,仅进行 IPv6 解析传 2,进行 IPv4、IPv6 双栈解析传 3。默认为根据客户端本地网络栈支持情况发起对应的解析请求。
.setCustomNetStack(3)
//(可选)设置域名解析请求超时时间,默认为1000ms
.timeoutMills(1000)
//(可选)设置域名解析请求超时时间,默认为2000ms
.timeoutMills(2000)
//(可选)[V4.4.0废弃]是否开启解析异常上报,默认false,不上报
.enableReport(true)
//(可选)[V4.1.0] 解析缓存自动刷新, 以域名形式进行配置,填写形式:"baidu.com", "qq.com"。配置的域名会在 TTL * 75% 时自动发起解析请求更新缓存,实现配置域名解析时始终命中缓存。此项建议不要设置太多域名,当前限制为最多 10 个域名。与预解析分开独立配置。
Expand Down Expand Up @@ -126,7 +126,7 @@ ii. 老版本初始化方法
* @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权
* @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98",以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准
* @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭
* @param timeout dns请求超时时间,单位ms,建议设置1000
* @param timeout dns请求超时时间,单位ms,建议设置2000
*/
MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dnsIp, debug, timeout);
```
Expand All @@ -144,7 +144,7 @@ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dns
* @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权
* @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求,channel为DesHttp和AesHttp时选择),"119.29.29.99"(仅支持 https 请求,channel为Https时选择)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准
* @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭
* @param timeout dns请求超时时间,单位ms,建议设置1000
* @param timeout dns请求超时时间,单位ms,建议设置2000
* @param enableReport 是否开启解析异常上报,默认false,不上报
*/
MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dnsIp, debug, timeout, enableReport);
Expand All @@ -163,7 +163,7 @@ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dns
* @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权
* @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求,channel为DesHttp和AesHttp时选择),"119.29.29.99"(仅支持 https 请求,channel为Https时选择)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准
* @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭
* @param timeout dns请求超时时间,单位ms,建议设置1000
* @param timeout dns请求超时时间,单位ms,建议设置2000
* @param channel 设置 channel,可选:DesHttp(默认), AesHttp, Https
* @param token 腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于 HTTPS 校验
* @param enableReport 是否开启解析异常上报,默认false,不上报
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/tencent/msdk/dns/DnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static final class Builder {
private String mDnsKey = "";
private String mToken = "";

private int mTimeoutMills = 1000;
private int mTimeoutMills = 2000;

private int mMaxNumOfPreLookupDomains = DEFAULT_MAX_NUM_OF_PRE_LOOKUP_DOMAINS;
private int mMaxNumOfIpRankItems = DEFAULT_MAX_NUM_OF_IP_RANK_ITEMS;
Expand Down Expand Up @@ -372,7 +372,7 @@ public Builder token(String token) {

/**
* 设置域名解析请求超时时间
* 不设置时, 默认为1000ms
* 不设置时, 默认为2000ms
*
* @param timeoutMills 域名解析请求超时时间, 单位为ms
* @return 当前Builder实例, 方便链式调用
Expand Down Expand Up @@ -590,7 +590,7 @@ public Builder https() {
*/
@Deprecated
public Builder enableReport(boolean enableReport) {
mEnableReport = enableReport;
// mEnableReport = enableReport;
return this;
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/tencent/msdk/dns/DnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.log.ILogNode;
import com.tencent.msdk.dns.base.network.NetworkChangeManager;
import com.tencent.msdk.dns.base.report.BeaconReporterInitParameters;
import com.tencent.msdk.dns.base.report.IReporter;
import com.tencent.msdk.dns.base.report.ReportManager;
import com.tencent.msdk.dns.base.utils.CommonUtils;
Expand Down Expand Up @@ -92,11 +91,11 @@ public void run() {
ActivityLifecycleDetector.install(appContext);
// Room 本地数据读取
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
Cache.readFromDb();
}
});
@Override
public void run() {
Cache.readFromDb();
}
});
ReportHelper.init(config);
DnsExecutors.sExecutorSupplier = sConfig.executorSupplier;
setLookedUpListener(config.lookedUpListener);
Expand Down Expand Up @@ -171,6 +170,7 @@ public static synchronized void setCachedIpEnable(boolean mCachedIpEnable) {

/**
* 设置是否上报,是否启用域名服务(获取底层配置)
*
* @param mEnableReport
* @param mEnableDomainServer
*/
Expand Down Expand Up @@ -334,14 +334,15 @@ public void run() {
try {
JSONObject temp = new JSONObject(result);
long expiredTime = Long.parseLong(temp.get("expired_time").toString());
final String requestDomain = temp.get("request_name").toString();
long current = System.currentTimeMillis();
if (expiredTime < current) {
// 缓存过期,发起异步请求
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
DnsLog.d("async look up send");
getAddrsByName(domain, true, true);
getAddrsByName(requestDomain, true, true);
}
});
// 缓存过期且不允许使用过期缓存
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void init(Context context, String appID, String dnsId, String dnsKey, Str
* @param dnsKey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权
* @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求),"119.29.29.99"(仅支持 https 请求)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准
* @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭
* @param timeout dns请求超时时间,单位ms,建议设置1000
* @param timeout dns请求超时时间,单位ms,建议设置2000
* @param channel 设置 channel,可选:DesHttp(默认), AesHttp, Https
* @param token 腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于 HTTPS 校验
*/
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/tencent/msdk/dns/base/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.core.IpSet;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public final class CommonUtils {
Expand Down Expand Up @@ -97,4 +101,18 @@ public static String getIpfromSet(IpSet ipSet) {
}
return v4Ip + ";" + v6Ip;
}

public static String[] templateIps(String[] ips, LookupParameters<LookupExtra> lookupParameters) {
String requestHostname = lookupParameters.requestHostname;
if (ips.length > 0 && !lookupParameters.requestHostname.equals(lookupParameters.hostname) && requestHostname.split(",").length == 1) {
// 批量解析中单个域名下发请求的格式处理
List<String> list = new ArrayList<>();
for (String ip : ips) {
list.add(requestHostname + ":" + ip);
}
return list.toArray(new String[list.size()]);
} else {
return ips;
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public static String getRequest(String urlStr) {
String host = url.getHost();
String file = url.getFile();
return GET_METHOD + ' ' + file + ' ' + HTTP_VERSION + CRLF +
"Connection: close" + CRLF +
HOST_HEADER + ": " + host + CRLF +
CRLF;
"Connection: keep-alive" + CRLF +
HOST_HEADER + ": " + host + CRLF +
CRLF;
} catch (MalformedURLException e) {
return "";
}
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/tencent/msdk/dns/core/DnsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ LookupResult<IStatisticsMerge> getResultFromCache(LookupParameters<LookupExtra>
sStatMergeFactory.create(
(Class<LookupExtra>) lookupExtra.getClass(), lookupParams.appContext);
lookupContext.statisticsMerge(statMerge);
statMerge.statContext(lookupContext);

IDns dns;
switch (currentNetworkStack) {
Expand All @@ -132,7 +131,8 @@ LookupResult<IStatisticsMerge> getResultFromCache(LookupParameters<LookupExtra>
}

LookupResult lookupResultFromCache = dns.getResultFromCache(lookupParams);
if (lookupResultFromCache.stat.lookupSuccess()) {
statMerge.statContext(lookupContext);
if (lookupResultFromCache.stat.lookupSuccess() || lookupResultFromCache.stat.lookupPartCached()) {
lookupContext.sorter().put(dns, lookupResultFromCache.ipSet.ips);
lookupContext.statisticsMerge()
.merge(dns, lookupResultFromCache.stat);
Expand All @@ -145,6 +145,7 @@ LookupResult<IStatisticsMerge> getResultFromCache(LookupParameters<LookupExtra>
lookupResult.ipSet + "; " + lookupResult.stat);
return lookupResult;
}

return new LookupResult<IStatisticsMerge>(
IpSet.EMPTY, statMerge);
}
Expand Down Expand Up @@ -238,13 +239,19 @@ LookupResult<IStatisticsMerge> lookup(LookupParameters<LookupExtra> lookupParams
// 暂时不忽略LocalDns解析结果(即超时时间内会等待LocalDns解析结果, 无论RestDns是否已经解析成功)
if (null != restDnsGroup) {
// 先查缓存,有其一即可
LookupResult<IStatisticsMerge> lookupResult = getResultFromCache(lookupParams);
DnsLog.d("getResultFromCache: " + lookupResult);
if (lookupResult.stat.lookupSuccess()) {
lookupResultHolder.mLookupResult = lookupResult;
DnsLog.d("DnsManager lookup getResultFromCache success");
return lookupResult;
LookupResult<IStatisticsMerge> lookupResultFromCache = getResultFromCache(lookupParams);
DnsLog.d("getResultFromCache: " + lookupResultFromCache);
if (lookupResultFromCache.stat.lookupSuccess()) {
if (lookupResultFromCache.stat.lookupPartCached()) {
// 仅部分命中缓存
lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet);
} else {
lookupResultHolder.mLookupResult = lookupResultFromCache;
DnsLog.d("DnsManager lookup getResultFromCache success");
return lookupResultFromCache;
}
}

// 打开Selector
prepareTasks(restDnsGroup, lookupContext);
if (!lookupContext.allDnsLookedUp() && null != localDnsGroup) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/IDns.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ interface IStatistics {
boolean lookupNeedRetry();

boolean lookupFailed();

boolean lookupPartCached();
}

DnsDescription getDescription();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/ISorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ interface IFactory {
// NOTE: ips可能为空数组
void put(IDns dns, String[] ips);

void putPartCache(IpSet ipSet);

IpSet sort();
}
5 changes: 5 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/IStatisticsMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public boolean lookupFailed() {
return false;
}

@Override
public boolean lookupPartCached() {
return false;
}

@Override
public String toJsonResult() {
return "{\"v4_ips\":\"\",\"v4_ttl\":\"\",\"v4_client_ip\":\"\",\"v6_ips\":\"\",\"v6_ttl\":\"\",\"v6_client_ip\":\"\"}";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/LookupContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public String hostname() {
return mLookupParams.hostname;
}

public String requestHostname() {
return mLookupParams.requestHostname;
}

public int timeoutMills() {
return mLookupParams.timeoutMills;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/LookupParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class LookupParameters<LookupExtra extends IDns.ILookupExtra> {
public final int curRetryTime;
public final boolean netChangeLookup;

public String requestHostname;

private LookupParameters(
Context appContext,
Expand All @@ -54,6 +55,7 @@ private LookupParameters(
this.enableAsyncLookup = enableAsyncLookup;
this.curRetryTime = curRetryTime;
this.netChangeLookup = netChangeLookup;
setRequestHostname(hostname);
}

@Override
Expand Down Expand Up @@ -81,6 +83,10 @@ public boolean equals(Object o) {
CommonUtils.equals(channel, that.channel);
}

public void setRequestHostname(String hostname) {
this.requestHostname = hostname;
}

@Override
public int hashCode() {
return CommonUtils.hash(appContext, hostname, timeoutMills, dnsIp, lookupExtra, channel,
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.util.Log;

import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.utils.CommonUtils;
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.DnsDescription;
import com.tencent.msdk.dns.core.IDns;
Expand Down Expand Up @@ -42,11 +43,11 @@ public LookupResult lookup(LookupParameters lookupParams) {
Statistics stat = new Statistics();
stat.startLookup();

String[] ips = lookup(lookupParams.hostname);
String[] ips = lookup(lookupParams.requestHostname);

stat.endLookup();
stat.ips = ips;
return new LookupResult<>(ips, stat);
return new LookupResult<>(CommonUtils.templateIps(stat.ips, lookupParams), stat);
}

@Override
Expand Down Expand Up @@ -129,5 +130,9 @@ public String toString() {
'}';
}

@Override
public boolean lookupPartCached() {
return false;
}
}
}
Loading

0 comments on commit 9d1be27

Please sign in to comment.