Skip to content

Commit

Permalink
feat:新增合并v4和v6请求&解决localDNS阻塞问题
Browse files Browse the repository at this point in the history
  • Loading branch information
erichuyuehu committed May 20, 2022
1 parent edde89f commit 3748405
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 55 deletions.
13 changes: 13 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,13 @@ - (NSMutableDictionary *)formatParams:(BOOL)isFromCache Domain:(NSString *)domai
NSString * httpDnsTTL_4A = @"";
NSString * httpDnsErrCode_A = @"";
NSString * httpDnsErrCode_4A = @"";
NSString * httpDnsErrCode_BOTH = @"";
NSString * httpDnsErrMsg_A = @"";
NSString * httpDnsErrMsg_4A = @"";
NSString * httpDnsErrMsg_BOTH = @"";
NSString * httpDnsRetry_A = @"";
NSString * httpDnsRetry_4A = @"";
NSString * httpDnsRetry_BOTH = @"";
NSString * cache_A = @"";
NSString * cache_4A = @"";
NSString * dns_A = @"0";
Expand Down Expand Up @@ -606,6 +609,13 @@ - (NSMutableDictionary *)formatParams:(BOOL)isFromCache Domain:(NSString *)domai
httpDnsErrMsg_4A = httpDnsInfo_A[kDnsErrMsg];
httpDnsRetry_4A = httpDnsInfo_A[kDnsRetry];
}

NSDictionary * httpDnsInfo_BOTH = cacheInfo[kMSDKHttpDnsInfo_BOTH];
if (httpDnsInfo_BOTH) {
httpDnsErrCode_BOTH = httpDnsInfo_BOTH[kDnsErrCode];
httpDnsErrMsg_BOTH = httpDnsInfo_BOTH[kDnsErrMsg];
httpDnsRetry_BOTH = httpDnsInfo_BOTH[kDnsRetry];
}
}
}

Expand Down Expand Up @@ -637,14 +647,17 @@ - (NSMutableDictionary *)formatParams:(BOOL)isFromCache Domain:(NSString *)domai
//ErrCode
[params setValue:httpDnsErrCode_A forKey:kMSDKDns_A_ErrCode];
[params setValue:httpDnsErrCode_4A forKey:kMSDKDns_4A_ErrCode];
[params setValue:httpDnsErrCode_BOTH forKey:kMSDKDns_BOTH_ErrCode];

//ErrMsg
[params setValue:httpDnsErrMsg_A forKey:kMSDKDns_A_ErrMsg];
[params setValue:httpDnsErrMsg_4A forKey:kMSDKDns_4A_ErrMsg];
[params setValue:httpDnsErrMsg_BOTH forKey:kMSDKDns_BOTH_ErrMsg];

//Retry
[params setValue:httpDnsRetry_A forKey:kMSDKDns_A_Retry];
[params setValue:httpDnsRetry_4A forKey:kMSDKDns_4A_Retry];
[params setValue:httpDnsRetry_BOTH forKey:kMSDKDns_BOTH_Retry];

//dns
[params setValue:dns_A forKey:kMSDKDns_DNS_A_IP];
Expand Down
5 changes: 3 additions & 2 deletions MSDKDns/MSDKDnsInfoTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#import <Foundation/Foundation.h>
#import "Resolver/HttpsDnsResolver.h"

@interface MSDKDnsInfoTool : NSObject

Expand All @@ -14,9 +15,9 @@

+ (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;
+ (NSString *) decryptUseDES:(NSString *)cipherString key:(NSString *)key;
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey Use4A:(BOOL)use4A;
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey IPType:(HttpDnsIPType)ipType;

+ (NSString *)encryptUseAES:(NSString *)plainText key:(NSString *)key;
+ (NSString *)decryptUseAES:(NSString *)cipherString key:(NSString *)key;
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey Use4A:(BOOL)use4A encryptType:(NSInteger)encryptType; //encryptType: 0 des,1 aes
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey IPType:(HttpDnsIPType)ipType encryptType:(NSInteger)encryptType; //encryptType: 0 des,1 aes
@end
10 changes: 6 additions & 4 deletions MSDKDns/MSDKDnsInfoTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ + (NSData *)bytesFromHexString:(NSString *)hexString length:(int)len
return newData;
}

