Skip to content

Commit

Permalink
Merge pull request #1 from DNSPod/feature/upgrade
Browse files Browse the repository at this point in the history
Feature/upgrade
  • Loading branch information
ocleo1 authored May 24, 2021
2 parents e727ec2 + 2efe331 commit d18f56d
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 205 deletions.
13 changes: 7 additions & 6 deletions MSDKDns.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "44224FBF1B312DD6003497C4"
BuildableName = "MSDKDns.framework"
BlueprintName = "MSDKDns"
ReferencedContainer = "container:MSDKDns.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "44224FBF1B312DD6003497C4"
BuildableName = "MSDKDns.framework"
BlueprintName = "MSDKDns"
ReferencedContainer = "container:MSDKDns.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
67 changes: 67 additions & 0 deletions MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns_C11.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DD43F493231CC36D0000A89F"
BuildableName = "MSDKDns_C11.framework"
BlueprintName = "MSDKDns_C11"
ReferencedContainer = "container:MSDKDns.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DD43F493231CC36D0000A89F"
BuildableName = "MSDKDns_C11.framework"
BlueprintName = "MSDKDns_C11"
ReferencedContainer = "container:MSDKDns.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
3 changes: 3 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

- (void)getHostByName:(NSString *)domain returnIps:(void (^)(NSArray * ipsArray))handler;
- (NSArray *)getHostByName:(NSString *)domain;
- (void)getHostsByNames:(NSArray *)domains returnIps:(void (^)(NSDictionary * ipsDict))handler;
- (NSDictionary *)getHostsByNames:(NSArray *)domains;
- (void)dnsHasDone:(MSDKDnsService *)service;
- (void)cacheDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain;
- (void)clearCacheForDomain:(NSString *)domain;
- (void)clearCacheForDomains:(NSArray *)domains;
- (void)clearCache;
- (NSDictionary *) getDnsDetail:(NSString *) domain;

Expand Down
173 changes: 162 additions & 11 deletions MSDKDns/CacheManager/MSDKDnsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#import "MSDKDnsNetworkManager.h"
#import "msdkdns_local_ip_stack.h"

extern NSString *DES_HTTP_CHANNEL;
extern NSString *DES_HTTPS_CHANNEL;
extern NSString *AES_HTTP_CHANNEL;
extern NSString *AES_HTTPS_CHANNEL;

@interface MSDKDnsManager ()

Expand Down Expand Up @@ -85,12 +81,7 @@ - (NSArray *) getHostByName:(NSString *)domain {
}
int dnsId = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsId];
NSString * dnsKey = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsKey];
// 增加channel,用channel决定是加密方式是aes还是des
NSString *channel = [[MSDKDnsParamsManager shareInstance] msdkDnsGetChannel];
NSInteger encryptType = 0;
if ([channel isEqualToString:AES_HTTPS_CHANNEL] || [channel isEqualToString:AES_HTTP_CHANNEL]) {
encryptType = 1;
}
HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType];
//进行httpdns请求
MSDKDnsService * dnsService = [[MSDKDnsService alloc] init];
[self.serviceArray addObject:dnsService];
Expand Down Expand Up @@ -161,7 +152,8 @@ - (void) getHostByName:(NSString *)domain returnIps:(void (^)(NSArray *))handler
MSDKDnsService * dnsService = [[MSDKDnsService alloc] init];
[self.serviceArray addObject:dnsService];
__weak __typeof__(self) weakSelf = self;
[dnsService getHostByName:domain TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack returnIps:^() {
HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType];
[dnsService getHostByName:domain TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^() {
__strong __typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf uploadReport:NO Domain:domain NetStack:netStack];
Expand All @@ -175,6 +167,128 @@ - (void) getHostByName:(NSString *)domain returnIps:(void (^)(NSArray *))handler
});
}

