Skip to content

Commit

Permalink
Merge pull request #23 from lemondark/feature/domainServer
Browse files Browse the repository at this point in the history
feature: v4.4.0版本
  • Loading branch information
ilovetochangetheworld authored May 25, 2023
2 parents 4946870 + 0da562f commit ce3ce30
Show file tree
Hide file tree
Showing 31 changed files with 814 additions and 310 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ext.subVersionCode = Integer.valueOf(String.format('%d%02d%02d',
android {
dependencies {
// ...
implementation project(path: ':httpdns-sdk-android'
implementation project(path: ':httpdns-sdk-android')
}
```

Expand Down Expand Up @@ -85,8 +85,17 @@ DnsConfig dnsConfigBuilder = DnsConfig.Builder()
.setCustomNetStack(3)
//(可选)设置域名解析请求超时时间,默认为1000ms
.timeoutMills(1000)
//(可选)是否开启解析异常上报,默认false,不上报
//(可选)[V4.4.0废弃]是否开启解析异常上报,默认false,不上报
.enableReport(true)
//(可选)[V4.1.0] 解析缓存自动刷新, 以域名形式进行配置,填写形式:"baidu.com", "qq.com"。配置的域名会在 TTL * 75% 时自动发起解析请求更新缓存,实现配置域名解析时始终命中缓存。此项建议不要设置太多域名,当前限制为最多 10 个域名。与预解析分开独立配置。
.persistentCacheDomains("baidu.com", "qq.com")
//(可选)[V4.2.0] IP 优选,以 IpRankItem(hostname, port) 组成的 List 配置, port(可选)默认值为 8080。例如:IpRankItem("qq.com", 443)。sdk 会根据配置项进行 socket 连接测速情况对解析 IP 进行排序,IP 优选不阻塞当前解析,在下次解析时生效。当前限制为最多 10 项。
.ipRankItems(ipRankItemList)
//(可选)[V4.3.0] 设置是否允许使用过期缓存,默认false,解析时先取未过期的缓存结果,不满足则等待解析请求完成后返回解析结果。
// 设置为true时,会直接返回缓存的解析结果,没有缓存则返回0;0,用户可使用localdns(InetAddress)进行兜底。且在无缓存结果或缓存已过期时,会异步发起解析请求更新缓存。因异步API(getAddrByNameAsync,getAddrsByNameAsync)逻辑在回调中始终返回未过期的解析结果,设置为true时,异步API不可使用。建议使用同步API (getAddrByName,getAddrsByName)。
.setUseExpiredIpEnable(true)
//(可选)[V4.3.0] 设置是否启用本地缓存(Room),默认false
.setCachedIpEnable(true)
// 以build()结束
.build();

Expand Down
96 changes: 65 additions & 31 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply from: 'upload.gradle'

android {
compileSdkVersion 28
Expand Down Expand Up @@ -30,24 +31,57 @@ android {
buildTypes {
debug {
minifyEnabled false
buildConfigField("String","VERSION_NAME","\"${project.subVersionName}a\"")
buildConfigField("String", "VERSION_NAME", "\"${project.subVersionName}a\"")
}

release {
minifyEnabled true
buildConfigField("String","VERSION_NAME","\"${project.subVersionName}a\"")
buildConfigField("String", "VERSION_NAME", "\"${project.subVersionName}a\"")
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

flavorDimensions "version"

productFlavors {
normal {
buildConfigField "String", "HTTP_INIT_SERVER", HTTP_INIT_SERVER
buildConfigField "String", "HTTPS_INIT_SERVER", HTTPS_INIT_SERVER
buildConfigField "String", "HTTP_TOLERANCE_SERVER", HTTP_TOLERANCE_SERVER
buildConfigField "String", "HTTPS_TOLERANCE_SERVER", HTTPS_TOLERANCE_SERVER
buildConfigField "String", "ATTA_ID", ATTA_ID
buildConfigField "String", "ATTA_TOKEN", ATTA_TOKEN
buildConfigField "String[]", "DOMAIN_SERVICE_DOMAINS", DOMAIN_SERVICE_DOMAINS
buildConfigField "String", "DOMAIN_SERVICE_ID", DOMAIN_SERVICE_ID
buildConfigField "String", "DOMSIN_SERVICE_KEY", DOMSIN_SERVICE_KEY
buildConfigField "String", "DOMAIN_SERVICE_TOKEN", DOMAIN_SERVICE_TOKEN
}

intl {
buildConfigField "String", "HTTP_INIT_SERVER", INTL_HTTP_INIT_SERVER
buildConfigField "String", "HTTPS_INIT_SERVER", "\"\""
buildConfigField "String", "HTTP_TOLERANCE_SERVER", INTL_HTTP_TOLERANCE_SERVER
buildConfigField "String", "HTTPS_TOLERANCE_SERVER", "\"\""
buildConfigField "String", "ATTA_ID", INTL_ATTA_ID
buildConfigField "String", "ATTA_TOKEN", INTL_ATTA_TOKEN
buildConfigField "String[]", "DOMAIN_SERVICE_DOMAINS", INTL_DOMAIN_SERVICE_DOMAINS
buildConfigField "String", "DOMAIN_SERVICE_ID", INTL_DOMAIN_SERVICE_ID
buildConfigField "String", "DOMSIN_SERVICE_KEY", INTL_DOMSIN_SERVICE_KEY
buildConfigField "String", "DOMAIN_SERVICE_TOKEN", INTL_DOMAIN_SERVICE_TOKEN
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

libraryVariants.all { variant ->
def newLibName = "HTTPDNS_Android_v${defaultConfig.versionName}_${variant.buildType.name}.aar"
def flavor = variant.getFlavorName()
def newLibName = flavor == "normal" ?
"HTTPDNS_Android_v${defaultConfig.versionName}_${variant.buildType.name}.aar" :
"HTTPDNS_Android_v${defaultConfig.versionName}_${variant.buildType.name}_${flavor}.aar"
variant.outputs.all {
outputFileName = newLibName
}
Expand All @@ -65,36 +99,36 @@ dependencies {
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'android.arch.persistence.room:runtime:2.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:2.1.1'
implementation 'androidx.room:room-runtime:2.2.0'
annotationProcessor 'androidx.room:room-compiler:2.2.0'

doclava 'com.google.doclava:doclava:1.0.6'
doclava 'com.google.doclava:doclava:1.0.6'
}

task generateJavadoc(type: Javadoc, dependsOn: project.configurations.doclava) {
failOnError = true
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadocs/")

options.doclet = "com.google.doclava.Doclava"
options.docletpath = configurations.doclava.files.asType(List)
options.encoding = "UTF-8"
options.memberLevel = JavadocMemberLevel.PUBLIC

title = null
source = [
'src/main/java/com/tencent/msdk/dns/DnsConfig.java',
'src/main/java/com/tencent/msdk/dns/DnsService.java',
'src/main/java/com/tencent/msdk/dns/base/executor/DnsExecutors.java',
'src/main/java/com/tencent/msdk/dns/ILookedUpListener.java',
'src/main/java/com/tencent/msdk/dns/base/log/ILogNode.java',
'src/main/java/com/tencent/msdk/dns/core/LookupResult.java',
'src/main/java/com/tencent/msdk/dns/core/IpSet.java',
'src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java',
'src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java',
'src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java',
'src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java',

'src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java',
]
failOnError = true
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadocs/")

options.doclet = "com.google.doclava.Doclava"
options.docletpath = configurations.doclava.files as List
options.encoding = "UTF-8"
options.memberLevel = JavadocMemberLevel.PUBLIC

title = null
source = [
'src/main/java/com/tencent/msdk/dns/DnsConfig.java',
'src/main/java/com/tencent/msdk/dns/DnsService.java',
'src/main/java/com/tencent/msdk/dns/base/executor/DnsExecutors.java',
'src/main/java/com/tencent/msdk/dns/ILookedUpListener.java',
'src/main/java/com/tencent/msdk/dns/base/log/ILogNode.java',
'src/main/java/com/tencent/msdk/dns/core/LookupResult.java',
'src/main/java/com/tencent/msdk/dns/core/IpSet.java',
'src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java',
'src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java',
'src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java',
'src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java',

'src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java',
]
}
1 change: 1 addition & 0 deletions proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
-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.DnsConfig {*;}
-keep public class com.tencent.msdk.dns.HttpDnsResponseObserver {*;}
-keep public class com.tencent.msdk.dns.DnsConfig$Builder {*;}
-keep public class com.tencent.msdk.dns.base.executor.DnsExecutors$ExecutorSupplier {*;}

Expand Down
3 changes: 2 additions & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.msdk.dns" >

<application android:networkSecurityConfig="@xml/network_security_config">
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
88 changes: 73 additions & 15 deletions src/main/java/com/tencent/msdk/dns/BackupResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.utils.DebounceTask;
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.DnsDescription;
import com.tencent.msdk.dns.core.DnsManager;
import com.tencent.msdk.dns.core.IDns;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.rest.deshttp.DesHttpDns;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;
import com.tencent.msdk.dns.report.ReportHelper;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -24,16 +32,16 @@ private BackupResolver() {
// 当前ip解析失败的次数
private AtomicInteger mErrorCount = new AtomicInteger(0);
// 解析失败的最大次数
private int maxErrorCount = 3;
private final int maxErrorCount = 3;
private DnsConfig mConfig;
// 解析ips
List<String> backupIps;
List<String> dnsIps;
// 当前对应的解析ip index
private int mIpIndex = 0;
// 记录主ip切换时间,每隔10min切回测试一次主IP(不主动探测主备IP是否恢复)
private long mBackupTime = 0;
// 尝试切回主ip的间隔时间,默认为10分钟
private long mInterval = 10 * 60 * 1000;
private final long mInterval = 10 * 60 * 1000;

public static BackupResolver getInstance() {//静态get方法
if (mBackupResolver == null) {
Expand All @@ -50,20 +58,30 @@ public void init(DnsConfig dnsConfig) {
mConfig = dnsConfig;
mErrorCount = new AtomicInteger(0);
// http和https是两个IP
if (Const.HTTPS_CHANNEL.equals(dnsConfig.channel)) {
backupIps = new ArrayList<String>(Arrays.asList(mConfig.dnsIp, "119.28.28.99"));
} else {
backupIps = new ArrayList<String>(Arrays.asList(mConfig.dnsIp, "119.28.28.98"));
dnsIps = getBackUpIps();
}

public void getServerIps() {
if (Const.HTTPS_CHANNEL.equals(mConfig.channel) || !DnsService.getDnsConfig().enableDomainServer) {
return;
}
getServerIpsTask.run();
}

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

public void incrementErrorCount(){
public void incrementErrorCount() {
Integer count = mErrorCount.incrementAndGet();
DnsLog.d(Thread.currentThread().getName() + " increment mErrorCount: " + count);
}

public void setErrorCount(Integer count){
public void setErrorCount(Integer count) {
mErrorCount.set(count);
DnsLog.d(Thread.currentThread().getName() + " set mErrorCount: " + count);
}
Expand Down Expand Up @@ -104,19 +122,59 @@ public String getDnsIp() {
mBackupTime = SystemClock.elapsedRealtime();
}
// 当前的失败次数达到了切换阈值时进行ipIndex的首尾循环,这里重新切回主IP
if (mIpIndex >= backupIps.size() - 1) {
if (mIpIndex >= dnsIps.size() - 1) {
mIpIndex = 0;
mBackupTime = 0;
} else {
mIpIndex++;
}
// ip切换后清空ip错误次数
mErrorCount.set(0);
DnsLog.d("IP Changed:" + backupIps.get(mIpIndex));
DnsLog.d("IP Changed:" + dnsIps.get(mIpIndex));
}

String backip = backupIps.get(mIpIndex);

String backip = dnsIps.get(mIpIndex);
return TextUtils.isEmpty(backip) ? mConfig.dnsIp : backip;
}

/**
* 服务域名解析,获取服务IP
* https加密方式不支持域名接入
* 国内站在初始化和网络变更时发起解析服务刷新服务IP,刷新IP不命中缓存
* 国际站服务IP按州部署,无须在网络变更时刷新
*/
DebounceTask getServerIpsTask = DebounceTask.build(new Runnable() {
@Override
public void run() {
try {
IDns dns = new DesHttpDns(DnsDescription.Family.INET);
String domain = BuildConfig.DOMAIN_SERVICE_DOMAINS[0];
LookupExtra lookupExtra = new LookupExtra(BuildConfig.DOMAIN_SERVICE_ID, BuildConfig.DOMSIN_SERVICE_KEY, BuildConfig.DOMAIN_SERVICE_TOKEN);
LookupParameters lookupParameters = new LookupParameters.Builder<LookupExtra>()
.dnsIp(BuildConfig.HTTP_INIT_SERVER)
.channel("DesHttp")
.hostname(domain)
.lookupExtra(lookupExtra)
.context(DnsService.getContext())
.timeoutMills(2000)
.enableAsyncLookup(true)
.fallback2Local(false)
.build();
LookupResult result = DnsManager.lookupWrapper(lookupParameters);
ReportHelper.attaReportDomainServerLookupEvent(result);
if (result.stat.lookupSuccess()) {
List<String> mergeList = new ArrayList<>();
List<String> serverIps = Arrays.asList(result.ipSet.v4Ips);
List<String> backUpIps = getBackUpIps();
mergeList.addAll(serverIps);
mergeList.addAll(backUpIps);
dnsIps = mergeList;
DnsLog.d("dns servers Ips: " + dnsIps);
mIpIndex = 0;
mErrorCount.set(0);
}
} catch (Exception e) {
DnsLog.w(e, "getServerIpsTask failed");
}
}
}, 15L);
}
Loading

0 comments on commit ce3ce30

Please sign in to comment.