Skip to content

Commit

Permalink
Merge pull request #27 from lemondark/release/v4.8.0
Browse files Browse the repository at this point in the history
release: v4.8.0
  • Loading branch information
ilovetochangetheworld authored Dec 15, 2023
2 parents 71e8cfc + 7b7ca3a commit 2e3624a
Show file tree
Hide file tree
Showing 56 changed files with 942 additions and 760 deletions.
2 changes: 1 addition & 1 deletion proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# 对外接口类
-keep public class com.tencent.msdk.dns.DnsService {*;}
-keep public class com.tencent.msdk.dns.core.IpSet {*;}
-keep public class com.tencent.msdk.dns.core.ipRank.IpRankItem {*;}
-keep public class com.tencent.msdk.dns.core.rank.IpRankItem {*;}
-keep public class com.tencent.msdk.dns.DnsConfig {*;}
-keep public class com.tencent.msdk.dns.HttpDnsResponseObserver {*;}
-keep public class com.tencent.msdk.dns.DnsConfig$Builder {*;}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/tencent/msdk/dns/BackupResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private BackupResolver() {
// 尝试切回主ip的间隔时间,默认为10分钟
private final long mInterval = 10 * 60 * 1000;

public static BackupResolver getInstance() {//静态get方法
public static BackupResolver getInstance() {
// 静态get方法
if (mBackupResolver == null) {
synchronized (BackupResolver.class) {
if (mBackupResolver == null) {
Expand All @@ -53,7 +54,7 @@ public static BackupResolver getInstance() {//静态get方法
public void init(DnsConfig dnsConfig) {
mConfig = dnsConfig;
mErrorCount = new AtomicInteger(0);
// http和https是两个IP
// http和https是两个IP
dnsIps = getBackUpIps();
}

Expand All @@ -66,9 +67,11 @@ public void getServerIps() {

private ArrayList getBackUpIps() {
if (Const.HTTPS_CHANNEL.equals(mConfig.channel) && !BuildConfig.HTTPS_TOLERANCE_SERVER.isEmpty()) {
return new ArrayList<String>(Arrays.asList(BuildConfig.HTTPS_INIT_SERVER, BuildConfig.HTTPS_TOLERANCE_SERVER));
return new ArrayList<String>(Arrays.asList(BuildConfig.HTTPS_INIT_SERVER,
BuildConfig.HTTPS_TOLERANCE_SERVER));
} else {
return new ArrayList<String>(Arrays.asList(BuildConfig.HTTP_INIT_SERVER, BuildConfig.HTTP_TOLERANCE_SERVER));
return new ArrayList<String>(Arrays.asList(BuildConfig.HTTP_INIT_SERVER,
BuildConfig.HTTP_TOLERANCE_SERVER));
}
}

Expand Down Expand Up @@ -97,12 +100,10 @@ public boolean getCanReport(int errorCount) {

/**
* 主IP故障,切换备份IP策略
* 1. 主备IP切换:在精确性、速度上折中处理,主IP解析的同时,会发起LocalDNS解析,若主IP首次解析不成功,立即返回 上次解析结果,如果没有上次解析结果,则返回LocalDNS解析结果,如果主IP 3次解析不成功,则切换到备份IP进行解析。
* <p>
* 2. 备份IP 切换 域名兜底:所有备份IP都经超过3次不通,切换到域名兜底解析。
* <p>
* 1. 主备IP切换:在精确性、速度上折中处理,主IP解析的同时,会发起LocalDNS解析,若主IP首次解析不成功,立即返回
* 上次解析结果,如果没有上次解析结果,则返回LocalDNS解析结果,如果主IP 3次解析不成功,则切换到备份IP进行解析。
* 2. 备份IP切换 域名兜底:所有备份IP都经超过3次不通,切换到域名兜底解析。
* 3. 恢复:每隔10min切回测试一次主IP(不主动探测主备IP是否恢复)
* <p>
* 4. 通过参数控制 切换策略的次数判断(默认3次)、恢复主IP策略的时间间隔(默认10min)
*/
public String getDnsIp() {
Expand Down Expand Up @@ -143,7 +144,8 @@ public void run() {
try {
String dnsIp = BackupResolver.getInstance().getDnsIp();
String domain = BuildConfig.DOMAIN_SERVICE_DOMAINS[0];
LookupExtra lookupExtra = new LookupExtra(BuildConfig.DOMAIN_SERVICE_ID, BuildConfig.DOMSIN_SERVICE_KEY, BuildConfig.DOMAIN_SERVICE_TOKEN);
LookupExtra lookupExtra = new LookupExtra(BuildConfig.DOMAIN_SERVICE_ID,
BuildConfig.DOMSIN_SERVICE_KEY, BuildConfig.DOMAIN_SERVICE_TOKEN);
LookupParameters lookupParameters = new LookupParameters.Builder<LookupExtra>()
.dnsIp(dnsIp)
.channel("DesHttp")
Expand Down
141 changes: 76 additions & 65 deletions src/main/java/com/tencent/msdk/dns/DnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.tencent.msdk.dns.base.report.IReporter;
import com.tencent.msdk.dns.base.utils.CommonUtils;
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.ipRank.IpRankItem;
import com.tencent.msdk.dns.core.rank.IpRankItem;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;

import java.util.ArrayList;
Expand Down Expand Up @@ -62,16 +62,13 @@ public final class DnsConfig {

public String routeIp;

private DnsConfig(int logLevel,
String appId, String userId, boolean initBuiltInReporters,
String dnsId, String dnsKey, String token,
int timeoutMills,
Set<WildcardDomain> protectedDomains,
Set<String> preLookupDomains, boolean enablePersistentCache, Set<String> persistentCacheDomains,
Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
private DnsConfig(int logLevel, String appId, String userId, boolean initBuiltInReporters, String dnsId,
String dnsKey, String token, int timeoutMills, Set<WildcardDomain> protectedDomains,
Set<String> preLookupDomains, boolean enablePersistentCache, Set<String> persistentCacheDomains
, Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
int customNetStack, DnsExecutors.ExecutorSupplier executorSupplier,
ILookedUpListener lookedUpListener, List<ILogNode> logNodes,
List<IReporter> reporters, boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp) {
ILookedUpListener lookedUpListener, List<ILogNode> logNodes, List<IReporter> reporters,
boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp) {
this.logLevel = logLevel;
this.appId = appId;
this.userId = userId;
Expand Down Expand Up @@ -114,30 +111,30 @@ boolean needProtect(/* @Nullable */String hostname) {

@Override
public String toString() {
return "DnsConfig{" +
"logLevel=" + logLevel +
", appId='" + appId + '\'' +
", userId='" + userId + '\'' +
", lookupExtra=" + lookupExtra +
", timeoutMills=" + timeoutMills +
", protectedDomains=" + CommonUtils.toString(protectedDomains) +
", preLookupDomains=" + CommonUtils.toString(preLookupDomains) +
", enablePersistentCache=" + enablePersistentCache +
", persistentCacheDomains=" + CommonUtils.toString(persistentCacheDomains) +
", IpRankItems=" + CommonUtils.toString(ipRankItems) +
", channel='" + channel + '\'' +
", enableReport='" + enableReport + '\'' +
", blockFirst=" + blockFirst +
", customNetStack=" + customNetStack +
", executorSupplier=" + executorSupplier +
", lookedUpListener=" + lookedUpListener +
", logNodes=" + CommonUtils.toString(logNodes) +
", reporters=" + CommonUtils.toString(reporters) +
", useExpiredIpEnable=" + useExpiredIpEnable +
", cachedIpEnable=" + cachedIpEnable +
", enableDomainServer=" + enableDomainServer +
", routeIp=" + routeIp +
'}';
return "DnsConfig{"
+ "logLevel=" + logLevel
+ ", appId='" + appId + '\''
+ ", userId='" + userId + '\''
+ ", lookupExtra=" + lookupExtra
+ ", timeoutMills=" + timeoutMills
+ ", protectedDomains=" + CommonUtils.toString(protectedDomains)
+ ", preLookupDomains=" + CommonUtils.toString(preLookupDomains)
+ ", enablePersistentCache=" + enablePersistentCache
+ ", persistentCacheDomains=" + CommonUtils.toString(persistentCacheDomains)
+ ", IpRankItems=" + CommonUtils.toString(ipRankItems)
+ ", channel='" + channel + '\''
+ ", enableReport='" + enableReport + '\''
+ ", blockFirst=" + blockFirst
+ ", customNetStack=" + customNetStack
+ ", executorSupplier=" + executorSupplier
+ ", lookedUpListener=" + lookedUpListener
+ ", logNodes=" + CommonUtils.toString(logNodes)
+ ", reporters=" + CommonUtils.toString(reporters)
+ ", useExpiredIpEnable=" + useExpiredIpEnable
+ ", cachedIpEnable=" + cachedIpEnable
+ ", enableDomainServer=" + enableDomainServer
+ ", routeIp=" + routeIp
+ '}';
}

/* @VisibleForTesting */
Expand Down Expand Up @@ -169,10 +166,10 @@ boolean contains(String hostname) {

@Override
public String toString() {
return "WildcardDomain{" +
"mIsWildcard=" + mIsWildcard +
", mNakedDomain='" + mNakedDomain + '\'' +
'}';
return "WildcardDomain{"
+ "mIsWildcard=" + mIsWildcard
+ ", mNakedDomain='" + mNakedDomain + '\''
+ '}';
}
}

Expand Down Expand Up @@ -232,14 +229,18 @@ public static final class Builder {
* SDK默认仅将日志通过logcat输出, tag统一使用HTTPDNS
* 不设置时, 默认输出<a href="https://developer.android.google.cn/reference/android/util/Log.html#WARN">WARN</a>及以上等级的日志
*
* @param logLevel 最低日志等级, 使用<a href="https://developer.android.google.cn/reference/android/util/Log">Log</a>类定义的常量, 可选值为
* @param logLevel 最低日志等级, 使用<a href="https://developer.android.google.cn/reference/android/util/Log">Log</a>
* 类定义的常量, 可选值为
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#VERBOSE">VERBOSE</a>,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#DEBUG">DEBUG</a>,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#DEBUG">DEBUG</a>
* ,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#INFO">INFO</a>,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#WARN">WARN</a>,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#ERROR">ERROR</a>,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#ERROR">ERROR</a>
* ,
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#ASSERT">ASSERT</a>,
* 其中<a href="https://developer.android.google.cn/reference/android/util/Log.html#ASSERT">ASSERT</a>即不输出任何日志
* 其中
* <a href="https://developer.android.google.cn/reference/android/util/Log.html#ASSERT">ASSERT</a>即不输出任何日志
* @return 当前Builder实例, 方便链式调用
*/
public Builder logLevel(int logLevel) {
Expand Down Expand Up @@ -280,7 +281,7 @@ public Builder userId(String userId) {
/**
* 启停缓存自动刷新功能, 默认开启
*
* @param enablePersistentCache, 启停缓存自动刷新功能
* @param enablePersistentCache 启停缓存自动刷新功能
* @return 当前Builder实例, 方便链式调用
*/
public Builder enablePersistentCache(boolean enablePersistentCache) {
Expand Down Expand Up @@ -342,12 +343,13 @@ public Builder dnsId(String dnsId) {
/**
* 设置DnsKey
*
* @param dnsKey dnsKey, 即HTTPDNS服务的授权Id对应的加密密钥, 从<a href="https://console.cloud.tencent.com/HttpDNS">腾讯云官网</a>申请获得
* @param dnsKey dnsKey, 即HTTPDNS服务的授权Id对应的加密密钥, 从<a href="https://console.cloud.tencent.com/HttpDNS">腾讯云官网</a>
* 申请获得
* @return 当前Builder实例, 方便链式调用
* @throws IllegalArgumentException dnsKey为空时抛出
*/
public Builder dnsKey(String dnsKey) {
if (TextUtils.isEmpty(dnsKey)) {
if (mChannel != Const.HTTPS_CHANNEL && TextUtils.isEmpty(dnsKey)) {
throw new IllegalArgumentException("dnsKey".concat(Const.EMPTY_TIPS));
}
mDnsKey = dnsKey;
Expand Down Expand Up @@ -391,8 +393,7 @@ public Builder timeoutMills(int timeoutMills) {
*/
public Builder maxNumOfPreLookupDomains(int maxNumOfPreLookupDomains) {
if (0 >= maxNumOfPreLookupDomains) {
throw new IllegalArgumentException(
"maxNumOfPreLookupDomains".concat(Const.LESS_THAN_0_TIPS));
throw new IllegalArgumentException("maxNumOfPreLookupDomains".concat(Const.LESS_THAN_0_TIPS));
}
mMaxNumOfPreLookupDomains = maxNumOfPreLookupDomains;
return this;
Expand Down Expand Up @@ -558,7 +559,8 @@ public Builder ipRankItems(List<IpRankItem> ipRankItems) {

public Builder channel(String channel) {
if (channel.equals(Const.HTTPS_CHANNEL) && BuildConfig.FLAVOR.equals("intl")) {
throw new IllegalArgumentException("httpdns-sdk-intl version still doesn't support " + Const.HTTPS_CHANNEL);
throw new IllegalArgumentException("httpdns-sdk-intl version still doesn't support "
+ Const.HTTPS_CHANNEL);
}
mChannel = channel;
return this;
Expand All @@ -576,7 +578,8 @@ public Builder desHttp() {

public Builder https() {
if (BuildConfig.FLAVOR.equals("intl")) {
throw new IllegalArgumentException("httpdns-sdk-intl version still doesn't support " + Const.HTTPS_CHANNEL);
throw new IllegalArgumentException("httpdns-sdk-intl version still doesn't support "
+ Const.HTTPS_CHANNEL);
}
mChannel = Const.HTTPS_CHANNEL;
return this;
Expand Down Expand Up @@ -618,16 +621,17 @@ public Builder nonBlockFirst() {

/**
* 设置{@link DnsExecutors.ExecutorSupplier}, 用于为SDK定制线程池
* 不设置时, 默认使用<a href="https://developer.android.com/reference/android/os/AsyncTask.html#THREAD_POOL_EXECUTOR">THREAD_POOL_EXECUTOR</a>作为SDK内部使用的线程池
* 不设置时, 默认使用
* <a href="https://developer.android.com/reference/android/os/AsyncTask.html#THREAD_POOL_EXECUTOR">THREAD_POOL_EXECUTOR</a>作为SDK内部使用的线程池
*
* @param executorSupplier {@link DnsExecutors.ExecutorSupplier}接口实现类实例, SDK通过{@link DnsExecutors.ExecutorSupplier#get()}获取SDK内部使用的线程池
* @param executorSupplier {@link DnsExecutors.ExecutorSupplier}接口实现类实例,
* SDK通过{@link DnsExecutors.ExecutorSupplier#get()}获取SDK内部使用的线程池
* @return 当前Builder实例, 方便链式调用
* @throws IllegalArgumentException executorSupplier为null时抛出
*/
public Builder executorSupplier(DnsExecutors.ExecutorSupplier executorSupplier) {
if (null == executorSupplier) {
throw new IllegalArgumentException(
"executorSupplier".concat(Const.NULL_POINTER_TIPS));
throw new IllegalArgumentException("executorSupplier".concat(Const.NULL_POINTER_TIPS));
}
mExecutorSupplier = executorSupplier;
return this;
Expand All @@ -642,8 +646,7 @@ public Builder executorSupplier(DnsExecutors.ExecutorSupplier executorSupplier)
*/
public Builder lookedUpListener(ILookedUpListener lookedUpListener) {
if (null == lookedUpListener) {
throw new IllegalArgumentException(
"lookedUpListener".concat(Const.NULL_POINTER_TIPS));
throw new IllegalArgumentException("lookedUpListener".concat(Const.NULL_POINTER_TIPS));
}
mLookedUpListener = lookedUpListener;
return this;
Expand Down Expand Up @@ -690,8 +693,10 @@ public Builder setCustomNetStack(int customNetStack) {
* 允许使用过期缓存
*
* @param useExpiredIpEnable 默认false,解析时先取未过期的缓存结果,不满足则等待解析请求完成后返回解析结果
* 设置为true时,会直接返回缓存的解析结果,没有缓存则返回0;0,用户可使用localdns(InetAddress)进行兜底。且在无缓存结果或缓存已过期时,会异步发起解析请求更新缓存。
* 因异步API(getAddrByNameAsync,getAddrsByNameAsync)逻辑在回调中始终返回未过期的解析结果,设置为true时,异步API不可使用。建议使用同步API (getAddrByName,getAddrsByName)
* 设置为true时,会直接返回缓存的解析结果,没有缓存则返回0;
* 0,用户可使用localdns(InetAddress)进行兜底。且在无缓存结果或缓存已过期时,会异步发起解析请求更新缓存。
* 因异步API(getAddrByNameAsync,getAddrsByNameAsync)逻辑在回调中始终返回未过期的解析结果,设置为true时,异步API
* 不可使用。建议使用同步API (getAddrByName,getAddrsByName)
* @return 当前Builder实例, 方便链式调用
*/
public Builder setUseExpiredIpEnable(boolean useExpiredIpEnable) {
Expand Down Expand Up @@ -728,14 +733,20 @@ public Builder routeIp(String routeIp) {
* @return DnsConfig实例
*/
public DnsConfig build() {
return new DnsConfig(mLogLevel,
mAppId, mUserId, mInitBuiltInReporters, mDnsId, mDnsKey, mToken,
mTimeoutMills,
mProtectedDomains, mPreLookupDomains, mEnablePersistentCache, mPersistentCacheDomains,
mIpRankItems, mChannel, mEnableReport, mBlockFirst,
mCustomNetStack, mExecutorSupplier,
mLookedUpListener, mLogNodes,
mReporters, mUseExpiredIpEnable, mCachedIpEnable, mRouteIp);
if (TextUtils.isEmpty(mDnsId)) {
throw new IllegalArgumentException("dnsId".concat(Const.EMPTY_TIPS));
}
if (mChannel != Const.HTTPS_CHANNEL && TextUtils.isEmpty(mDnsKey)) {
throw new IllegalArgumentException("dnsKey".concat(Const.EMPTY_TIPS));
}
if (mChannel == Const.HTTPS_CHANNEL && TextUtils.isEmpty(mToken)) {
throw new IllegalArgumentException("token".concat(Const.EMPTY_TIPS));
}
return new DnsConfig(mLogLevel, mAppId, mUserId, mInitBuiltInReporters, mDnsId, mDnsKey, mToken,
mTimeoutMills, mProtectedDomains, mPreLookupDomains, mEnablePersistentCache,
mPersistentCacheDomains, mIpRankItems, mChannel, mEnableReport, mBlockFirst, mCustomNetStack,
mExecutorSupplier, mLookedUpListener, mLogNodes, mReporters, mUseExpiredIpEnable, mCachedIpEnable
, mRouteIp);
}
}
}
Loading

0 comments on commit 2e3624a

Please sign in to comment.