diff --git a/MSDKDns.xcodeproj/project.pbxproj b/MSDKDns.xcodeproj/project.pbxproj index 9abe592..67b28f9 100644 --- a/MSDKDns.xcodeproj/project.pbxproj +++ b/MSDKDns.xcodeproj/project.pbxproj @@ -38,6 +38,16 @@ 54EA82232760890B005F68A9 /* AttaReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 54EA82212760890B005F68A9 /* AttaReport.m */; }; 54EA822427608A55005F68A9 /* AttaReport.h in Headers */ = {isa = PBXBuildFile; fileRef = 54EA82202760890B005F68A9 /* AttaReport.h */; }; 54EA822527608A58005F68A9 /* AttaReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 54EA82212760890B005F68A9 /* AttaReport.m */; }; + 5F1E288028B72A7D00AD0D9F /* HTTPDNSORM.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F1E287B28B72A7D00AD0D9F /* HTTPDNSORM.m */; }; + 5F1E288128B72A7D00AD0D9F /* HTTPDNSORM.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F1E287B28B72A7D00AD0D9F /* HTTPDNSORM.m */; }; + 5F1E288228B72A7D00AD0D9F /* HTTPDNSORM.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287C28B72A7D00AD0D9F /* HTTPDNSORM.h */; }; + 5F1E288328B72A7D00AD0D9F /* HTTPDNSORM.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287C28B72A7D00AD0D9F /* HTTPDNSORM.h */; }; + 5F1E288428B72A7D00AD0D9F /* MSDKDnsDB.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F1E287D28B72A7D00AD0D9F /* MSDKDnsDB.m */; }; + 5F1E288528B72A7D00AD0D9F /* MSDKDnsDB.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F1E287D28B72A7D00AD0D9F /* MSDKDnsDB.m */; }; + 5F1E288628B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287E28B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h */; }; + 5F1E288728B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287E28B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h */; }; + 5F1E288828B72A7D00AD0D9F /* MSDKDnsDB.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287F28B72A7D00AD0D9F /* MSDKDnsDB.h */; }; + 5F1E288928B72A7D00AD0D9F /* MSDKDnsDB.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F1E287F28B72A7D00AD0D9F /* MSDKDnsDB.h */; }; 5F5DAF4E28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */; }; 5F5DAF4F28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */; }; 5F5DAF5028A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */; }; @@ -110,6 +120,12 @@ 54EA81F1275478D1005F68A9 /* httpdnsIps.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = httpdnsIps.h; sourceTree = ""; }; 54EA82202760890B005F68A9 /* AttaReport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AttaReport.h; sourceTree = ""; }; 54EA82212760890B005F68A9 /* AttaReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AttaReport.m; sourceTree = ""; }; + 5F1E287B28B72A7D00AD0D9F /* HTTPDNSORM.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTTPDNSORM.m; sourceTree = ""; }; + 5F1E287C28B72A7D00AD0D9F /* HTTPDNSORM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPDNSORM.h; sourceTree = ""; }; + 5F1E287D28B72A7D00AD0D9F /* MSDKDnsDB.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSDKDnsDB.m; sourceTree = ""; }; + 5F1E287E28B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "HTTPDNSORM+WCTTableCoding.h"; sourceTree = ""; }; + 5F1E287F28B72A7D00AD0D9F /* MSDKDnsDB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsDB.h; sourceTree = ""; }; + 5F1E288F28B86DFD00AD0D9F /* WCDB.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WCDB.framework; path = ../WCDB.framework; sourceTree = ""; }; 5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsTCPSpeedTester.h; sourceTree = ""; }; 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSDKDnsTCPSpeedTester.m; sourceTree = ""; }; 5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsSpeedTester.h; sourceTree = ""; }; @@ -179,6 +195,7 @@ 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */, C8EBE7D1256664C400BEFEEC /* aes.h */, C8EBE7D2256664C500BEFEEC /* aes.mm */, + 5F85CBA828B4B3B1003D20D1 /* WCDB */, 54EA821F2760890B005F68A9 /* Reporter */, 501001EB215E1F1D003288A5 /* Network */, 445B363D1CBBBFAF00BD4345 /* Manager */, @@ -251,9 +268,22 @@ path = Reporter; sourceTree = ""; }; + 5F85CBA828B4B3B1003D20D1 /* WCDB */ = { + isa = PBXGroup; + children = ( + 5F1E287C28B72A7D00AD0D9F /* HTTPDNSORM.h */, + 5F1E287B28B72A7D00AD0D9F /* HTTPDNSORM.m */, + 5F1E287E28B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h */, + 5F1E287F28B72A7D00AD0D9F /* MSDKDnsDB.h */, + 5F1E287D28B72A7D00AD0D9F /* MSDKDnsDB.m */, + ); + path = WCDB; + sourceTree = ""; + }; C8EBE7D92566675800BEFEEC /* Frameworks */ = { isa = PBXGroup; children = ( + 5F1E288F28B86DFD00AD0D9F /* WCDB.framework */, C8EBE7DA2566675800BEFEEC /* libc++.tbd */, ); name = Frameworks; @@ -266,7 +296,9 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 5F1E288228B72A7D00AD0D9F /* HTTPDNSORM.h in Headers */, 44BFE2481CA59D9800D7FE87 /* MSDKDnsReachability.h in Headers */, + 5F1E288828B72A7D00AD0D9F /* MSDKDnsDB.h in Headers */, 504F543A1ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.h in Headers */, 445B36691CBD1D4700BD4345 /* MSDKDnsNetworkManager.h in Headers */, C8EBE7D3256664C500BEFEEC /* aes.h in Headers */, @@ -278,6 +310,7 @@ 5F5DAF4E28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */, 448EE4E81B329899004A2131 /* MSDKDnsResolver.h in Headers */, 54EA82222760890B005F68A9 /* AttaReport.h in Headers */, + 5F1E288628B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h in Headers */, 5F5DAF5228A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */, 44BFE2371CA58C3A00D7FE87 /* MSDKDnsInfoTool.h in Headers */, 502422EC2140073F0094403C /* MSDKDnsParamsManager.h in Headers */, @@ -291,7 +324,9 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 5F1E288328B72A7D00AD0D9F /* HTTPDNSORM.h in Headers */, DD43F4A6231CC36D0000A89F /* MSDKDnsReachability.h in Headers */, + 5F1E288928B72A7D00AD0D9F /* MSDKDnsDB.h in Headers */, DD43F4A7231CC36D0000A89F /* MSDKDnsHttpMessageTools.h in Headers */, DD43F4A8231CC36D0000A89F /* MSDKDnsNetworkManager.h in Headers */, 2FBE52E825E77AFE0012A0DF /* aes.h in Headers */, @@ -303,6 +338,7 @@ 5F5DAF4F28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */, DD43F4AE231CC36D0000A89F /* MSDKDnsResolver.h in Headers */, 54EA822427608A55005F68A9 /* AttaReport.h in Headers */, + 5F1E288728B72A7D00AD0D9F /* HTTPDNSORM+WCTTableCoding.h in Headers */, 5F5DAF5328A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */, DD43F4AF231CC36D0000A89F /* MSDKDnsInfoTool.h in Headers */, DD43F4B0231CC36D0000A89F /* MSDKDnsParamsManager.h in Headers */, @@ -363,7 +399,7 @@ 44224FB81B312DD6003497C4 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0730; + LastUpgradeCheck = 1340; ORGANIZATIONNAME = Tencent; TargetAttributes = { 44224FBF1B312DD6003497C4 = { @@ -506,9 +542,11 @@ 445B366B1CBD1D4700BD4345 /* MSDKDnsNetworkManager.m in Sources */, 4497F8BA1B4628F000D51391 /* MSDKDnsLog.m in Sources */, 504F543C1ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.m in Sources */, + 5F1E288428B72A7D00AD0D9F /* MSDKDnsDB.m in Sources */, 448EE4E91B329899004A2131 /* MSDKDnsResolver.m in Sources */, 54EA82232760890B005F68A9 /* AttaReport.m in Sources */, 4455D15F1B3A5B90005BF126 /* MSDKDns.m in Sources */, + 5F1E288028B72A7D00AD0D9F /* HTTPDNSORM.m in Sources */, 448EE4E71B329899004A2131 /* LocalDnsResolver.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -529,9 +567,11 @@ DD43F49D231CC36D0000A89F /* MSDKDnsNetworkManager.m in Sources */, DD43F49E231CC36D0000A89F /* MSDKDnsLog.m in Sources */, DD43F49F231CC36D0000A89F /* MSDKDnsHttpMessageTools.m in Sources */, + 5F1E288528B72A7D00AD0D9F /* MSDKDnsDB.m in Sources */, DD43F4A0231CC36D0000A89F /* MSDKDnsResolver.m in Sources */, 54EA822527608A58005F68A9 /* AttaReport.m in Sources */, DD43F4A1231CC36D0000A89F /* MSDKDns.m in Sources */, + 5F1E288128B72A7D00AD0D9F /* HTTPDNSORM.m in Sources */, DD43F4A2231CC36D0000A89F /* LocalDnsResolver.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -548,13 +588,24 @@ CLANG_CXX_LIBRARY = "compiler-default"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -595,13 +646,24 @@ CLANG_CXX_LIBRARY = "compiler-default"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -617,7 +679,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 = 10.0; MACOSX_DEPLOYMENT_TARGET = ""; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; @@ -638,15 +700,18 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; ENABLE_BITCODE = YES; - FRAMEWORK_SEARCH_PATHS = "$(inherited)"; + FRAMEWORK_SEARCH_PATHS = ( + "$(PROJECT_DIR)/../**", + "$(inherited)", + ); GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp; INFOPLIST_FILE = MSDKDns/Info.plist; INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)"; - IPHONEOS_DEPLOYMENT_TARGET = 6.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.5.0; + MARKETING_VERSION = 1.6.0; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( @@ -677,15 +742,18 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; ENABLE_BITCODE = YES; - FRAMEWORK_SEARCH_PATHS = "$(inherited)"; + FRAMEWORK_SEARCH_PATHS = ( + "$(PROJECT_DIR)/../**", + "$(inherited)", + ); GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp; INFOPLIST_FILE = MSDKDns/Info.plist; INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)"; - IPHONEOS_DEPLOYMENT_TARGET = 6.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.5.0; + MARKETING_VERSION = 1.6.0; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( "-ObjC", @@ -714,7 +782,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; ENABLE_BITCODE = YES; - FRAMEWORK_SEARCH_PATHS = "$(inherited)"; + FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/.."; GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp; INFOPLIST_FILE = MSDKDns/Info.plist; INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)"; @@ -722,7 +790,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.5.0; + MARKETING_VERSION = 1.6.0; ONLY_ACTIVE_ARCH = NO; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( @@ -753,7 +821,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; ENABLE_BITCODE = YES; - FRAMEWORK_SEARCH_PATHS = "$(inherited)"; + FRAMEWORK_SEARCH_PATHS = "$(PROJECT_DIR)/.."; GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp; INFOPLIST_FILE = MSDKDns/Info.plist; INSTALL_PATH = "$(BUILT_PRODUCTS_DIR)"; @@ -761,7 +829,7 @@ LINK_WITH_STANDARD_LIBRARIES = NO; MACH_O_TYPE = mh_object; MACOSX_DEPLOYMENT_TARGET = ""; - MARKETING_VERSION = 1.5.0; + MARKETING_VERSION = 1.6.0; OTHER_CFLAGS = "-fembed-bitcode"; OTHER_LDFLAGS = ( "-ObjC", diff --git a/MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns.xcscheme b/MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns.xcscheme index 9bba7e2..4cfa5da 100644 --- a/MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns.xcscheme +++ b/MSDKDns.xcodeproj/xcshareddata/xcschemes/MSDKDns.xcscheme @@ -1,6 +1,6 @@ 0 && _domainDict) { + cacheDomainDict = [[NSDictionary alloc] initWithDictionary:_domainDict]; + } + timeOut = [[MSDKDnsParamsManager shareInstance] msdkDnsGetMTimeOut]; + }); + // 待查询数组 + NSMutableArray *toCheckDomains = [NSMutableArray array]; + // 需要排除结果的域名数组 + NSMutableArray *toEmptyDomains = [NSMutableArray array]; + // 查找缓存,不存在或者ttl超时则放入待查询数组,ttl超时还放入排除结果的数组以便如果禁用返回ttl过期的解析结果则进行排除结果 + for (int i = 0; i < [domains count]; i++) { + NSString *domain = [domains objectAtIndex:i]; + if ([[self domianCache:cacheDomainDict check:domain] isEqualToString:MSDKDnsDomainCacheEmpty]) { + [toCheckDomains addObject:domain]; + } else if ([[self domianCache:cacheDomainDict check:domain] isEqualToString:MSDKDnsDomainCacheExpired]) { + [toCheckDomains addObject:domain]; + [toEmptyDomains addObject:domain]; + } else { + MSDKDNSLOG(@"%@ TTL has not expiried,return result from cache directly!", domain); + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + [self uploadReport:YES Domain:domain NetStack:netStack]; + }); + } + } + // 当待查询数组中存在数据的时候,就开启异步线程执行解析操作,并且更新缓存 + if (toCheckDomains && [toCheckDomains count] != 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]; + MSDKDnsService * dnsService = [[MSDKDnsService alloc] init]; + [self.serviceArray addObject:dnsService]; + __weak __typeof__(self) weakSelf = self; + //进行httpdns请求 + [dnsService getHostsByNames:toCheckDomains TimeOut:timeOut DnsId:dnsId DnsKey:dnsKey NetStack:netStack encryptType:encryptType returnIps:^() { + __strong __typeof(self) strongSelf = weakSelf; + if (strongSelf) { + [toCheckDomains enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [strongSelf uploadReport:NO Domain:obj NetStack:netStack]; + }]; + [strongSelf dnsHasDone:dnsService]; + } + }]; + }); + } + NSDictionary * result = verbose? + [self fullResultDictionaryEnableExpired:domains fromCache:cacheDomainDict toEmpty:toEmptyDomains] : + [self resultDictionaryEnableExpired:domains fromCache:cacheDomainDict toEmpty:toEmptyDomains]; + return result; +} + #pragma mark async - (void)getHostsByNames:(NSArray *)domains @@ -345,6 +407,83 @@ - (NSDictionary *)fullResultDictionary: (NSArray *)domains fromCache:(NSDictiona return resultDict; } +- (NSDictionary *)resultDictionaryEnableExpired: (NSArray *)domains fromCache:(NSDictionary *)domainDict toEmpty:(NSArray *)emptyDomains { + NSMutableDictionary *resultDict = [NSMutableDictionary dictionary]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + for (int i = 0; i < [domains count]; i++) { + NSString *domain = [domains objectAtIndex:i]; + NSArray *arr = [self resultArray:domain fromCache:domainDict]; + BOOL domainNeedEmpty = [emptyDomains containsObject:domain]; + // 缓存过期,并且没有开启使用过期缓存 + if (domainNeedEmpty && !expiredIPEnabled) { + [resultDict setObject:@[@0,@0] forKey:domain]; + } else { + [resultDict setObject:arr forKey:domain]; + } + } + return resultDict; +} + +- (NSDictionary *)fullResultDictionaryEnableExpired: (NSArray *)domains fromCache:(NSDictionary *)domainDict toEmpty:(NSArray *)emptyDomains { + NSMutableDictionary *resultDict = [NSMutableDictionary dictionary]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + for (int i = 0; i < [domains count]; i++) { + NSString *domain = [domains objectAtIndex:i]; + BOOL domainNeedEmpty = [emptyDomains containsObject:domain]; + NSMutableDictionary * ipResult = [NSMutableDictionary dictionary]; + BOOL httpOnly = [[MSDKDnsParamsManager shareInstance] msdkDnsGetHttpOnly]; + if (domainDict) { + NSDictionary * cacheDict = domainDict[domain]; + if (cacheDict && [cacheDict isKindOfClass:[NSDictionary class]]) { + + NSDictionary * hresultDict_A = cacheDict[kMSDKHttpDnsCache_A]; + NSDictionary * hresultDict_4A = cacheDict[kMSDKHttpDnsCache_4A]; + + if (!httpOnly) { + NSDictionary * lresultDict = cacheDict[kMSDKLocalDnsCache]; + if (lresultDict && [lresultDict isKindOfClass:[NSDictionary class]]) { + NSArray *ipsArray = [lresultDict[kIP] mutableCopy]; + if (ipsArray.count == 2) { + // 缓存过期,并且没有开启使用过期缓存 + if (domainNeedEmpty && !expiredIPEnabled) { + [ipResult setObject:@[@0] forKey:@"ipv4"]; + [ipResult setObject:@[@0] forKey:@"ipv6"]; + } else { + [ipResult setObject:@[ipsArray[0]] forKey:@"ipv4"]; + [ipResult setObject:@[ipsArray[1]] forKey:@"ipv6"]; + } + } + } + } + if (hresultDict_A && [hresultDict_A isKindOfClass:[NSDictionary class]]) { + NSArray * ipsArray = hresultDict_A[kIP]; + if (ipsArray && [ipsArray isKindOfClass:[NSArray class]] && ipsArray.count > 0) { + // 缓存过期,并且没有开启使用过期缓存 + if (domainNeedEmpty && !expiredIPEnabled) { + [ipResult setObject:@[@0] forKey:@"ipv4"]; + } else { + [ipResult setObject:ipsArray forKey:@"ipv4"]; + } + } + } + if (hresultDict_4A && [hresultDict_4A isKindOfClass:[NSDictionary class]]) { + NSArray * ipsArray = hresultDict_4A[kIP]; + if (ipsArray && [ipsArray isKindOfClass:[NSArray class]] && ipsArray.count > 0) { + // 缓存过期,并且没有开启使用过期缓存 + if (domainNeedEmpty && !expiredIPEnabled) { + [ipResult setObject:@[@0] forKey:@"ipv6"]; + } else { + [ipResult setObject:ipsArray forKey:@"ipv6"]; + } + } + } + } + } + [resultDict setObject:ipResult forKey:domain]; + } + return resultDict; +} + - (void)dnsHasDone:(MSDKDnsService *)service { NSArray * tmpArray = [NSArray arrayWithArray:self.serviceArray]; NSMutableArray * tmp = [[NSMutableArray alloc] init]; @@ -685,6 +824,70 @@ - (BOOL) domianCache:(NSDictionary *)cache hit:(NSString *)domain { } return NO; } + +// 检查缓存状态 +- (NSString *) domianCache:(NSDictionary *)cache check:(NSString *)domain { + NSDictionary * domainInfo = cache[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 MSDKDnsDomainCacheHit; + } else { + return MSDKDnsDomainCacheExpired; + } + } + } + return MSDKDnsDomainCacheEmpty; +} + +- (void)loadIPsFromPersistCacheAsync { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + NSDictionary *result = [[MSDKDnsDB shareInstance] getDataFromDB]; + MSDKDNSLOG(@"loadDB domainInfo = %@",result); + NSMutableArray *expiredDomains = [[NSMutableArray alloc] init]; + for (NSString *domain in result) { + NSDictionary *domainInfo = [result valueForKey:domain]; + if ([self isDomainCacheExpired:domainInfo]) { + [expiredDomains addObject:domain]; + } + [self cacheDomainInfo:domainInfo Domain:domain]; + } + // 删除本地持久化缓存中过期缓存 + if (expiredDomains && expiredDomains.count > 0){ + [[MSDKDnsDB shareInstance] deleteDBData:expiredDomains]; + } + }); +} + +- (BOOL)isDomainCacheExpired: (NSDictionary *)domainInfo { + NSDictionary *httpDnsIPV4Info = [domainInfo valueForKey:kMSDKHttpDnsCache_A]; + NSDictionary *httpDnsIPV6Info = [domainInfo valueForKey:kMSDKHttpDnsCache_4A]; + NSMutableString *expiredTime = [[NSMutableString alloc] init]; + double nowTime = [[NSDate date] timeIntervalSince1970]; + if (httpDnsIPV4Info) { + NSString *ipv4ExpiredTime = [httpDnsIPV4Info valueForKey:kTTLExpired]; + if (ipv4ExpiredTime) { + expiredTime = [[NSMutableString alloc]initWithString:ipv4ExpiredTime]; + } + } + if (httpDnsIPV6Info) { + NSString *ipv6ExpiredTime = [httpDnsIPV6Info valueForKey:kTTLExpired]; + if (ipv6ExpiredTime) { + expiredTime = [[NSMutableString alloc]initWithString:ipv6ExpiredTime]; + } + } + if (expiredTime && nowTime <= expiredTime.doubleValue) { + return false; + } + return true; +} + # pragma mark - detect address type - (msdkdns::MSDKDNS_TLocalIPStack)detectAddressType { msdkdns::MSDKDNS_TLocalIPStack netStack = msdkdns::MSDKDNS_ELocalIPStack_None; diff --git a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m index 24d2693..39ff4a2 100644 --- a/MSDKDns/CacheManager/MSDKDnsNetworkManager.m +++ b/MSDKDns/CacheManager/MSDKDnsNetworkManager.m @@ -69,9 +69,12 @@ - (instancetype)init queue:nil usingBlock:^(NSNotification *note) { - MSDKDNSLOG(@"Network did changed,clear MSDKDns cache"); - //网络状态发生变化时清除缓存 - [[MSDKDnsManager shareInstance] clearAllCache]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (!expiredIPEnabled) { + MSDKDNSLOG(@"Network did changed,clear MSDKDns cache"); + //网络状态发生变化时清除缓存 + [[MSDKDnsManager shareInstance] clearAllCache]; + } //对保活域名发送解析请求 [self getHostsByKeepAliveDomains]; //重置ip指针 @@ -83,9 +86,12 @@ - (instancetype)init queue:nil usingBlock:^(NSNotification *note) { - MSDKDNSLOG(@"Application did enter background,clear MSDKDns cache"); - //进入后台时清除缓存,暂停网络监测 - [[MSDKDnsManager shareInstance] clearAllCache]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (!expiredIPEnabled) { + MSDKDNSLOG(@"Application did enter background,clear MSDKDns cache"); + //进入后台时清除缓存,暂停网络监测 + [[MSDKDnsManager shareInstance] clearAllCache]; + } [self.reachability stopNotifier]; }]; diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.h b/MSDKDns/CacheManager/MSDKDnsParamsManager.h index 873b916..08bec4a 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.h +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.h @@ -30,6 +30,8 @@ - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains; - (void)msdkDnsSetIPRankData: (NSDictionary *)IPRankData; - (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive; +- (void)msdkDnsSetExpiredIPEnabled: (BOOL)enable; +- (void)msdkDnsSetPersistCacheIPEnabled: (BOOL)enable; - (NSString *) msdkDnsGetMDnsIp; - (NSString *) msdkDnsGetMOpenId; @@ -50,4 +52,7 @@ - (NSArray *)msdkDnsGetKeepAliveDomains; - (NSDictionary *)msdkDnsGetIPRankData; - (BOOL)msdkDnsGetEnableKeepDomainsAlive; +- (BOOL)msdkDnsGetExpiredIPEnabled; +- (BOOL)msdkDnsGetPersistCacheIPEnabled; + @end diff --git a/MSDKDns/CacheManager/MSDKDnsParamsManager.m b/MSDKDns/CacheManager/MSDKDnsParamsManager.m index 47c42b9..b391bdc 100644 --- a/MSDKDns/CacheManager/MSDKDnsParamsManager.m +++ b/MSDKDns/CacheManager/MSDKDnsParamsManager.m @@ -35,6 +35,8 @@ @interface MSDKDnsParamsManager() @property (strong, nonatomic, readwrite) NSArray* keepAliveDomains; @property (strong, nonatomic, readwrite) NSDictionary* IPRankData; @property (assign, nonatomic, readwrite) BOOL enableKeepDomainsAlive; +@property (assign, nonatomic, readwrite) BOOL expiredIPEnabled; +@property (assign, nonatomic, readwrite) BOOL persistCacheIPEnabled; @end @@ -59,6 +61,7 @@ - (id) init { _enableReport = NO; _msdkAddressType = HttpDnsAddressTypeAuto; _enableKeepDomainsAlive = YES; + _expiredIPEnabled = NO; } return self; } @@ -179,6 +182,17 @@ - (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive { }); } +- (void)msdkDnsSetExpiredIPEnabled: (BOOL)enable { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + self.expiredIPEnabled = enable; + }); +} + +- (void)msdkDnsSetPersistCacheIPEnabled: (BOOL)enable { + dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{ + self.persistCacheIPEnabled = enable; + }); +} #pragma mark - getter @@ -265,5 +279,12 @@ - (BOOL)msdkDnsGetEnableKeepDomainsAlive { return _enableKeepDomainsAlive; } +- (BOOL)msdkDnsGetExpiredIPEnabled { + return _expiredIPEnabled; +} +- (BOOL)msdkDnsGetPersistCacheIPEnabled { + return _persistCacheIPEnabled; +} + @end diff --git a/MSDKDns/MSDKDns.h b/MSDKDns/MSDKDns.h index 59a6f0b..dff0d13 100644 --- a/MSDKDns/MSDKDns.h +++ b/MSDKDns/MSDKDns.h @@ -5,7 +5,7 @@ #ifndef __MSDKDns_H__ #define __MSDKDns_H__ -#define MSDKDns_Version @"1.5.0" +#define MSDKDns_Version @"1.6.0" #import @@ -94,6 +94,16 @@ typedef struct DnsConfigStruct { */ - (void) WGSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive; +/** + * 设置允许返回TTL过期域名的IP,默认关闭 + */ +- (void) WGSetExpiredIPEnabled:(BOOL)enable; + +/** + * 设置持久化缓存功能,默认关闭 + */ +- (void) WGSetPersistCacheIPEnabled:(BOOL)enable; + #pragma mark - 域名解析接口,按需调用 /** 域名同步解析(通用接口) diff --git a/MSDKDns/MSDKDns.m b/MSDKDns/MSDKDns.m index e73c0d8..1383441 100644 --- a/MSDKDns/MSDKDns.m +++ b/MSDKDns/MSDKDns.m @@ -128,6 +128,15 @@ - (void) WGSetNoHijackDomainArray:(NSArray *)noHijackDomainArray { } } +- (void) WGSetExpiredIPEnabled:(BOOL)enable { + [[MSDKDnsParamsManager shareInstance] msdkDnsSetExpiredIPEnabled:enable]; +} + +- (void) WGSetPersistCacheIPEnabled:(BOOL)enable { + [[MSDKDnsParamsManager shareInstance] msdkDnsSetPersistCacheIPEnabled:enable]; + [[MSDKDnsManager shareInstance] loadIPsFromPersistCacheAsync]; +} + #pragma mark - get host by name - (NSArray *) WGGetHostByName:(NSString *)domain { @@ -144,7 +153,13 @@ - (NSArray *) WGGetHostByName:(NSString *)domain { //进行httpdns请求 NSDate * date = [NSDate date]; //进行httpdns请求 - NSDictionary *res = [[MSDKDnsManager shareInstance] getHostsByNames:@[domain] verbose:NO]; + NSDictionary * res = @{}; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + res = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:@[domain] verbose:NO]; + } else { + res = [[MSDKDnsManager shareInstance] getHostsByNames:@[domain] verbose:NO]; + } dnsResult = [res objectForKey:domain]; NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; MSDKDNSLOG(@"MSDKDns WGGetHostByName Total Time Consume is %.1fms", time_consume); @@ -179,7 +194,12 @@ - (NSDictionary *) WGGetHostsByNames:(NSArray *)domains { //进行httpdns请求 NSDate * date = [NSDate date]; //进行httpdns请求 - dnsResult = [[MSDKDnsManager shareInstance] getHostsByNames:domains verbose:NO]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:domains verbose:NO]; + } else { + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNames:domains verbose:NO]; + } NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; MSDKDNSLOG(@"%@, MSDKDns Result is:%@",domains, dnsResult); MSDKDNSLOG(@"MSDKDns WGGetHostByName Total Time Consume is %.1fms", time_consume); @@ -208,7 +228,12 @@ - (NSDictionary *) WGGetAllHostsByNames:(NSArray *)domains { //进行httpdns请求 NSDate * date = [NSDate date]; //进行httpdns请求 - dnsResult = [[MSDKDnsManager shareInstance] getHostsByNames:domains verbose:YES]; + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:domains verbose:YES]; + } else { + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNames:domains verbose:YES]; + } NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; MSDKDNSLOG(@"%@, MSDKDns Result is:%@",domains, dnsResult); MSDKDNSLOG(@"MSDKDns WGGetHostByName Total Time Consume is %.1fms", time_consume); @@ -218,6 +243,14 @@ - (NSDictionary *) WGGetAllHostsByNames:(NSArray *)domains { - (void)WGGetHostByNameAsync:(NSString *)domain returnIps:(void (^)(NSArray *))handler { @synchronized(self) { + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + //开启了使用过期缓存功能,给出提示建议使用同步接口进行解析 + @throw [NSException exceptionWithName:@"MSDKDns wrong use of api" + reason:@"WGGetHostByNameAsync cannot be used when useExpiredIpEnable is set to true, it is recommended to switch to the WGGetHostByName" + userInfo:nil]; + return; + } MSDKDNSLOG(@"GetHostByNameAsync:%@",domain); if (!domain || domain.length == 0) { //请求域名为空,返回空 @@ -258,6 +291,14 @@ - (void)WGGetHostByNameAsync:(NSString *)domain returnIps:(void (^)(NSArray *))h - (void)WGGetHostsByNamesAsync:(NSArray *)domains returnIps:(void (^)(NSDictionary *))handler { @synchronized(self) { + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + //开启了使用过期缓存功能,给出提示建议使用同步接口进行解析 + @throw [NSException exceptionWithName:@"MSDKDns wrong use of api" + reason:@"WGGetHostsByNamesAsync cannot be used when useExpiredIpEnable is set to true, it is recommended to switch to the WGGetHostsByNames" + userInfo:nil]; + return; + } MSDKDNSLOG(@"GetHostByNameAsync:%@",domains); if (!domains || [domains count] == 0) { //请求域名为空,返回空 @@ -300,6 +341,14 @@ - (void)WGGetHostsByNamesAsync:(NSArray *)domains returnIps:(void (^)(NSDictiona - (void)WGGetAllHostsByNamesAsync:(NSArray *)domains returnIps:(void (^)(NSDictionary *))handler { @synchronized(self) { + BOOL expiredIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetExpiredIPEnabled]; + if (expiredIPEnabled) { + //开启了使用过期缓存功能,给出提示建议使用同步接口进行解析 + @throw [NSException exceptionWithName:@"MSDKDns wrong use of api" + reason:@"WGGetAllHostsByNamesAsync cannot be used when useExpiredIpEnable is set to true, it is recommended to switch to the WGGetAllHostsByNames" + userInfo:nil]; + return; + } MSDKDNSLOG(@"GetHostByNameAsync:%@",domains); if (!domains || [domains count] == 0) { //请求域名为空,返回空 @@ -340,6 +389,89 @@ - (void)WGGetAllHostsByNamesAsync:(NSArray *)domains returnIps:(void (^)(NSDicti } } +- (NSArray *) WGGetHostByNameEnableExpired:(NSString *)domain { + @synchronized(self) { + NSArray * dnsResult = @[@"0", @"0"]; + MSDKDNSLOG(@"GetHostByNameEnableExpired:%@",domain); + if (!domain || domain.length == 0) { + //请求域名为空,返回空 + MSDKDNSLOG(@"MSDKDns Result is Empty!"); + return dnsResult; + } + // 转换成小写 + domain = [domain lowercaseString]; + NSDate * date = [NSDate date]; + //进行httpdns请求 + NSDictionary *res = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:@[domain] verbose:NO]; + dnsResult = [res objectForKey:domain]; + NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; + MSDKDNSLOG(@"MSDKDns WGGetHostByNameEnableExpired Total Time Consume is %.1fms", time_consume); + NSMutableString * ipsStr = [NSMutableString stringWithString:@""]; + for (int i = 0; i < dnsResult.count; i++) { + NSString * ip = dnsResult[i]; + [ipsStr appendFormat:@"%@,",ip]; + } + MSDKDNSLOG(@"%@, MSDKDns Result is:%@",domain, ipsStr); + return dnsResult; + } +} + +- (NSDictionary *) WGGetHostsByNamesEnableExpired:(NSArray *)domains { + @synchronized(self) { + NSDictionary * dnsResult = @{}; + MSDKDNSLOG(@"GetHostsByNamesEnableExpired:%@",domains); + if (!domains || [domains count] == 0) { + //请求域名为空,返回空 + MSDKDNSLOG(@"MSDKDns Result is Empty!"); + return dnsResult; + } + // 转换成小写 + NSMutableArray *lowerCaseArray = [NSMutableArray array]; + for(int i = 0; i < [domains count]; i++) { + NSString *d = [domains objectAtIndex:i]; + if (d && d.length > 0) { + [lowerCaseArray addObject:[d lowercaseString]]; + } + } + domains = lowerCaseArray; + NSDate * date = [NSDate date]; + //进行httpdns请求 + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:domains verbose:NO]; + NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; + MSDKDNSLOG(@"%@, MSDKDns Result is:%@",domains, dnsResult); + MSDKDNSLOG(@"MSDKDns WGGetHostsByNamesEnableExpired Total Time Consume is %.1fms", time_consume); + return dnsResult; + } +} + +- (NSDictionary *) WGGetAllHostsByNamesEnableExpired:(NSArray *)domains { + @synchronized(self) { + NSDictionary * dnsResult = @{}; + MSDKDNSLOG(@"GetAllHostsByNamesEnableExpired:%@",domains); + if (!domains || [domains count] == 0) { + //请求域名为空,返回空 + MSDKDNSLOG(@"MSDKDns Result is Empty!"); + return dnsResult; + } + // 转换成小写 + NSMutableArray *lowerCaseArray = [NSMutableArray array]; + for(int i = 0; i < [domains count]; i++) { + NSString *d = [domains objectAtIndex:i]; + if (d && d.length > 0) { + [lowerCaseArray addObject:[d lowercaseString]]; + } + } + domains = lowerCaseArray; + NSDate * date = [NSDate date]; + //进行httpdns请求 + dnsResult = [[MSDKDnsManager shareInstance] getHostsByNamesEnableExpired:domains verbose:YES]; + NSTimeInterval time_consume = [[NSDate date] timeIntervalSinceDate:date] * 1000; + MSDKDNSLOG(@"%@, MSDKDns Result is:%@",domains, dnsResult); + MSDKDNSLOG(@"MSDKDns WGGetAllHostsByNamesEnableExpired Total Time Consume is %.1fms", time_consume); + return dnsResult; + } +} + - (NSDictionary *) WGGetDnsDetail:(NSString *) domain { return [[MSDKDnsManager shareInstance] getDnsDetail:domain]; } diff --git a/MSDKDns/MSDKDnsPrivate.h b/MSDKDns/MSDKDnsPrivate.h index 29329f2..94df37b 100644 --- a/MSDKDns/MSDKDnsPrivate.h +++ b/MSDKDns/MSDKDnsPrivate.h @@ -69,4 +69,18 @@ static NSString * const MSDKDns_Success = @"0"; // 成功 static NSString * const MSDKDnsEventHttpDnsfail = @"HttpDnsfail"; static NSString * const MSDKDnsEventHttpDnsSpend = @"HttpDnsSpend"; +//命中缓存的状态 +static NSString * const MSDKDnsDomainCacheHit = @"domainCacheHit"; //命中缓存 +static NSString * const MSDKDnsDomainCacheExpired = @"domainCacheExpired"; //缓存过期 +static NSString * const MSDKDnsDomainCacheEmpty = @"domainCacheEmpty"; //没有缓存 + +//本地DB存储字段 +static NSString * const DB_HttpDNS_IPV4_Channel = [kMSDKHttpDnsCache_A stringByAppendingString:kChannel]; +static NSString * const DB_HttpDNS_IPV4_ClientIP = [kMSDKHttpDnsCache_A stringByAppendingString:kClientIP]; +static NSString * const DB_HttpDNS_IPV4_TimeConsuming = [kMSDKHttpDnsCache_A stringByAppendingString:kDnsTimeConsuming]; +static NSString * const DB_HttpDNS_IPV4_TTL = [kMSDKHttpDnsCache_A stringByAppendingString:kTTL]; +static NSString * const DB_HttpDNS_IPV4_TTLExpired = [kMSDKHttpDnsCache_A stringByAppendingString:kTTLExpired]; + + + #endif diff --git a/MSDKDns/MSDKDnsService.m b/MSDKDns/MSDKDnsService.m index ad069e8..b6d313a 100644 --- a/MSDKDns/MSDKDnsService.m +++ b/MSDKDns/MSDKDnsService.m @@ -8,6 +8,7 @@ #import "MSDKDnsInfoTool.h" #import "MSDKDnsLog.h" #import "MSDKDnsManager.h" +#import "MSDKDnsDB.h" #import "MSDKDnsNetworkManager.h" #import "MSDKDnsParamsManager.h" #import "MSDKDnsTCPSpeedTester.h" @@ -539,6 +540,10 @@ - (void)cacheDomainInfo:(MSDKDnsResolver *)resolver { if (cacheDict && domain) { [[MSDKDnsManager shareInstance] cacheDomainInfo:cacheDict Domain:domain]; + BOOL persistCacheIPEnabled = [[MSDKDnsParamsManager shareInstance] msdkDnsGetPersistCacheIPEnabled]; + if (resolver && resolver != self.localDnsResolver && persistCacheIPEnabled){ + [[MSDKDnsDB shareInstance] insertOrReplaceDomainInfo:cacheDict Domain:domain]; + } } } } diff --git a/MSDKDns/WCDB/HTTPDNSORM+WCTTableCoding.h b/MSDKDns/WCDB/HTTPDNSORM+WCTTableCoding.h new file mode 100644 index 0000000..ce48274 --- /dev/null +++ b/MSDKDns/WCDB/HTTPDNSORM+WCTTableCoding.h @@ -0,0 +1,29 @@ +/** + * Copyright (c) Tencent. All rights reserved. + */ + +#if __cplusplus >= 201103L + #import + #import "HTTPDNSORM.h" + @interface HTTPDNSORM (WCTTableCoding) + + WCDB_PROPERTY(domain) + + WCDB_PROPERTY(httpDnsIPV4Channel) + WCDB_PROPERTY(httpDnsIPV4ClientIP) + WCDB_PROPERTY(httpDnsIPV4IPs) + WCDB_PROPERTY(httpDnsIPV4TimeConsuming) + WCDB_PROPERTY(httpDnsIPV4TTL) + WCDB_PROPERTY(httpDnsIPV4TTLExpried) + + WCDB_PROPERTY(httpDnsIPV6Channel) + WCDB_PROPERTY(httpDnsIPV6ClientIP) + WCDB_PROPERTY(httpDnsIPV6IPs) + WCDB_PROPERTY(httpDnsIPV6TimeConsuming) + WCDB_PROPERTY(httpDnsIPV6TTL) + WCDB_PROPERTY(httpDnsIPV6TTLExpried) + + @end + +#endif + diff --git a/MSDKDns/WCDB/HTTPDNSORM.h b/MSDKDns/WCDB/HTTPDNSORM.h new file mode 100644 index 0000000..27a811e --- /dev/null +++ b/MSDKDns/WCDB/HTTPDNSORM.h @@ -0,0 +1,26 @@ +/** + * Copyright (c) Tencent. All rights reserved. + */ + +#import + +@interface HTTPDNSORM : NSObject + +@property(retain) NSString *domain; + +@property(retain) NSString *httpDnsIPV4Channel; +@property(retain) NSString *httpDnsIPV4ClientIP; +@property(retain) NSArray *httpDnsIPV4IPs; +@property(retain) NSString *httpDnsIPV4TimeConsuming; +@property(retain) NSString *httpDnsIPV4TTL; +@property(retain) NSString *httpDnsIPV4TTLExpried; + +@property(retain) NSString *httpDnsIPV6Channel; +@property(retain) NSString *httpDnsIPV6ClientIP; +@property(retain) NSArray *httpDnsIPV6IPs; +@property(retain) NSString *httpDnsIPV6TimeConsuming; +@property(retain) NSString *httpDnsIPV6TTL; +@property(retain) NSString *httpDnsIPV6TTLExpried; + + +@end diff --git a/MSDKDns/WCDB/HTTPDNSORM.m b/MSDKDns/WCDB/HTTPDNSORM.m new file mode 100644 index 0000000..88935f4 --- /dev/null +++ b/MSDKDns/WCDB/HTTPDNSORM.m @@ -0,0 +1,39 @@ +/** + * Copyright (c) Tencent. All rights reserved. + */ + +#if __cplusplus >= 201103L + + #import + #import "HTTPDNSORM.h" + #import "HTTPDNSORM+WCTTableCoding.h" + + + @implementation HTTPDNSORM + + WCDB_IMPLEMENTATION(HTTPDNSORM) + + WCDB_SYNTHESIZE(HTTPDNSORM, domain) + + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4Channel) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4ClientIP) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4IPs) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4TimeConsuming) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4TTL) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV4TTLExpried) + + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6Channel) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6ClientIP) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6IPs) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6TimeConsuming) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6TTL) + WCDB_SYNTHESIZE(HTTPDNSORM, httpDnsIPV6TTLExpried) + + WCDB_PRIMARY(HTTPDNSORM, domain) + + WCDB_INDEX(HTTPDNSORM, "_index", domain) + + @end + +#endif + diff --git a/MSDKDns/WCDB/MSDKDnsDB.h b/MSDKDns/WCDB/MSDKDnsDB.h new file mode 100644 index 0000000..9086e20 --- /dev/null +++ b/MSDKDns/WCDB/MSDKDnsDB.h @@ -0,0 +1,20 @@ +// Created by eric hu on 2022/8/23. +// Copyright © 2022 Tencent. All rights reserved. +// + +#import + +@interface MSDKDnsDB : NSObject + +//@property (strong, nonatomic, readonly) WCTDatabase *database; + ++ (instancetype)shareInstance; + +- (void)insertOrReplaceDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain; + +- (NSDictionary *)getDataFromDB; + +- (void)deleteDBData: (NSArray *)domains; + +@end + diff --git a/MSDKDns/WCDB/MSDKDnsDB.m b/MSDKDns/WCDB/MSDKDnsDB.m new file mode 100644 index 0000000..7d5427b --- /dev/null +++ b/MSDKDns/WCDB/MSDKDnsDB.m @@ -0,0 +1,203 @@ +// Created by eric hu on 2022/8/23. +// Copyright © 2022 Tencent. All rights reserved. +// + +#import "MSDKDnsDB.h" +#import "MSDKDnsLog.h" +#import "MSDKDnsPrivate.h" + +#if __cplusplus >= 201103L + #import + #import "HTTPDNSORM.h" + #import "HTTPDNSORM+WCTTableCoding.h" +#endif + +@interface MSDKDnsDB () + +@property (strong, nonatomic, readwrite) id database; +@property (strong, nonatomic, readwrite) NSString *tableName; + +@end + +@implementation MSDKDnsDB + +#pragma mark - init + +static MSDKDnsDB * _sharedInstance = nil; ++ (instancetype)shareInstance { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _sharedInstance = [[MSDKDnsDB alloc] init]; + }); + return _sharedInstance; +} + +- (instancetype) init { + if (self = [super init]) { + _tableName = @"HttpDNSTable"; + // 获取WCTDatabase类 + Class databaseClass = NSClassFromString(@"WCTDatabase"); + // 获取HTTPDNSORM类 + Class HTTPDNSORMClass = NSClassFromString(@"HTTPDNSORM"); + if (databaseClass == 0x0) { + MSDKDNSLOG(@"WCTDatabase framework is not imported"); + } else if (HTTPDNSORMClass == 0x0) { + MSDKDNSLOG(@"MSDKDns does not support persistent cache, we recommend using MSDKDns_C11"); + } else { + @try { + NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); + NSString *baseDirectory = [documentPaths objectAtIndex:0]; + NSString *path = [baseDirectory stringByAppendingPathComponent:_tableName]; + + _database = [[databaseClass alloc] initWithPath:path]; + // 获取方法编号 + SEL createTableAndIndexesOfNameSEL = NSSelectorFromString(@"createTableAndIndexesOfName:withClass:"); + // 调用WCTDatabase类的方法 + if (_database && [_database respondsToSelector:createTableAndIndexesOfNameSEL] ) { + IMP imp = [_database methodForSelector:createTableAndIndexesOfNameSEL]; + using connectDatabase = BOOL (*)(id, SEL, NSString *, Class); + BOOL success = ((connectDatabase) imp)(_database, createTableAndIndexesOfNameSEL, _tableName, HTTPDNSORMClass.class); + if (!success) { + MSDKDNSLOG(@"database connection failed"); + } + } + } @catch (NSException *exception) { + MSDKDNSLOG(@"database connection failed"); + } + } + } + return self; +} + +- (void)insertOrReplaceDomainInfo:(NSDictionary *)domainInfo Domain:(NSString *)domain { + + NSDictionary * hresultDict_A = domainInfo[kMSDKHttpDnsCache_A]; + NSDictionary * hresultDict_4A = domainInfo[kMSDKHttpDnsCache_4A]; + + // 获取HTTPDNSORM类 + Class HTTPDNSORMClass = NSClassFromString(@"HTTPDNSORM"); + + if (HTTPDNSORMClass == 0x0) { + MSDKDNSLOG(@"MSDKDns does not support persistent cache, we recommend using MSDKDns_C11"); + return; + } + + id httpDnsData = [[HTTPDNSORMClass alloc] init]; + + [httpDnsData setValue:[domain copy] forKey:@"domain"]; + + if (hresultDict_A){ + [httpDnsData setValue:hresultDict_A[kChannel] forKey:@"httpDnsIPV4Channel"]; + [httpDnsData setValue:hresultDict_A[kClientIP] forKey:@"httpDnsIPV4ClientIP"]; + [httpDnsData setValue:hresultDict_A[kIP] forKey:@"httpDnsIPV4IPs"]; + [httpDnsData setValue:hresultDict_A[kDnsTimeConsuming] forKey:@"httpDnsIPV4TimeConsuming"]; + [httpDnsData setValue:hresultDict_A[kTTL] forKey:@"httpDnsIPV4TTL"]; + [httpDnsData setValue:hresultDict_A[kTTLExpired] forKey:@"httpDnsIPV4TTLExpried"]; + } + + if (hresultDict_4A) { + [httpDnsData setValue:hresultDict_4A[kChannel] forKey:@"httpDnsIPV6Channel"]; + [httpDnsData setValue:hresultDict_4A[kClientIP] forKey:@"httpDnsIPV6ClientIP"]; + [httpDnsData setValue:hresultDict_4A[kIP] forKey:@"httpDnsIPV6IPs"]; + [httpDnsData setValue:hresultDict_4A[kDnsTimeConsuming] forKey:@"httpDnsIPV6TimeConsuming"]; + [httpDnsData setValue:hresultDict_4A[kTTL] forKey:@"httpDnsIPV6TTL"]; + [httpDnsData setValue:hresultDict_4A[kTTLExpired] forKey:@"httpDnsIPV6TTLExpried"]; + } + // 获取方法编号 + SEL insertOrReplaceObjectSEL = NSSelectorFromString(@"insertOrReplaceObject:into:"); + + if (_database && [_database respondsToSelector:insertOrReplaceObjectSEL] ) { + @try { + IMP imp = [_database methodForSelector:insertOrReplaceObjectSEL]; + using insertData = BOOL (*)(id, SEL, id, NSString *); + BOOL success = ((insertData) imp)(_database, insertOrReplaceObjectSEL, httpDnsData, _tableName); + if (!success) { + MSDKDNSLOG(@"Failed to insert data into database"); + } + } @catch (NSException *exception) { + MSDKDNSLOG(@"Failed to insert data into database"); + } + } +} + +- (NSDictionary *)getDataFromDB { + // 获取HTTPDNSORM类 + Class HTTPDNSORMClass = NSClassFromString(@"HTTPDNSORM"); + SEL getAllObjectsOfClassSEL = NSSelectorFromString(@"getAllObjectsOfClass:fromTable:"); + + NSMutableDictionary *newResult = [[NSMutableDictionary alloc] init]; + + if (HTTPDNSORMClass == 0x0) { + MSDKDNSLOG(@"MSDKDns does not support persistent cache, we recommend using MSDKDns_C11"); + return newResult; + } + + if (_database && [_database respondsToSelector:getAllObjectsOfClassSEL]) { + @try { + IMP imp = [_database methodForSelector:getAllObjectsOfClassSEL]; + using GetAllData = NSArray* (*)(id, SEL, Class, NSString *); + NSArray *result = ((GetAllData) imp)(_database, getAllObjectsOfClassSEL, HTTPDNSORMClass.class, _tableName); + if (!result) { + MSDKDNSLOG(@"Failed to insert data into database"); + } + for (id item in result) { + NSMutableDictionary *domainInfo = [[NSMutableDictionary alloc] init]; + NSMutableDictionary *httpDnsIPV4Info = [[NSMutableDictionary alloc] init]; + NSMutableDictionary *httpDnsIPV6Info = [[NSMutableDictionary alloc] init]; + + @try { + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4Channel"] forKey:kChannel]; + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4ClientIP"] forKey:kClientIP]; + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4IPs"] forKey:kIP]; + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4TimeConsuming"] forKey:kDnsTimeConsuming]; + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4TTL"] forKey:kTTL]; + [httpDnsIPV4Info setObject:[item valueForKey:@"httpDnsIPV4TTLExpried"] forKey:kTTLExpired]; + + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6Channel"] forKey:kChannel]; + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6ClientIP"] forKey:kClientIP]; + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6IPs"] forKey:kIP]; + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6TimeConsuming"] forKey:kDnsTimeConsuming]; + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6TTL"] forKey:kTTL]; + [httpDnsIPV6Info setObject:[item valueForKey:@"httpDnsIPV6TTLExpried"] forKey:kTTLExpired]; + } @catch (NSException *exception) {} + [domainInfo setObject:httpDnsIPV4Info forKey:kMSDKHttpDnsCache_A]; + [domainInfo setObject:httpDnsIPV6Info forKey:kMSDKHttpDnsCache_4A]; + + [newResult setObject:domainInfo forKey:[item valueForKey:@"domain"]]; + } + } @catch (NSException *exception) { + MSDKDNSLOG(@"Failed to insert data into database"); + } + } + return newResult; +} + +- (void)deleteDBData: (NSArray *)domains { + // 获取HTTPDNSORM类 + Class HTTPDNSORMClass = NSClassFromString(@"HTTPDNSORM"); + SEL deleteObjectsFromTableSEL = NSSelectorFromString(@"deleteObjectsFromTable:where:"); + + if (HTTPDNSORMClass == 0x0) { + MSDKDNSLOG(@"MSDKDns does not support persistent cache, we recommend using MSDKDns_C11"); + return; + } + + if (_database && [_database respondsToSelector:deleteObjectsFromTableSEL]) { + @try { +#if __cplusplus >= 201103L + IMP imp = [_database methodForSelector:deleteObjectsFromTableSEL]; + using deleteData = BOOL (*)(id, SEL, NSString *, WCTExpr); + // 删除表数据 + BOOL success = ((deleteData) imp)(_database, deleteObjectsFromTableSEL, _tableName, HTTPDNSORM.domain.in(domains)); + if (!success) { + MSDKDNSLOG(@"Failed to delete data into database"); + } +#endif + } @catch (NSException *exception) { + MSDKDNSLOG(@"Failed to delete data into database"); + } + } +} + + +@end