- (void)getHostsByNames:(NSArray *)domains returnIps:(void (^)(NSDictionary * ipsDict))handler {
// 获取当前ipv4/ipv6/双栈网络环境
msdkdns::MSDKDNS_TLocalIPStack netStack = msdkdns::msdkdns_detect_local_ip_stack();
__block float timeOut = 2.0;
__block NSDictionary * cacheDomainDict = nil;
dispatch_sync([MSDKDnsInfoTool msdkdns_queue], ^{
if (domains && [domains count] > 0 && _domainDict) {
cacheDomainDict = [[NSDictionary alloc] initWithDictionary:_domainDict];
}
timeOut = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMTimeOut];
});
// 待查询数组
NSMutableArray *toCheckDomains = [NSMutableArray array];
// 查找缓存,缓存中有HttpDns数据且ttl未超时则直接返回结果,不存在或者ttl超时则放入待查询数组
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
if (![self domianCached:domain]) {
[toCheckDomains addObject:domain];
} else {
MSDKDNSLOG(@"TTL has not expiried,return result from cache directly!");
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
[self uploadReport:YES Domain:domain NetStack:netStack];
});
}
}
// 全部有缓存时,直接返回
if([toCheckDomains count] == 0) {
NSDictionary * result = [self resultDictionary:domains DomainDic:_domainDict];
if (handler) {
handler(result);
}
return;
}
NSString *toCheckDomainStr = [toCheckDomains componentsJoinedByString:@","];
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
if (!_serviceArray) {
self.serviceArray = [[NSMutableArray alloc] init];
}
int dnsId = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsId];
NSString * dnsKey = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsKey];
//进行httpdns请求
MSDKDnsService * dnsService = [[MSDKDnsService alloc] init];
[self.serviceArray addObject:dnsService];
__weak __typeof__(self) weakSelf = self;
HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType];
[dnsService getHostsByNames:toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^() {
__strong __typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf uploadReport:NO Domain:toCheckDomainStr NetStack:netStack];
[strongSelf dnsHasDone:dnsService];
NSDictionary * result = [strongSelf resultDictionary:domains DomainDic:_domainDict];
if (handler) {
handler(result);
}
}
}];
});
}

- (NSDictionary *)getHostsByNames:(NSArray *)domains {
// 获取当前ipv4/ipv6/双栈网络环境
msdkdns::MSDKDNS_TLocalIPStack netStack = msdkdns::msdkdns_detect_local_ip_stack();
__block float timeOut = 2.0;
__block NSDictionary * cacheDomainDict = nil;
dispatch_sync([MSDKDnsInfoTool msdkdns_queue], ^{
if (domains && [domains count] > 0 && _domainDict) {
cacheDomainDict = [[NSDictionary alloc] initWithDictionary:_domainDict];
}
timeOut = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMTimeOut];
});
// 待查询数组
NSMutableArray *toCheckDomains = [NSMutableArray array];
// 查找缓存,缓存中有HttpDns数据且ttl未超时则直接返回结果,不存在或者ttl超时则放入待查询数组
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
if (![self domianCached:domain]) {
[toCheckDomains addObject:domain];
} else {
MSDKDNSLOG(@"TTL has not expiried,return result from cache directly!");
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
[self uploadReport:YES Domain:domain NetStack:netStack];
});
}
}
// 全部有缓存时,直接返回
if([toCheckDomains count] == 0) {
NSDictionary * result = [self resultDictionary:domains DomainDic:_domainDict];
return result;
}
NSString *toCheckDomainStr = [toCheckDomains componentsJoinedByString:@","];
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
if (!_serviceArray) {
self.serviceArray = [[NSMutableArray alloc] init];
}
int dnsId = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsId];
NSString * dnsKey = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMDnsKey];
HttpDnsEncryptType encryptType = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEncryptType];
//进行httpdns请求
MSDKDnsService * dnsService = [[MSDKDnsService alloc] init];
[self.serviceArray addObject:dnsService];
__weak __typeof__(self) weakSelf = self;
[dnsService getHostsByNames:toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^() {
__strong __typeof(self) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf uploadReport:NO Domain:toCheckDomainStr NetStack:netStack];
[strongSelf dnsHasDone:dnsService];
}
dispatch_semaphore_signal(sema);
}];
});
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, timeOut * NSEC_PER_SEC));
cacheDomainDict = nil;
dispatch_sync([MSDKDnsInfoTool msdkdns_queue], ^{
if (domains && [domains count] > 0 && _domainDict) {
cacheDomainDict = [[NSDictionary alloc] initWithDictionary:_domainDict];
}
});
NSDictionary * result = [self resultDictionary:domains DomainDic:cacheDomainDict];
return result;
}