+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey Use4A:(BOOL)use4A
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey IPType:(HttpDnsIPType)ipType
{
return [self httpsUrlWithDomain:domain DnsId:dnsId DnsKey:dnsKey Use4A:use4A encryptType:HttpDnsEncryptTypeDES];
return [self httpsUrlWithDomain:domain DnsId:dnsId DnsKey:dnsKey IPType:ipType encryptType:HttpDnsEncryptTypeDES];
}

+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey Use4A:(BOOL)use4A encryptType:(NSInteger)encryptType
+ (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSString *)dnsKey IPType:(HttpDnsIPType)ipType encryptType:(NSInteger)encryptType
{
if (!domain || domain.length == 0) {
MSDKDNSLOG(@"HttpDns Domain cannot be empty!");
Expand Down Expand Up @@ -435,8 +435,10 @@ + (NSURL *) httpsUrlWithDomain:(NSString *)domain DnsId:(int)dnsId DnsKey:(NSStr
httpServer = serviceIp;
}
NSString * urlStr = [NSString stringWithFormat:@"%@://%@/d?dn=%@&clientip=1&ttl=1&query=1&id=%d", protocol, httpServer, domainEncrypStr, dnsId];
if (use4A) {
if (ipType == HttpDnsTypeIPv6) {
urlStr = [urlStr stringByAppendingString:@"&type=aaaa"];
}else if (ipType == HttpDnsTypeDual) {
urlStr = [urlStr stringByAppendingString:@"&type=addrs"];
}
if (encryptType == HttpDnsEncryptTypeAES) {
urlStr = [urlStr stringByAppendingFormat:@"&alg=aes"];
Expand Down
4 changes: 4 additions & 0 deletions MSDKDns/MSDKDnsPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static NSString * const kMSDKHttpDnsCache_A = @"httpDnsCache_A";
static NSString * const kMSDKHttpDnsCache_4A = @"httpDnsCache_4A";
static NSString * const kMSDKHttpDnsInfo_A = @"httpDnsInfo_A";
static NSString * const kMSDKHttpDnsInfo_4A = @"httpDnsInfo_4A";
static NSString * const kMSDKHttpDnsInfo_BOTH = @"httpDnsInfo_BOTH";
static NSString * const kMSDKLocalDnsCache = @"localDnsCache";

//HttpDns解析结果数据上报相关
Expand Down Expand Up @@ -54,6 +55,9 @@ static NSString * const kMSDKDns_4A_TTL = @"hdns_4a_ttl"; // 域名
static NSString * const kMSDKDns_4A_ClientIP = @"hdns_4a_client_ip"; // 域名解析AAAA记录结果客户端IP
static NSString * const kMSDKDns_4A_Time = @"hdns_4a_time_ms"; // 域名解析AAAA记录耗时(单位ms)
static NSString * const kMSDKDns_4A_Retry = @"hdns_4a_retry"; // 域名解析AAAA记录重试次数
static NSString * const kMSDKDns_BOTH_Retry = @"hdns_both_retry"; // 双栈域名解析重试次数
static NSString * const kMSDKDns_BOTH_ErrCode = @"hdns_both_err_code"; // 双栈域名解析解析错误码
static NSString * const kMSDKDns_BOTH_ErrMsg = @"hdns_both_err_msg"; // 双栈域名解析解析错误信息
static NSString * const kMSDKDns_DNS_A_IP = @"dns_ips"; // 域名解析结果v4 IP,多个ip以“,”拼接
static NSString * const kMSDKDns_DNS_4A_IP = @"dns_4a_ips"; // 域名解析结果v6 IP,多个ip以“,”拼接

Expand Down
77 changes: 57 additions & 20 deletions MSDKDns/MSDKDnsService.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @interface MSDKDnsService () <MSDKDnsResolverDelegate>
@property (strong, nonatomic) NSArray * toCheckDomains;
@property (strong, nonatomic) HttpsDnsResolver * httpDnsResolver_A;
@property (strong, nonatomic) HttpsDnsResolver * httpDnsResolver_4A;
@property (strong, nonatomic) HttpsDnsResolver * httpDnsResolver_BOTH;
@property (strong, nonatomic) LocalDnsResolver * localDnsResolver;
@property (nonatomic, strong) void (^ completionHandler)();
@property (atomic, assign) BOOL isCallBack;
Expand Down Expand Up @@ -79,18 +80,24 @@ - (void)startCheck:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey enc
self.dnsKey = dnsKey;
self.encryptType = encryptType;

if (_netStack != msdkdns::MSDKDNS_ELocalIPStack_IPv4) {
if (_netStack == msdkdns::MSDKDNS_ELocalIPStack_IPv6) {
dispatch_async([MSDKDnsInfoTool msdkdns_resolver_queue], ^{
[self startHttpDns_4A:timeOut DnsId:dnsId DnsKey:dnsKey encryptType:encryptType];
});
}

if (_netStack != msdkdns::MSDKDNS_ELocalIPStack_IPv6) {
if (_netStack == msdkdns::MSDKDNS_ELocalIPStack_IPv4) {
dispatch_async([MSDKDnsInfoTool msdkdns_resolver_queue], ^{
[self startHttpDns:timeOut DnsId:dnsId DnsKey:dnsKey encryptType:encryptType];
});
}

if (_netStack == msdkdns::MSDKDNS_ELocalIPStack_Dual) {
dispatch_async([MSDKDnsInfoTool msdkdns_resolver_queue], ^{
[self startHttpDnsBoth:timeOut DnsId:dnsId DnsKey:dnsKey encryptType:encryptType];
});
}

BOOL httpOnly = [[MSDKDnsParamsManager shareInstance] msdkDnsGetHttpOnly];
if (!httpOnly) {
dispatch_async([MSDKDnsInfoTool msdkdns_resolver_queue], ^{
Expand All @@ -105,7 +112,16 @@ - (void)startCheck:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey enc
});
}

//进行httpdns请求
//进行httpdns ipv4和ipv6合并请求
- (void)startHttpDnsBoth:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey encryptType:(NSInteger)encryptType
{
MSDKDNSLOG(@"%@ StartHttpDns!", self.toCheckDomains);
self.httpDnsResolver_BOTH = [[HttpsDnsResolver alloc] init];
self.httpDnsResolver_BOTH.delegate = self;
[self.httpDnsResolver_BOTH startWithDomains:self.toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:msdkdns::MSDKDNS_ELocalIPStack_Dual encryptType:encryptType];
}

//进行httpdns ipv4请求
- (void)startHttpDns:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey encryptType:(NSInteger)encryptType
{
MSDKDNSLOG(@"%@ StartHttpDns!", self.toCheckDomains);
Expand All @@ -114,7 +130,7 @@ - (void)startHttpDns:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey e
[self.httpDnsResolver_A startWithDomains:self.toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:msdkdns::MSDKDNS_ELocalIPStack_IPv4 encryptType:encryptType];
}


//进行httpdns ipv6请求
- (void)startHttpDns_4A:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey encryptType:(NSInteger)encryptType
{
MSDKDNSLOG(@"%@ StartHttpDns!", self.toCheckDomains);
Expand All @@ -140,7 +156,7 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do
[self cacheDomainInfo:resolver];
NSDictionary * info = @{kDnsErrCode:MSDKDns_Success, kDnsErrMsg:@"", kDnsRetry:@"0"};
[self callBack:resolver Info:info];
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) {
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A || resolver == self.httpDnsResolver_BOTH) {
NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains];
// 获取延迟记录字典
NSMutableDictionary *domainISOpenDelayDispatch = [[MSDKDnsManager shareInstance] msdkDnsGetDomainISOpenDelayDispatch];
Expand Down Expand Up @@ -169,7 +185,7 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do
}
});
// 正常解析结果上报,上报解析耗时
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) {
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A || resolver == self.httpDnsResolver_BOTH) {
if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport] && [[AttaReport sharedInstance] shoulReportDnsSpend]) {
NSDictionary *domainDic = [domainInfo objectForKey:[self.toCheckDomains firstObject]];
NSString* routeip = [[MSDKDnsParamsManager shareInstance] msdkDnsGetRouteIp];
Expand Down Expand Up @@ -216,6 +232,10 @@ - (void) retryHttpDns:(MSDKDnsResolver *)resolver {
dispatch_async([MSDKDnsInfoTool msdkdns_retry_queue], ^{
[self startHttpDns_4A:self.timeOut DnsId:self.dnsId DnsKey:self.dnsKey encryptType:self.encryptType];
});
} else if (resolver == self.httpDnsResolver_BOTH) {
dispatch_async([MSDKDnsInfoTool msdkdns_retry_queue], ^{
[self startHttpDnsBoth:self.timeOut DnsId:self.dnsId DnsKey:self.dnsKey encryptType:self.encryptType];
});
}
} else {
MSDKDNSLOG(@"fail %lu times, switch server!", (unsigned long)[[MSDKDnsParamsManager shareInstance] msdkDnsGetRetryTimesBeforeSwitchServer]);
Expand Down Expand Up @@ -271,6 +291,10 @@ - (void)callBack:(MSDKDnsResolver *)resolver Info:(NSDictionary *)info {

[cacheDict setObject:info forKey:kMSDKHttpDnsInfo_4A];

} else if (resolver && (resolver == self.httpDnsResolver_BOTH)) {

[cacheDict setObject:info forKey:kMSDKHttpDnsInfo_BOTH];

}

if (cacheDict && domain) {
Expand All @@ -280,20 +304,21 @@ - (void)callBack:(MSDKDnsResolver *)resolver Info:(NSDictionary *)info {

MSDKDNSLOG(@"callBack! :%@", self.toCheckDomains);
BOOL httpOnly = [[MSDKDnsParamsManager shareInstance] msdkDnsGetHttpOnly];
//LocalHttp 和 HttpDns均完成,则返回结果
if (httpOnly || self.localDnsResolver.isFinished) {
if (self.httpDnsResolver_A && self.httpDnsResolver_4A) {
if (self.httpDnsResolver_A.isFinished && self.httpDnsResolver_4A.isFinished) {
[self callNotify];
}
} else if (self.httpDnsResolver_A && !self.httpDnsResolver_4A) {
if (self.httpDnsResolver_A.isFinished) {
[self callNotify];
}
} else if (!self.httpDnsResolver_A && self.httpDnsResolver_4A) {
if (self.httpDnsResolver_4A.isFinished) {
[self callNotify];
}
if (self.httpDnsResolver_A && self.httpDnsResolver_4A) {
if (self.httpDnsResolver_A.isFinished && self.httpDnsResolver_4A.isFinished) {
[self callNotify];
}
} else if (self.httpDnsResolver_A && !self.httpDnsResolver_4A) {
if (self.httpDnsResolver_A.isFinished) {
[self callNotify];
}
} else if (!self.httpDnsResolver_A && self.httpDnsResolver_4A) {
if (self.httpDnsResolver_4A.isFinished) {
[self callNotify];
}
} else if (self.httpDnsResolver_BOTH) {
if (self.httpDnsResolver_BOTH.isFinished) {
[self callNotify];
}
}
}
Expand Down Expand Up @@ -343,6 +368,18 @@ - (void)cacheDomainInfo:(MSDKDnsResolver *)resolver {
if (cacheValue) {
[cacheDict setObject:cacheValue forKey:kMSDKLocalDnsCache];
}
} else if (resolver && (resolver == self.httpDnsResolver_BOTH) && self.httpDnsResolver_BOTH.domainInfo) {
NSDictionary *cacheValue = [self.httpDnsResolver_BOTH.domainInfo objectForKey:domain];
if (cacheValue) {
NSDictionary *ipv4CacheValue = [cacheValue objectForKey:@"ipv4"];
NSDictionary *ipv6CacheValue = [cacheValue objectForKey:@"ipv6"];
if (ipv4CacheValue) {
[cacheDict setObject:ipv4CacheValue forKey:kMSDKHttpDnsCache_A];
}
if (ipv6CacheValue) {
[cacheDict setObject:ipv6CacheValue forKey:kMSDKHttpDnsCache_4A];
}
}
}

if (cacheDict && domain) {
Expand Down
Loading

0 comments on commit 3748405

Please sign in to comment.