Skip to content

Commit

Permalink
Merge pull request #30 from HuYuee/feature/v1.9.0
Browse files Browse the repository at this point in the history
Feature/v1.9.0
  • Loading branch information
Vastness authored Oct 26, 2023
2 parents 5e0ff61 + 12001e7 commit cd98ddc
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 109 deletions.
36 changes: 18 additions & 18 deletions MSDKDns.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MACOSX_DEPLOYMENT_TARGET = "";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = NO;
Expand Down Expand Up @@ -1010,7 +1010,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MACOSX_DEPLOYMENT_TARGET = "";
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -1039,15 +1039,15 @@
GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp;
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_LIBRARY_DIR)/Tcl/sqlite3",
);
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
Expand Down Expand Up @@ -1086,15 +1086,15 @@
GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp;
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_LIBRARY_DIR)/Tcl/sqlite3",
);
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
"-ObjC",
Expand Down Expand Up @@ -1133,11 +1133,11 @@
);
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
Expand Down Expand Up @@ -1175,11 +1175,11 @@
GCC_PREPROCESSOR_DEFINITIONS = "IS_INTL=1";
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
"-ObjC",
Expand Down Expand Up @@ -1220,15 +1220,15 @@
);
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_LIBRARY_DIR)/Tcl/sqlite3",
);
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
Expand Down Expand Up @@ -1268,15 +1268,15 @@
GCC_PREPROCESSOR_DEFINITIONS = "IS_INTL=1";
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_LIBRARY_DIR)/Tcl/sqlite3",
);
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
"-ObjC",
Expand Down Expand Up @@ -1310,11 +1310,11 @@
GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp;
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
Expand Down Expand Up @@ -1350,11 +1350,11 @@
GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp;
INFOPLIST_FILE = MSDKDns/Info.plist;
INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LINK_WITH_STANDARD_LIBRARIES = NO;
MACH_O_TYPE = mh_object;
MACOSX_DEPLOYMENT_TARGET = "";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
OTHER_CFLAGS = "-fembed-bitcode";
OTHER_LDFLAGS = (
"-ObjC",
Expand Down
2 changes: 1 addition & 1 deletion MSDKDns/CacheManager/MSDKDnsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef enum {
- (void)dnsHasDone:(MSDKDnsService *)service;
- (void)cacheDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain;
- (void)clearCacheForDomain:(NSString *)domain;
- (void)clearCacheForDomains:(NSArray *)domains;
- (void)clearCacheForDomains:(NSArray<NSString *> *)domains;
- (void)clearAllCache;
- (NSDictionary *)getDnsDetail:(NSString *)domain;

Expand Down
122 changes: 98 additions & 24 deletions MSDKDns/CacheManager/MSDKDnsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ @interface MSDKDnsManager ()
@property (nonatomic, assign, readwrite) HttpDnsSdkStatus sdkStatus;
@property (nonatomic, strong, readwrite) NSArray * dnsServers;
@property (strong, nonatomic) NSMutableURLRequest *request;
@property (strong, nonatomic, readwrite) NSMutableDictionary * cacheDomainCountDict;

@end

Expand All @@ -43,6 +44,10 @@ - (void)dealloc {
[self.serviceArray removeAllObjects];
[self setServiceArray:nil];
}
if (_cacheDomainCountDict) {
[self.cacheDomainCountDict removeAllObjects];
[self setCacheDomainCountDict:nil];
}
}

#pragma mark - init
Expand All @@ -64,6 +69,7 @@ - (instancetype) init {
_serviceArray = [[NSMutableArray alloc] init];
_sdkStatus = net_undetected;
_dnsServers = [self defaultServers];
_cacheDomainCountDict = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand Down Expand Up @@ -200,6 +206,55 @@ - (NSDictionary *)getHostsByNamesEnableExpired:(NSArray *)domains verbose:(BOOL)
NSDictionary * result = verbose?
[self fullResultDictionaryEnableExpired:domains fromCache:cacheDomainDict toEmpty:toEmptyDomains] :
[self resultDictionaryEnableExpired:domains fromCache:cacheDomainDict toEmpty:toEmptyDomains];
// 当开启乐观DNS之后,如果结果为0则上报errorCode=3,要所有结果都为0
BOOL needReport = NO;
if (verbose) {
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
NSDictionary *domainData = result[domain];
if (domainData && domainData.count > 0) {
needReport = NO;
break;
} else {
needReport = YES;
}
}
} else {
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
NSArray *domainResArray = result[domain];
if (domainResArray && domainResArray.count > 0) {
if ([domainResArray[0] isEqualToString:@"0"] && [domainResArray[1] isEqualToString:@"0"]) {
needReport = YES;
} else {
needReport = NO;
break;
}
} else {
needReport = YES;
}
}
}
if (needReport && domains) {
for (int i = 0; i < [domains count]; i++) {
NSString *domain = [domains objectAtIndex:i];
[[AttaReport sharedInstance] reportEvent:@{
MSDKDns_ErrorCode: MSDKDns_NoData,
@"eventName": MSDKDnsEventHttpDnsCached,
@"dnsIp": [[MSDKDnsManager shareInstance] currentDnsServer],
@"req_dn": domain,
@"req_type": @"a",
@"req_timeout": @0,
@"req_ttl": @0,
@"req_query": @0,
@"req_ip": @"",
@"spend": @0,
@"statusCode": @0,
@"count": @1,
@"isCache": @1,
}];
}
}
return result;
}

