Skip to content

Commit

Permalink
Merge pull request #18 from HuYuee/master
Browse files Browse the repository at this point in the history
feat:增加缓存刷新逻辑
  • Loading branch information
ocleo1 authored Jul 11, 2022
2 parents d3a2164 + bd548e0 commit ea5046a
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 75 deletions.
8 changes: 4 additions & 4 deletions MSDKDns.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 clearDispatchTag:(BOOL)needClear;
- (void)preResolveDomains;
- (void)dnsHasDone:(MSDKDnsService *)service;
- (void)cacheDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain;
Expand All @@ -37,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
76 changes: 76 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand Down Expand Up @@ -197,6 +201,29 @@ - (void)getHostsByNames:(NSArray *)domains
}
}];
});

}

#pragma mark 发送解析请求刷新缓存

- (void)refreshCacheDelay:(NSArray *)domains clearDispatchTag:(BOOL)needClear {
// 获取当前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:^{
if(needClear){
// 当请求结束了需要将该域名开启的标志清除,方便下次继续开启延迟解析请求
// NSLog(@"延时更新请求结束!请求域名为%@",domains);
[self msdkDnsClearDomainsOpenDelayDispatch:domains];
}
}];
}

- (void)preResolveDomains {
Expand Down Expand Up @@ -502,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 @@ -579,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 @@ -610,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 Expand Up @@ -716,4 +756,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
43 changes: 29 additions & 14 deletions MSDKDns/CacheManager/MSDKDnsNetworkManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import "MSDKDnsNetworkManager.h"
#import "MSDKDnsLog.h"
#import "MSDKDnsInfoTool.h"
#import "MSDKDnsManager.h"
#import "MSDKDnsParamsManager.h"
#import <UIKit/UIKit.h>
Expand Down Expand Up @@ -68,32 +69,36 @@ - (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];
//对保活域名发送解析请求
[self getHostsByKeepAliveDomains];
//重置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];
//对保活域名发送解析请求
[self getHostsByKeepAliveDomains];
}];

_reachability = [MSDKDnsReachability reachabilityForInternetConnection];
[_reachability startNotifier];
Expand Down Expand Up @@ -275,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
2 changes: 2 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- (void)msdkDnsSetEnableReport: (BOOL)enableReport;
- (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains;
- (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType;
- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains;

- (NSString *) msdkDnsGetMDnsIp;
- (NSString *) msdkDnsGetMOpenId;
Expand All @@ -44,4 +45,5 @@
- (BOOL)msdkDnsGetEnableReport;
- (NSArray *)msdkDnsGetPreResolvedDomains;
- (HttpDnsAddressType)msdkDnsGetAddressType;
- (NSArray *)msdkDnsGetKeepAliveDomains;
@end
16 changes: 16 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ @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
Expand Down Expand Up @@ -156,6 +158,14 @@ - (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType {
});
}

- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
self.keepAliveDomains = [domains copy];
});
}



#pragma mark - getter

- (BOOL)msdkDnsGetHttpOnly {
Expand Down Expand Up @@ -229,4 +239,10 @@ - (HttpDnsAddressType)msdkDnsGetAddressType {
return _msdkAddressType;
}

- (NSArray *)msdkDnsGetKeepAliveDomains {
return _keepAliveDomains;
}



@end
7 changes: 6 additions & 1 deletion MSDKDns/MSDKDns.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __MSDKDns_H__
#define __MSDKDns_H__

#define MSDKDns_Version @"1.3.5"
#define MSDKDns_Version @"1.4.0"

#import <Foundation/Foundation.h>

Expand Down Expand Up @@ -79,6 +79,11 @@ typedef struct DnsConfigStruct {
*/
- (void) WGSetPreResolvedDomains:(NSArray *)domains;

/**
* 设置保活的域名,设置的域名会定时更新缓存,数量不能大于8个
*/
- (void) WGSetKeepAliveDomains:(NSArray *)domains;

#pragma mark - 域名解析接口,按需调用
/**
域名同步解析(通用接口)
Expand Down
6 changes: 6 additions & 0 deletions MSDKDns/MSDKDns.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,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]];
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
Loading

0 comments on commit ea5046a

Please sign in to comment.