From 374840541bd098892a7370b1ca1294b1f70b1e1d Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Fri, 20 May 2022 11:09:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=90=88=E5=B9=B6v4?= =?UTF-8?q?=E5=92=8Cv6=E8=AF=B7=E6=B1=82&=E8=A7=A3=E5=86=B3localDNS?= =?UTF-8?q?=E9=98=BB=E5=A1=9E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns/CacheManager/MSDKDnsManager.m | 13 ++++ MSDKDns/MSDKDnsInfoTool.h | 5 +- MSDKDns/MSDKDnsInfoTool.m | 10 +-- MSDKDns/MSDKDnsPrivate.h | 4 ++ MSDKDns/MSDKDnsService.m | 77 ++++++++++++++++------ MSDKDns/Resolver/HttpsDnsResolver.m | 95 +++++++++++++++++++-------- MSDKDns/Resolver/MSDKDnsResolver.h | 6 ++ 7 files changed, 155 insertions(+), 55 deletions(-) diff --git a/MSDKDns/CacheManager/MSDKDnsManager.m b/MSDKDns/CacheManager/MSDKDnsManager.m index d0fbd79..251a310 100644 --- a/MSDKDns/CacheManager/MSDKDnsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsManager.m @@ -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"; @@ -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]; + } } } @@ -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]; diff --git a/MSDKDns/MSDKDnsInfoTool.h b/MSDKDns/MSDKDnsInfoTool.h index d9be288..fef0b55 100644 --- a/MSDKDns/MSDKDnsInfoTool.h +++ b/MSDKDns/MSDKDnsInfoTool.h @@ -3,6 +3,7 @@ */ #import +#import "Resolver/HttpsDnsResolver.h" @interface MSDKDnsInfoTool : NSObject @@ -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 diff --git a/MSDKDns/MSDKDnsInfoTool.m b/MSDKDns/MSDKDnsInfoTool.m index 9fa2589..608e276 100644 --- a/MSDKDns/MSDKDnsInfoTool.m +++ b/MSDKDns/MSDKDnsInfoTool.m @@ -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!"); @@ -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"]; diff --git a/MSDKDns/MSDKDnsPrivate.h b/MSDKDns/MSDKDnsPrivate.h index 6de0c53..29329f2 100644 --- a/MSDKDns/MSDKDnsPrivate.h +++ b/MSDKDns/MSDKDnsPrivate.h @@ -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解析结果数据上报相关 @@ -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以“,”拼接 diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index 8757d92..4a0fbbc 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -17,6 +17,7 @@ @interface MSDKDnsService () @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; @@ -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], ^{ @@ -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); @@ -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); @@ -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]; @@ -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]; @@ -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]); @@ -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) { @@ -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]; } } } @@ -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) { diff --git a/MSDKDns/Resolver/HttpsDnsResolver.m b/MSDKDns/Resolver/HttpsDnsResolver.m index 19699c0..2996fea 100644 --- a/MSDKDns/Resolver/HttpsDnsResolver.m +++ b/MSDKDns/Resolver/HttpsDnsResolver.m @@ -14,7 +14,7 @@ @interface HttpsDnsResolver() 1 ? [ipsStr substringFromIndex:ipsStr.length - 1] : @""; - if ([tempStr isEqualToString:@";"]) { - ipsStr = [ipsStr substringToIndex:ipsStr.length - 1]; - } - NSArray *ipsArray = [ipsStr componentsSeparatedByString:@";"]; - //校验ip合法性 - BOOL isIPLegal = [self isIPLegal:ipsArray Use4A:_use4A]; - - if (isIPLegal) { - double timeInterval = [[NSDate date] timeIntervalSince1970]; - NSString * ttlExpried = [NSString stringWithFormat:@"%0.0f", (timeInterval + ttl.floatValue * 0.75)]; - NSString * timeConsuming = [NSString stringWithFormat:@"%d", [self dnsTimeConsuming]]; - NSString * channel = @"http"; - return @{kIP:ipsArray, kClientIP:clientIP, kTTL:ttl, kTTLExpired:ttlExpried, kDnsTimeConsuming:timeConsuming, kChannel:channel}; - } + } + return nil; +} + +-(NSDictionary *)parseIPString:(NSString *)iPstring ClientIP:(NSString *)clientIP Use4A:(BOOL)use4A { + NSString *ipsStr = nil; + NSString *ttl = nil; + NSArray * tmpArr = [iPstring componentsSeparatedByString:@","]; + if (tmpArr && [tmpArr count] == 2) { + ipsStr = tmpArr[0]; + ttl = tmpArr[1]; + } + NSString * tempStr = ipsStr.length > 1 ? [ipsStr substringFromIndex:ipsStr.length - 1] : @""; + if ([tempStr isEqualToString:@";"]) { + ipsStr = [ipsStr substringToIndex:ipsStr.length - 1]; + } + NSArray *ipsArray = [ipsStr componentsSeparatedByString:@";"]; + //校验ip合法性 + BOOL isIPLegal = [self isIPLegal:ipsArray Use4A:use4A]; + + if (isIPLegal) { + double timeInterval = [[NSDate date] timeIntervalSince1970]; + NSString * ttlExpried = [NSString stringWithFormat:@"%0.0f", (timeInterval + ttl.floatValue * 0.75)]; + NSString * timeConsuming = [NSString stringWithFormat:@"%d", [self dnsTimeConsuming]]; + NSString * channel = @"http"; + return @{kIP:ipsArray, kClientIP:clientIP, kTTL:ttl, kTTLExpired:ttlExpried, kDnsTimeConsuming:timeConsuming, kChannel:channel}; } return nil; } diff --git a/MSDKDns/Resolver/MSDKDnsResolver.h b/MSDKDns/Resolver/MSDKDnsResolver.h index 1723c2f..36956de 100644 --- a/MSDKDns/Resolver/MSDKDnsResolver.h +++ b/MSDKDns/Resolver/MSDKDnsResolver.h @@ -6,6 +6,12 @@ #import "MSDKDnsPrivate.h" #import "msdkdns_local_ip_stack.h" +typedef enum { + HttpDnsTypeIPv4 = 1, // 只支持ipv4 + HttpDnsTypeIPv6 = 2, // 只支持ipv6 + HttpDnsTypeDual = 3, // 支持双协议栈 +} HttpDnsIPType; + @class MSDKDnsResolver; @protocol MSDKDnsResolverDelegate