Expand Down Expand Up @@ -566,14 +621,16 @@ - (void)clearCacheForDomain:(NSString *)domain {

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

- (void)clearAllCache {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
MSDKDNSLOG(@"MSDKDns clearCache");
MSDKDNSLOG(@"MSDKDns cleared all caches!");
if (self.domainDict) {
[self.domainDict removeAllObjects];
self.domainDict = nil;
Expand All @@ -583,26 +640,43 @@ - (void)clearAllCache {

#pragma mark - uploadReport
- (void)hitCacheAttaUploadReport:(NSString *)domain {
static int count = 0;
count ++;
if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport] && [[AttaReport sharedInstance] shoulReportDnsSpend]) {
[[AttaReport sharedInstance] reportEvent:@{
MSDKDns_ErrorCode: MSDKDns_Success,
@"eventName": MSDKDnsEventHttpDnsCached,
@"dnsIp": [[MSDKDnsManager shareInstance] currentDnsServer],
@"req_dn": domain,
@"req_type": @"a",
@"req_timeout": @0,
@"req_ttl": @0,
@"req_query": @0,
@"req_ip": @"",
@"spend": @0,
@"statusCode": @0,
@"count":[NSString stringWithFormat:@"%d", count],
@"isCache": @1,
}];
count = 0;
}
// 检查控制台解析监控上报开关是否开启
if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport]) {
if (self.cacheDomainCountDict) {
NSNumber *num = self.cacheDomainCountDict[domain];
if (num) {
int numInt = num.intValue + 1;
[self.cacheDomainCountDict setValue:[NSNumber numberWithInt:numInt] forKey:domain];
} else {
[self.cacheDomainCountDict setValue:[NSNumber numberWithInt:1] forKey:domain];
}

if ([[AttaReport sharedInstance] shoulReportDnsSpend]) {
NSArray *dictKey = [self.cacheDomainCountDict allKeys];
NSInteger length = [dictKey count];
for (int i = 0; i < length; i++) {
id domainKey = [dictKey objectAtIndex:i];
NSNumber *cacheCount = [self.cacheDomainCountDict objectForKey:domainKey];
[[AttaReport sharedInstance] reportEvent:@{
MSDKDns_ErrorCode: MSDKDns_Success,
@"eventName": MSDKDnsEventHttpDnsCached,
@"dnsIp": [[MSDKDnsManager shareInstance] currentDnsServer],
@"req_dn": domainKey,
@"req_type": @"a",
@"req_timeout": @0,
@"req_ttl": @0,
@"req_query": @0,
@"req_ip": @"",
@"spend": @0,
@"statusCode": @0,
@"count": cacheCount,
@"isCache": @1,
}];
}
[self.cacheDomainCountDict removeAllObjects];
}
}
}
}

- (void)uploadReport:(BOOL)isFromCache Domain:(NSString *)domain NetStack:(msdkdns::MSDKDNS_TLocalIPStack)netStack {
Expand Down Expand Up @@ -1205,7 +1279,7 @@ - (void)switchToMainServer {

- (void)resetDnsServers:(NSArray *)servers {
self.waitToSwitch = YES;
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
dispatch_barrier_async([MSDKDnsInfoTool msdkdns_resolver_queue], ^{
NSMutableArray *array = [[NSMutableArray alloc] init];
if (servers && [servers count] > 0) {
[array addObjectsFromArray: servers];
Expand Down
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.8.1"
#define MSDKDns_Version @"1.9.0"

#import <Foundation/Foundation.h>

Expand Down Expand Up @@ -192,6 +192,11 @@ typedef struct DnsConfigStruct {
*/
- (void)clearCache;

/**
需要清除的host域名数组。如果需要清空全部数据,传nil或者空数组即可
*/
- (void)clearHostCache:(NSArray *)hostArray;

#pragma mark-查询网络栈支持情况
/**
查询网络栈支持情况
Expand Down
10 changes: 9 additions & 1 deletion MSDKDns/MSDKDns.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ - (BOOL) initConfigWithDictionary:(NSDictionary *)config {
conf->minutesBeforeSwitchToMain = [[config objectForKey:@"minutesBeforeSwitchToMain"] intValue];
conf->enableReport = [[config objectForKey:@"enableReport"] boolValue];
conf->addressType = (HttpDnsAddressType)[[config objectForKey:@"addressType"] intValue];
return [self initConfig:conf];
return [self initConfig:conf];
}

#pragma mark - setting
Expand Down Expand Up @@ -408,4 +408,12 @@ - (void)clearCache {
[[MSDKDnsManager shareInstance] clearAllCache];
}

- (void)clearHostCache:(NSArray *)hostArray {
if (hostArray == nil || ([hostArray isKindOfClass:[NSArray class]] && hostArray.count == 0)) {
[[MSDKDnsManager shareInstance] clearAllCache];
}else if ([hostArray isKindOfClass:[NSArray class]] && hostArray.count > 0){
[[MSDKDnsManager shareInstance] clearCacheForDomains:hostArray];
}
}

@end
Loading

0 comments on commit cd98ddc

Please sign in to comment.