- (NSArray *)resultArray: (NSString *)domain DomainDic:(NSDictionary *)domainDict {
NSMutableArray * ipResult = [@[@"0", @"0"] mutableCopy];
if (domainDict) {
Expand Down Expand Up @@ -205,6 +319,16 @@ - (NSArray *)resultArray: (NSString *)domain DomainDic:(NSDictionary *)domainDic
return ipResult;
}

- (NSDictionary *)resultDictionary: (NSArray *)domains DomainDic:(NSDictionary *)domainDict {
NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
NSArray *resultArray = [self resultArray:domain DomainDic:domainDict];
[resultDict setObject:resultArray forKey:domain];
}
return resultDict;
}

- (void)dnsHasDone:(MSDKDnsService *)service {
NSArray * tmpArray = [NSArray arrayWithArray:self.serviceArray];
NSMutableArray * tmp = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -237,6 +361,13 @@ - (void)clearCacheForDomain:(NSString *)domain {
}
}

- (void)clearCacheForDomains:(NSArray *)domains {
for(int i = 0; i < [domains count]; i++) {
NSString* domain = [domains objectAtIndex:i];
[self clearCacheForDomain:domain];
}
}

- (void)clearCache {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
MSDKDNSLOG(@"MSDKDns clearCache");
Expand Down Expand Up @@ -520,4 +651,24 @@ - (NSString *)getIPsStringFromIPsArray:(NSArray *)ipsArray {
return ipsStr;
}

# pragma mark - getCacheData
// 检查是否存在有效缓冲
- (BOOL) domianCached:(NSString *)domain {
NSDictionary * domainInfo = _domainDict[domain];
if (domainInfo && [domainInfo isKindOfClass:[NSDictionary class]]) {
NSDictionary * cacheDict = domainInfo[kMSDKHttpDnsCache_A];
if (!cacheDict || ![cacheDict isKindOfClass:[NSDictionary class]]) {
cacheDict = domainInfo[kMSDKHttpDnsCache_4A];
}
if (cacheDict && [cacheDict isKindOfClass:[NSDictionary class]]) {
NSString * ttlExpried = cacheDict[kTTLExpired];
double timeInterval = [[NSDate date] timeIntervalSince1970];
if (timeInterval <= ttlExpried.doubleValue) {
return YES;
}
}
}
return NO;
}

@end
9 changes: 7 additions & 2 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#import <Foundation/Foundation.h>
#import "MSDKDns.h"

@interface MSDKDnsParamsManager : NSObject

Expand All @@ -14,14 +15,18 @@
- (void)msdkDnsSetMDnsIp:(NSString *) mdnsIp;
- (void)msdkDnsSetMOpenId:(NSString *) mdnsOpenId;
- (void)msdkDnsSetMAppId:(NSString *) mdnsAppId MTimeOut:(int)mdnsTimeOut;
- (void)msdkDnsSetMAppId:(NSString *) mdnsAppId MTimeOut:(int)mdnsTimeOut MChannel:(NSString *)mdnsChannel;
- (void)msdkDnsSetMAppId:(NSString *) mdnsAppId MTimeOut:(int)mdnsTimeOut MEncryptType:(HttpDnsEncryptType)mdnsEncryptType;
- (void)msdkDnsSetMAppId:(NSString *) mdnsAppId MToken:(NSString* )mdnsToken MTimeOut:(int)mdnsTimeOut MEncryptType:(HttpDnsEncryptType)mdnsEncryptType;
- (void)msdkDnsSetMDnsId:(int) mdnsId MDnsKey:(NSString *)mdnsKey;
- (void)msdkDnsSetRouteIp:(NSString *)routeIp;

- (NSString *) msdkDnsGetMDnsIp;
- (NSString *) msdkDnsGetMOpenId;
- (NSString *) msdkDnsGetMAppId;
- (int) msdkDnsGetMDnsId;
- (NSString *) msdkDnsGetMDnsKey;
- (float) msdkDnsGetMTimeOut;
- (NSString *)msdkDnsGetChannel;
- (HttpDnsEncryptType)msdkDnsGetEncryptType;
- (NSString *)msdkDnsGetMToken;
- (NSString *)msdkDnsGetRouteIp;
@end
Loading

0 comments on commit d18f56d

Please sign in to comment.