From 8bcc5ad2690a947f0525812264fd88732f69272d Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Thu, 21 Apr 2022 17:02:32 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns/CacheManager/MSDKDnsManager.h | 1 + MSDKDns/CacheManager/MSDKDnsManager.m | 17 ++++++++++ MSDKDns/CacheManager/MSDKDnsParamsManager.h | 2 ++ MSDKDns/CacheManager/MSDKDnsParamsManager.m | 12 +++++++ MSDKDns/MSDKDns.h | 1 + MSDKDns/MSDKDns.m | 3 ++ MSDKDns/MSDKDnsService.m | 35 +++++++++++++++++++-- 7 files changed, 69 insertions(+), 2 deletions(-) diff --git a/MSDKDns/CacheManager/MSDKDnsManager.h b/MSDKDns/CacheManager/MSDKDnsManager.h index e0c7d0d..23481ac 100644 --- a/MSDKDns/CacheManager/MSDKDnsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsManager.h @@ -26,6 +26,7 @@ - (void)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose returnIps:(void (^)(NSDictionary * ipsDict))handler; - (NSDictionary *)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose; +- (void)refreshCacheDelay:(NSArray *)domains callback:(void (^)())handle; - (void)preResolveDomains; - (void)dnsHasDone:(MSDKDnsService *)service; - (void)cacheDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain; diff --git a/MSDKDns/CacheManager/MSDKDnsManager.m b/MSDKDns/CacheManager/MSDKDnsManager.m index c5da2c8..937ddc5 100644 --- a/MSDKDns/CacheManager/MSDKDnsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsManager.m @@ -197,6 +197,23 @@ - (void)getHostsByNames:(NSArray *)domains } }]; }); + +} + +#pragma mark 发送解析请求刷新缓存 + +- (void)refreshCacheDelay:(NSArray *)domains callback:(void (^)())handle { + // 获取当前ipv4/ipv6/双栈网络环境 + msdkdns::MSDKDNS_TLocalIPStack netStack = [self detectAddressType]; + __block float timeOut = 2.0; + timeOut = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMTimeOut]; + //进行httpdns请求 + int dnsId = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsId]; + NSString * dnsKey = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsKey]; + HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType]; + + MSDKDnsService * dnsService = [[MSDKDnsService alloc] init]; + [dnsService getHostsByNames:domains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:handle]; } - (void)preResolveDomains { diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.h b/MSDKDns/CacheManager/MSDKDnsParamsManager.h index e0f9a19..cb57bcc 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.h @@ -27,6 +27,7 @@ - (void)msdkDnsSetEnableReport: (BOOL)enableReport; - (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains; - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType; +- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains; - (NSString *) msdkDnsGetMDnsIp; - (NSString *) msdkDnsGetMOpenId; @@ -44,4 +45,5 @@ - (BOOL)msdkDnsGetEnableReport; - (NSArray *)msdkDnsGetPreResolvedDomains; - (HttpDnsAddressType)msdkDnsGetAddressType; +- (NSArray *)msdkDnsGetKeepAliveDomains; @end diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.m b/MSDKDns/CacheManager/MSDKDnsParamsManager.m index c35abaf..e954c8c 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.m @@ -32,6 +32,7 @@ @interface MSDKDnsParamsManager() @property (assign, nonatomic, readwrite) BOOL enableReport; @property (strong, nonatomic, readwrite) NSArray* preResolvedDomains; @property (assign, nonatomic, readwrite) HttpDnsAddressType msdkAddressType; +@property (strong, nonatomic, readwrite) NSArray* keepAliveDomains; @end @implementation MSDKDnsParamsManager @@ -156,6 +157,13 @@ - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType { }); } +- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + self.keepAliveDomains = domains; + }); +} + + #pragma mark - getter - (BOOL)msdkDnsGetHttpOnly { @@ -229,4 +237,8 @@ - (HttpDnsAddressType)msdkDnsGetAddressType { return _msdkAddressType; } +- (NSArray *)msdkDnsGetKeepAliveDomains { + return _keepAliveDomains; +} + @end diff --git a/MSDKDns/MSDKDns.h b/MSDKDns/MSDKDns.h index bbe0a5c..d8885cb 100644 --- a/MSDKDns/MSDKDns.h +++ b/MSDKDns/MSDKDns.h @@ -37,6 +37,7 @@ typedef struct DnsConfigStruct { NSUInteger retryTimesBeforeSwitchServer; // 可选,切换ip之前重试次数, 默认3次 NSUInteger minutesBeforeSwitchToMain; // 可选,设置切回主ip间隔时长,默认10分钟 BOOL enableReport; // 是否开启解析异常上报,默认NO,不上报 + NSArray* keepAliveDomains; } DnsConfig; @interface MSDKDns : NSObject diff --git a/MSDKDns/MSDKDns.m b/MSDKDns/MSDKDns.m index 9429655..440b067 100644 --- a/MSDKDns/MSDKDns.m +++ b/MSDKDns/MSDKDns.m @@ -52,6 +52,9 @@ - (BOOL) initConfig:(DnsConfig *)config { if (config->minutesBeforeSwitchToMain) { [[MSDKDnsParamsManager shareInstance] msdkDnsSetMinutesBeforeSwitchToMain:config->minutesBeforeSwitchToMain]; } + if (config->keepAliveDomains) { + [[MSDKDnsParamsManager shareInstance] msdkDnsSetKeepAliveDomains:config->keepAliveDomains]; + } [[MSDKDnsParamsManager shareInstance] msdkDnsSetEnableReport:config->enableReport]; [[MSDKDnsManager shareInstance] switchToMainServer]; self.msdkDnsReady = YES; diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index 31f9b3b..6be564e 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -134,12 +134,43 @@ - (void)startLocalDns:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey #pragma mark - MSDKDnsResolverDelegate - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)domainInfo { + static NSMutableDictionary *domainISOpenDelayDispatch = [[NSMutableDictionary alloc] init]; MSDKDNSLOG(@"%@ %@ domainInfo = %@", self.toCheckDomains, [resolver class], domainInfo); // 结果存缓存 dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ [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) { + NSLog(@"3333"); + NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; + for(id domain in domainInfo){ + // 判断此次请求的域名中有多少属于保活域名,是则开启延时解析请求,自动刷新缓存 + if([keepAliveDomains containsObject:domain]){ + NSString *afterTime = domainInfo[domain][kTTL]; +// NSLog(@"4444444延时更新请求等待,预计在%f秒后开始!请求域名为%@",afterTime.floatValue,domain); + @synchronized(self) { + if(domainISOpenDelayDispatch[domain] == nil){ + // 使用静态字典来记录该域名是否开启了一个延迟解析请求,如果已经开启则忽略,没有则立马开启一个 + [domainISOpenDelayDispatch setValue:@YES forKey:domain]; + MSDKDNSLOG(@"开启延时执行任务,预计在%f秒后开始请求域名为:%@", afterTime.floatValue, domain); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW,10* NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{ +// NSLog(@"延时更新请求开始!请求域名为%@",domain); + MSDKDNSLOG(@"延时更新请求开始!请求域名为%@",domain); + [[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] callback:^{ +// NSLog(@"请求结束,清除标志.请求域名为%@",domain); + MSDKDNSLOG(@"请求结束,清除标志.请求域名为%@",domain); + // 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求 + [domainISOpenDelayDispatch removeObjectForKey:domain]; + }]; + + }); + } + } + } + } + + } }); // 正常解析结果上报,上报解析耗时 if(resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) { @@ -243,7 +274,7 @@ - (void)callBack:(MSDKDnsResolver *)resolver Info:(NSDictionary *)info { } else if (resolver && (resolver == self.httpDnsResolver_4A)) { [cacheDict setObject:info forKey:kMSDKHttpDnsInfo_4A]; - + } if (cacheDict && domain) { @@ -309,7 +340,7 @@ - (void)cacheDomainInfo:(MSDKDnsResolver *)resolver { if (cacheValue) { [cacheDict setObject:cacheValue forKey:kMSDKHttpDnsCache_4A]; } - + } else if (resolver && (resolver == self.localDnsResolver) && self.localDnsResolver.domainInfo) { NSDictionary *cacheValue = [self.localDnsResolver.domainInfo objectForKey:domain]; From a5e9d4182c50a6cd3ce95d5d2d8066f56ef38ba3 Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Tue, 26 Apr 2022 16:24:48 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=92=8C=E5=9B=9E=E5=88=B0=E5=89=8D=E5=8F=B0?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns/CacheManager/MSDKDnsNetworkManager.m | 47 +++++++++++------ MSDKDns/CacheManager/MSDKDnsParamsManager.h | 4 ++ MSDKDns/CacheManager/MSDKDnsParamsManager.m | 29 ++++++++++- MSDKDns/MSDKDns.h | 6 ++- MSDKDns/MSDKDns.m | 9 ++-- MSDKDns/MSDKDnsService.m | 53 +++++++++++--------- 6 files changed, 104 insertions(+), 44 deletions(-) diff --git a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m index 605cb66..47dac15 100644 --- a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m +++ b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m @@ -4,6 +4,7 @@ #import "MSDKDnsNetworkManager.h" #import "MSDKDnsLog.h" +#import "MSDKDnsInfoTool.h" #import "MSDKDnsManager.h" #import "MSDKDnsParamsManager.h" #import @@ -68,32 +69,50 @@ - (instancetype)init queue:nil usingBlock:^(NSNotification *note) { - MSDKDNSLOG(@"Network did changed,clear MSDKDns cache"); - //网络状态发生变化时清除缓存 - [[MSDKDnsManager shareInstance] clearAllCache]; - //重置ip指针 - [[MSDKDnsManager shareInstance] switchToMainServer]; - }]; + MSDKDNSLOG(@"Network did changed,clear MSDKDns cache"); + //网络状态发生变化时清除缓存 + [[MSDKDnsManager shareInstance] clearAllCache]; + //对保活域名发送解析请求 + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; + if (keepAliveDomains && keepAliveDomains.count > 0) { + [[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains callback:^{ + MSDKDNSLOG(@"Delay request is finished,domains:%@",keepAliveDomains); + }]; + } + }); + //重置ip指针 + [[MSDKDnsManager shareInstance] switchToMainServer]; + }]; [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification *note) { - MSDKDNSLOG(@"Application did enter background,clear MSDKDns cache"); - //进入后台时清除缓存,暂停网络监测 - [[MSDKDnsManager shareInstance] clearAllCache]; - [self.reachability stopNotifier]; - }]; + MSDKDNSLOG(@"Application did enter background,clear MSDKDns cache"); + //进入后台时清除缓存,暂停网络监测 + [[MSDKDnsManager shareInstance] clearAllCache]; + [self.reachability stopNotifier]; + }]; [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification *note) { - //进入前台时,开启网络监测 - [self.reachability startNotifier]; - }]; + //进入前台时,开启网络监测 + [self.reachability startNotifier]; + //对保活域名发送解析请求 + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; + if (keepAliveDomains && keepAliveDomains.count > 0) { + [[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains callback:^{ + MSDKDNSLOG(@"Delay request is finished,domains:%@",keepAliveDomains); + }]; + } + }); + }]; _reachability = [MSDKDnsReachability reachabilityForInternetConnection]; [_reachability startNotifier]; diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.h b/MSDKDns/CacheManager/MSDKDnsParamsManager.h index cb57bcc..a8c7f11 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.h @@ -28,6 +28,9 @@ - (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains; - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType; - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains; +// 添加domain进入延迟记录字典里面 +- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain; +- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain; - (NSString *) msdkDnsGetMDnsIp; - (NSString *) msdkDnsGetMOpenId; @@ -46,4 +49,5 @@ - (NSArray *)msdkDnsGetPreResolvedDomains; - (HttpDnsAddressType)msdkDnsGetAddressType; - (NSArray *)msdkDnsGetKeepAliveDomains; +- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch; @end diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.m b/MSDKDns/CacheManager/MSDKDnsParamsManager.m index e954c8c..511c4d6 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.m @@ -33,6 +33,8 @@ @interface MSDKDnsParamsManager() @property (strong, nonatomic, readwrite) NSArray* preResolvedDomains; @property (assign, nonatomic, readwrite) HttpDnsAddressType msdkAddressType; @property (strong, nonatomic, readwrite) NSArray* keepAliveDomains; +// 延迟记录字典,记录哪些域名已经开启了延迟解析请求 +@property (strong, nonatomic, readwrite) NSMutableDictionary* domainISOpenDelayDispatch; @end @implementation MSDKDnsParamsManager @@ -159,10 +161,31 @@ - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType { - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains { dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ - self.keepAliveDomains = domains; + self.keepAliveDomains = [domains copy]; }); } +- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + if (domain && domain.length > 0) { + MSDKDNSLOG(@"domainISOpenDelayDispatch add domain:%@", domain); + if (!self.domainISOpenDelayDispatch) { + self.domainISOpenDelayDispatch = [[NSMutableDictionary alloc] init]; + } + [self.domainISOpenDelayDispatch setObject:@YES forKey:domain]; + } + }); +} + +- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain { + if (domain && domain.length > 0) { + MSDKDNSLOG(@"domainISOpenDelayDispatch remove domain:%@",domain); + if (self.domainISOpenDelayDispatch) { + [self.domainISOpenDelayDispatch removeObjectForKey:domain]; + } + } +} + #pragma mark - getter @@ -241,4 +264,8 @@ - (NSArray *)msdkDnsGetKeepAliveDomains { return _keepAliveDomains; } +- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch { + return _domainISOpenDelayDispatch; +} + @end diff --git a/MSDKDns/MSDKDns.h b/MSDKDns/MSDKDns.h index d8885cb..76f4446 100644 --- a/MSDKDns/MSDKDns.h +++ b/MSDKDns/MSDKDns.h @@ -37,7 +37,6 @@ typedef struct DnsConfigStruct { NSUInteger retryTimesBeforeSwitchServer; // 可选,切换ip之前重试次数, 默认3次 NSUInteger minutesBeforeSwitchToMain; // 可选,设置切回主ip间隔时长,默认10分钟 BOOL enableReport; // 是否开启解析异常上报,默认NO,不上报 - NSArray* keepAliveDomains; } DnsConfig; @interface MSDKDns : NSObject @@ -80,6 +79,11 @@ typedef struct DnsConfigStruct { */ - (void) WGSetPreResolvedDomains:(NSArray *)domains; +/** + * 设置保活的域名,设置的域名会定时更新缓存,数量不能大于8个 + */ +- (void) WGSetKeepAliveDomains:(NSArray *)domains; + #pragma mark - 域名解析接口,按需调用 /** 域名同步解析(通用接口) diff --git a/MSDKDns/MSDKDns.m b/MSDKDns/MSDKDns.m index 440b067..d7e2ec5 100644 --- a/MSDKDns/MSDKDns.m +++ b/MSDKDns/MSDKDns.m @@ -52,9 +52,6 @@ - (BOOL) initConfig:(DnsConfig *)config { if (config->minutesBeforeSwitchToMain) { [[MSDKDnsParamsManager shareInstance] msdkDnsSetMinutesBeforeSwitchToMain:config->minutesBeforeSwitchToMain]; } - if (config->keepAliveDomains) { - [[MSDKDnsParamsManager shareInstance] msdkDnsSetKeepAliveDomains:config->keepAliveDomains]; - } [[MSDKDnsParamsManager shareInstance] msdkDnsSetEnableReport:config->enableReport]; [[MSDKDnsManager shareInstance] switchToMainServer]; self.msdkDnsReady = YES; @@ -103,6 +100,12 @@ - (void) WGSetPreResolvedDomains:(NSArray *)domains { [[MSDKDnsManager shareInstance] preResolveDomains]; } +- (void) WGSetKeepAliveDomains:(NSArray *)domains { + if (domains) { + [[MSDKDnsParamsManager shareInstance] msdkDnsSetKeepAliveDomains:domains]; + } +} + - (void) WGSetHijackDomainArray:(NSArray *)hijackDomainArray { if (hijackDomainArray) { [[MSDKDnsParamsManager shareInstance] setHijackDomainArray:[hijackDomainArray copy]]; diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index 6be564e..0251dea 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -134,46 +134,49 @@ - (void)startLocalDns:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey #pragma mark - MSDKDnsResolverDelegate - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)domainInfo { - static NSMutableDictionary *domainISOpenDelayDispatch = [[NSMutableDictionary alloc] init]; MSDKDNSLOG(@"%@ %@ domainInfo = %@", self.toCheckDomains, [resolver class], domainInfo); // 结果存缓存 dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ [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) { - NSLog(@"3333"); + if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) { NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; - for(id domain in domainInfo){ + // 获取延迟记录字典 + NSMutableDictionary *domainISOpenDelayDispatch = [[MSDKDnsParamsManager shareInstance] msdkDnsGetDomainISOpenDelayDispatch]; + [domainInfo enumerateKeysAndObjectsUsingBlock:^(id _Nonnull domain, id _Nonnull obj, BOOL * _Nonnull stop) { + // NSLog(@"domain = %@", domain); + // NSLog(@"domainInfo = %@", domainInfo); + // 判断此次请求的域名中有多少属于保活域名,是则开启延时解析请求,自动刷新缓存 - if([keepAliveDomains containsObject:domain]){ + if (keepAliveDomains && domain && [keepAliveDomains containsObject:domain]) { NSString *afterTime = domainInfo[domain][kTTL]; -// NSLog(@"4444444延时更新请求等待,预计在%f秒后开始!请求域名为%@",afterTime.floatValue,domain); - @synchronized(self) { - if(domainISOpenDelayDispatch[domain] == nil){ - // 使用静态字典来记录该域名是否开启了一个延迟解析请求,如果已经开启则忽略,没有则立马开启一个 - [domainISOpenDelayDispatch setValue:@YES forKey:domain]; - MSDKDNSLOG(@"开启延时执行任务,预计在%f秒后开始请求域名为:%@", afterTime.floatValue, domain); - dispatch_after(dispatch_time(DISPATCH_TIME_NOW,10* NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{ -// NSLog(@"延时更新请求开始!请求域名为%@",domain); - MSDKDNSLOG(@"延时更新请求开始!请求域名为%@",domain); - [[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] callback:^{ -// NSLog(@"请求结束,清除标志.请求域名为%@",domain); - MSDKDNSLOG(@"请求结束,清除标志.请求域名为%@",domain); - // 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求 - [domainISOpenDelayDispatch removeObjectForKey:domain]; - }]; + + // NSLog(@"4444444延时更新请求等待,预计在%f秒后开始!请求域名为%@",afterTime.floatValue,domain); + if (!domainISOpenDelayDispatch[domain]) { + // 使用静态字典来记录该域名是否开启了一个延迟解析请求,如果已经开启则忽略,没有则立马开启一个 + [[MSDKDnsParamsManager shareInstance] msdkDnsAddDomainOpenDelayDispatch:domain]; + MSDKDNSLOG(@"Start the delayed execution task, it is expected to start requesting the domain name %@ after %f seconds", domain, afterTime.floatValue); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW,afterTime.floatValue* NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{ + // NSLog(@"延时更新请求开始!请求域名为%@",domain); + MSDKDNSLOG(@"The delayed update request starts! domain:%@",domain); + [[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] callback:^{ + // NSLog(@"请求结束,清除标志.请求域名为%@",domain); + MSDKDNSLOG(@"The request is over, clear the flag. domain:%@",domain); + // 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求 + [[MSDKDnsParamsManager shareInstance] msdkDnsClearDomainOpenDelayDispatch:domain]; - }); - } + }]; + + }); } + } - } - + }]; } }); // 正常解析结果上报,上报解析耗时 - if(resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) { + if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) { if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport] && [[AttaReport sharedInstance] shoulReportDnsSpend]) { NSDictionary *domainDic = [domainInfo objectForKey:[self.toCheckDomains firstObject]]; NSString* routeip = [[MSDKDnsParamsManager shareInstance] msdkDnsGetRouteIp]; From edde89f151176770a3c4fa1691c39ab52ddd3059 Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Thu, 28 Apr 2022 20:13:54 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat:=E6=8A=BD=E7=A6=BB=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=A3=E8=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns/CacheManager/MSDKDnsManager.h | 9 +++- MSDKDns/CacheManager/MSDKDnsManager.m | 50 +++++++++++++++++++- MSDKDns/CacheManager/MSDKDnsNetworkManager.m | 28 +++++------ MSDKDns/CacheManager/MSDKDnsParamsManager.h | 4 -- MSDKDns/CacheManager/MSDKDnsParamsManager.m | 27 +---------- MSDKDns/MSDKDnsService.m | 15 ++---- 6 files changed, 74 insertions(+), 59 deletions(-) diff --git a/MSDKDns/CacheManager/MSDKDnsManager.h b/MSDKDns/CacheManager/MSDKDnsManager.h index 23481ac..1846906 100644 --- a/MSDKDns/CacheManager/MSDKDnsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsManager.h @@ -26,7 +26,7 @@ - (void)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose returnIps:(void (^)(NSDictionary * ipsDict))handler; - (NSDictionary *)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose; -- (void)refreshCacheDelay:(NSArray *)domains callback:(void (^)())handle; +- (void)refreshCacheDelay:(NSArray *)domains clearDispatchTag:(BOOL)needClear; - (void)preResolveDomains; - (void)dnsHasDone:(MSDKDnsService *)service; - (void)cacheDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain; @@ -38,4 +38,11 @@ - (NSString *)currentDnsServer; - (void)switchDnsServer; - (void)switchToMainServer; + +// 添加domain进入延迟记录字典里面 +- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain; +- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain; +// 批量删除 +- (void)msdkDnsClearDomainsOpenDelayDispatch:(NSArray *)domains; +- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch; @end diff --git a/MSDKDns/CacheManager/MSDKDnsManager.m b/MSDKDns/CacheManager/MSDKDnsManager.m index 937ddc5..d0fbd79 100644 --- a/MSDKDns/CacheManager/MSDKDnsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsManager.m @@ -23,6 +23,8 @@ @interface MSDKDnsManager () @property (nonatomic, assign, readwrite) int serverIndex; @property (nonatomic, strong, readwrite) NSDate *firstFailTime; // 记录首次失败的时间 @property (nonatomic, assign, readwrite) BOOL waitToSwitch; // 防止连续多次切换 +// 延迟记录字典,记录哪些域名已经开启了延迟解析请求 +@property (strong, nonatomic, readwrite) NSMutableDictionary* domainISOpenDelayDispatch; @end @@ -90,6 +92,7 @@ - (NSDictionary *)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose { } // 全部有缓存时,直接返回 if([toCheckDomains count] == 0) { + // NSLog(@"有缓存"); NSDictionary * result = verbose ? [self fullResultDictionary:domains fromCache:cacheDomainDict] : [self resultDictionary:domains fromCache:cacheDomainDict]; @@ -107,6 +110,7 @@ - (NSDictionary *)getHostsByNames:(NSArray *)domains verbose:(BOOL)verbose { MSDKDnsService * dnsService = [[MSDKDnsService alloc] init]; [self.serviceArray addObject:dnsService]; __weak __typeof__(self) weakSelf = self; + // NSLog(@"%@, MSDKDns Result is toCheckDomains",toCheckDomains); [dnsService getHostsByNames:toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^() { __strong __typeof(self) strongSelf = weakSelf; if (strongSelf) { @@ -202,7 +206,7 @@ - (void)getHostsByNames:(NSArray *)domains #pragma mark 发送解析请求刷新缓存 -- (void)refreshCacheDelay:(NSArray *)domains callback:(void (^)())handle { +- (void)refreshCacheDelay:(NSArray *)domains clearDispatchTag:(BOOL)needClear { // 获取当前ipv4/ipv6/双栈网络环境 msdkdns::MSDKDNS_TLocalIPStack netStack = [self detectAddressType]; __block float timeOut = 2.0; @@ -213,7 +217,13 @@ - (void)refreshCacheDelay:(NSArray *)domains callback:(void (^)())handle { HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType]; MSDKDnsService * dnsService = [[MSDKDnsService alloc] init]; - [dnsService getHostsByNames:domains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:handle]; + [dnsService getHostsByNames:domains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^{ + if(needClear){ + // 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求 + // NSLog(@"延时更新请求结束!请求域名为%@",domains); + [self msdkDnsClearDomainsOpenDelayDispatch:domains]; + } + }]; } - (void)preResolveDomains { @@ -733,4 +743,40 @@ - (void)switchToMainServer { }); } +# pragma mark - operate delay tag + +- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + if (domain && domain.length > 0) { + MSDKDNSLOG(@"domainISOpenDelayDispatch add domain:%@", domain); + if (!self.domainISOpenDelayDispatch) { + self.domainISOpenDelayDispatch = [[NSMutableDictionary alloc] init]; + } + [self.domainISOpenDelayDispatch setObject:@YES forKey:domain]; + } + }); +} + +- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain { + if (domain && domain.length > 0) { + // NSLog(@"请求结束,清除标志.请求域名为%@",domain); + MSDKDNSLOG(@"The cache update request end! request domain:%@",domain); + MSDKDNSLOG(@"domainISOpenDelayDispatch remove domain:%@", domain); + if (self.domainISOpenDelayDispatch) { + [self.domainISOpenDelayDispatch removeObjectForKey:domain]; + } + } +} + +- (void)msdkDnsClearDomainsOpenDelayDispatch:(NSArray *)domains { + for(int i = 0; i < [domains count]; i++) { + NSString* domain = [domains objectAtIndex:i]; + [self msdkDnsClearDomainOpenDelayDispatch:domain]; + } +} + +- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch { + return _domainISOpenDelayDispatch; +} + @end diff --git a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m index 47dac15..068432e 100644 --- a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m +++ b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m @@ -73,14 +73,7 @@ - (instancetype)init //网络状态发生变化时清除缓存 [[MSDKDnsManager shareInstance] clearAllCache]; //对保活域名发送解析请求 - dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ - NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; - if (keepAliveDomains && keepAliveDomains.count > 0) { - [[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains callback:^{ - MSDKDNSLOG(@"Delay request is finished,domains:%@",keepAliveDomains); - }]; - } - }); + [self getHostsByKeepAliveDomains]; //重置ip指针 [[MSDKDnsManager shareInstance] switchToMainServer]; }]; @@ -104,14 +97,7 @@ - (instancetype)init //进入前台时,开启网络监测 [self.reachability startNotifier]; //对保活域名发送解析请求 - dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ - NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; - if (keepAliveDomains && keepAliveDomains.count > 0) { - [[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains callback:^{ - MSDKDNSLOG(@"Delay request is finished,domains:%@",keepAliveDomains); - }]; - } - }); + [self getHostsByKeepAliveDomains]; }]; _reachability = [MSDKDnsReachability reachabilityForInternetConnection]; @@ -294,4 +280,14 @@ -(NSString*) localWiFiIPAddress { return waddr; } +- (void)getHostsByKeepAliveDomains{ + //对保活域名发送解析请求 + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; + if (keepAliveDomains && keepAliveDomains.count > 0) { + [[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains clearDispatchTag:NO]; + } + }); +} + @end diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.h b/MSDKDns/CacheManager/MSDKDnsParamsManager.h index a8c7f11..cb57bcc 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.h @@ -28,9 +28,6 @@ - (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains; - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType; - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains; -// 添加domain进入延迟记录字典里面 -- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain; -- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain; - (NSString *) msdkDnsGetMDnsIp; - (NSString *) msdkDnsGetMOpenId; @@ -49,5 +46,4 @@ - (NSArray *)msdkDnsGetPreResolvedDomains; - (HttpDnsAddressType)msdkDnsGetAddressType; - (NSArray *)msdkDnsGetKeepAliveDomains; -- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch; @end diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.m b/MSDKDns/CacheManager/MSDKDnsParamsManager.m index 511c4d6..6908502 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.m @@ -33,8 +33,7 @@ @interface MSDKDnsParamsManager() @property (strong, nonatomic, readwrite) NSArray* preResolvedDomains; @property (assign, nonatomic, readwrite) HttpDnsAddressType msdkAddressType; @property (strong, nonatomic, readwrite) NSArray* keepAliveDomains; -// 延迟记录字典,记录哪些域名已经开启了延迟解析请求 -@property (strong, nonatomic, readwrite) NSMutableDictionary* domainISOpenDelayDispatch; + @end @implementation MSDKDnsParamsManager @@ -165,26 +164,6 @@ - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains { }); } -- (void)msdkDnsAddDomainOpenDelayDispatch: (NSString *)domain { - dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ - if (domain && domain.length > 0) { - MSDKDNSLOG(@"domainISOpenDelayDispatch add domain:%@", domain); - if (!self.domainISOpenDelayDispatch) { - self.domainISOpenDelayDispatch = [[NSMutableDictionary alloc] init]; - } - [self.domainISOpenDelayDispatch setObject:@YES forKey:domain]; - } - }); -} - -- (void)msdkDnsClearDomainOpenDelayDispatch:(NSString *)domain { - if (domain && domain.length > 0) { - MSDKDNSLOG(@"domainISOpenDelayDispatch remove domain:%@",domain); - if (self.domainISOpenDelayDispatch) { - [self.domainISOpenDelayDispatch removeObjectForKey:domain]; - } - } -} #pragma mark - getter @@ -264,8 +243,6 @@ - (NSArray *)msdkDnsGetKeepAliveDomains { return _keepAliveDomains; } -- (NSMutableDictionary *)msdkDnsGetDomainISOpenDelayDispatch { - return _domainISOpenDelayDispatch; -} + @end diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index 0251dea..8757d92 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -143,7 +143,7 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A) { NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains]; // 获取延迟记录字典 - NSMutableDictionary *domainISOpenDelayDispatch = [[MSDKDnsParamsManager shareInstance] msdkDnsGetDomainISOpenDelayDispatch]; + NSMutableDictionary *domainISOpenDelayDispatch = [[MSDKDnsManager shareInstance] msdkDnsGetDomainISOpenDelayDispatch]; [domainInfo enumerateKeysAndObjectsUsingBlock:^(id _Nonnull domain, id _Nonnull obj, BOOL * _Nonnull stop) { // NSLog(@"domain = %@", domain); // NSLog(@"domainInfo = %@", domainInfo); @@ -155,19 +155,12 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do // NSLog(@"4444444延时更新请求等待,预计在%f秒后开始!请求域名为%@",afterTime.floatValue,domain); if (!domainISOpenDelayDispatch[domain]) { // 使用静态字典来记录该域名是否开启了一个延迟解析请求,如果已经开启则忽略,没有则立马开启一个 - [[MSDKDnsParamsManager shareInstance] msdkDnsAddDomainOpenDelayDispatch:domain]; + [[MSDKDnsManager shareInstance] msdkDnsAddDomainOpenDelayDispatch:domain]; MSDKDNSLOG(@"Start the delayed execution task, it is expected to start requesting the domain name %@ after %f seconds", domain, afterTime.floatValue); dispatch_after(dispatch_time(DISPATCH_TIME_NOW,afterTime.floatValue* NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{ // NSLog(@"延时更新请求开始!请求域名为%@",domain); - MSDKDNSLOG(@"The delayed update request starts! domain:%@",domain); - [[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] callback:^{ - // NSLog(@"请求结束,清除标志.请求域名为%@",domain); - MSDKDNSLOG(@"The request is over, clear the flag. domain:%@",domain); - // 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求 - [[MSDKDnsParamsManager shareInstance] msdkDnsClearDomainOpenDelayDispatch:domain]; - - }]; - + MSDKDNSLOG(@"The cache update request start! request domain:%@",domain); + [[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] clearDispatchTag:YES]; }); } From 374840541bd098892a7370b1ca1294b1f70b1e1d Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Fri, 20 May 2022 11:09:42 +0800 Subject: [PATCH 4/6] =?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 From fd5d35b31665b32cd54f095a3e8e60e69b184f73 Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Thu, 7 Jul 2022 10:32:01 +0800 Subject: [PATCH 5/6] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=88=B01.4.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns.xcodeproj/project.pbxproj | 8 ++++---- MSDKDns/MSDKDns.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/MSDKDns.xcodeproj/project.pbxproj b/MSDKDns.xcodeproj/project.pbxproj index 5b77e89..4aaa6b0 100644 --- a/MSDKDns.xcodeproj/project.pbxproj +++ b/MSDKDns.xcodeproj/project.pbxproj @@ -628,7 +628,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.4.0; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( @@ -667,7 +667,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.4.0; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( "-ObjC", @@ -704,7 +704,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.4.0; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( @@ -743,7 +743,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.4.0; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( "-ObjC", diff --git a/MSDKDns/MSDKDns.h b/MSDKDns/MSDKDns.h index 76f4446..06425fd 100644 --- a/MSDKDns/MSDKDns.h +++ b/MSDKDns/MSDKDns.h @@ -5,7 +5,7 @@ #ifndef __MSDKDns_H__ #define __MSDKDns_H__ -#define MSDKDns_Version @"1.3.5" +#define MSDKDns_Version @"1.4.0" #import From bd548e0bf4be27f34542ea689bd5f14576e29ae6 Mon Sep 17 00:00:00 2001 From: erichuyuehu Date: Thu, 7 Jul 2022 17:15:48 +0800 Subject: [PATCH 6/6] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0ipv4=E5=92=8Cipv6?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSDKDns/MSDKDnsService.m | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index 4a0fbbc..84c34a3 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -166,10 +166,33 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do // 判断此次请求的域名中有多少属于保活域名,是则开启延时解析请求,自动刷新缓存 if (keepAliveDomains && domain && [keepAliveDomains containsObject:domain]) { - NSString *afterTime = domainInfo[domain][kTTL]; + + NSMutableString * afterTime = [[NSMutableString alloc] init]; + + if(resolver == self.httpDnsResolver_BOTH){ + NSDictionary *domainResult = domainInfo[domain]; + if (domainResult) { + NSDictionary *ipv4Value = [domainResult objectForKey:@"ipv4"]; + NSDictionary *ipv6Value = [domainResult objectForKey:@"ipv6"]; + if (ipv6Value) { + NSString *ttl = [ipv6Value objectForKey:kTTL]; + afterTime = [[NSMutableString alloc]initWithString:ttl]; + } + if (ipv4Value) { + NSString *ttl = [ipv4Value objectForKey:kTTL]; + afterTime = [[NSMutableString alloc]initWithString:ttl]; + } + } + }else{ + NSDictionary *domainResult = domainInfo[domain]; + if (domainResult) { + NSString *ttl = [domainResult objectForKey:kTTL]; + afterTime = [[NSMutableString alloc]initWithString:ttl]; + } + } // NSLog(@"4444444延时更新请求等待,预计在%f秒后开始!请求域名为%@",afterTime.floatValue,domain); - if (!domainISOpenDelayDispatch[domain]) { + if (!domainISOpenDelayDispatch[domain] && afterTime) { // 使用静态字典来记录该域名是否开启了一个延迟解析请求,如果已经开启则忽略,没有则立马开启一个 [[MSDKDnsManager shareInstance] msdkDnsAddDomainOpenDelayDispatch:domain]; MSDKDNSLOG(@"Start the delayed execution task, it is expected to start requesting the domain name %@ after %f seconds", domain, afterTime.floatValue);