Skip to content

Commit

Permalink
Merge pull request #22 from HuYuee/feature/IPRank
Browse files Browse the repository at this point in the history
新增IP优选功能
  • Loading branch information
Vastness authored Aug 11, 2022
2 parents db74661 + 8beac8d commit 4c699c8
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 5 deletions.
18 changes: 18 additions & 0 deletions MSDKDns.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
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 */; };
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 */; };
5F5DAF5128A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */; };
5F5DAF5228A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */; };
5F5DAF5328A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */; };
C8EBE7D3256664C500BEFEEC /* aes.h in Headers */ = {isa = PBXBuildFile; fileRef = C8EBE7D1256664C400BEFEEC /* aes.h */; };
C8EBE7D4256664C500BEFEEC /* aes.mm in Sources */ = {isa = PBXBuildFile; fileRef = C8EBE7D2256664C500BEFEEC /* aes.mm */; };
C8EBE7F4256668A300BEFEEC /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = C8EBE7DA2566675800BEFEEC /* libc++.tbd */; };
Expand Down Expand Up @@ -104,6 +110,9 @@
54EA81F1275478D1005F68A9 /* httpdnsIps.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = httpdnsIps.h; sourceTree = "<group>"; };
54EA82202760890B005F68A9 /* AttaReport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AttaReport.h; sourceTree = "<group>"; };
54EA82212760890B005F68A9 /* AttaReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AttaReport.m; sourceTree = "<group>"; };
5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsTCPSpeedTester.h; sourceTree = "<group>"; };
5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSDKDnsTCPSpeedTester.m; sourceTree = "<group>"; };
5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsSpeedTester.h; sourceTree = "<group>"; };
C8EBE7D1256664C400BEFEEC /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = "<group>"; };
C8EBE7D2256664C500BEFEEC /* aes.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = aes.mm; sourceTree = "<group>"; };
C8EBE7DA2566675800BEFEEC /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/lib/libc++.tbd"; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -165,6 +174,9 @@
4497F8BC1B46306200D51391 /* MSDKDnsPrivate.h */,
504F54381ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.h */,
504F54391ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.m */,
5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */,
5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */,
5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */,
C8EBE7D1256664C400BEFEEC /* aes.h */,
C8EBE7D2256664C500BEFEEC /* aes.mm */,
54EA821F2760890B005F68A9 /* Reporter */,
Expand Down Expand Up @@ -263,8 +275,10 @@
4455D15E1B3A5B90005BF126 /* MSDKDns.h in Headers */,
448EE4E61B329899004A2131 /* LocalDnsResolver.h in Headers */,
DD5935581DDC56B200BF9348 /* HttpsDnsResolver.h in Headers */,
5F5DAF4E28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */,
448EE4E81B329899004A2131 /* MSDKDnsResolver.h in Headers */,
54EA82222760890B005F68A9 /* AttaReport.h in Headers */,
5F5DAF5228A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */,
44BFE2371CA58C3A00D7FE87 /* MSDKDnsInfoTool.h in Headers */,
502422EC2140073F0094403C /* MSDKDnsParamsManager.h in Headers */,
4497F8BD1B46306200D51391 /* MSDKDnsPrivate.h in Headers */,
Expand All @@ -286,8 +300,10 @@
DD43F4AB231CC36D0000A89F /* MSDKDns.h in Headers */,
DD43F4AC231CC36D0000A89F /* LocalDnsResolver.h in Headers */,
DD43F4AD231CC36D0000A89F /* HttpsDnsResolver.h in Headers */,
5F5DAF4F28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */,
DD43F4AE231CC36D0000A89F /* MSDKDnsResolver.h in Headers */,
54EA822427608A55005F68A9 /* AttaReport.h in Headers */,
5F5DAF5328A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */,
DD43F4AF231CC36D0000A89F /* MSDKDnsInfoTool.h in Headers */,
DD43F4B0231CC36D0000A89F /* MSDKDnsParamsManager.h in Headers */,
DD43F4B1231CC36D0000A89F /* MSDKDnsPrivate.h in Headers */,
Expand Down Expand Up @@ -483,6 +499,7 @@
DD5935561DDC56B200BF9348 /* HttpsDnsResolver.m in Sources */,
445B36651CBD095C00BD4345 /* MSDKDnsManager.m in Sources */,
444044FA1B3133A30010F5D5 /* MSDKDnsService.m in Sources */,
5F5DAF5028A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */,
501001EE215E1F1D003288A5 /* msdkdns_local_ip_stack.cpp in Sources */,
44BFE24A1CA59D9800D7FE87 /* MSDKDnsReachability.m in Sources */,
C8EBE7D4256664C500BEFEEC /* aes.mm in Sources */,
Expand All @@ -505,6 +522,7 @@
DD43F498231CC36D0000A89F /* HttpsDnsResolver.m in Sources */,
DD43F499231CC36D0000A89F /* MSDKDnsManager.m in Sources */,
DD43F49A231CC36D0000A89F /* MSDKDnsService.m in Sources */,
5F5DAF5128A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */,
2FBE52E925E77B000012A0DF /* aes.mm in Sources */,
DD43F49B231CC36D0000A89F /* msdkdns_local_ip_stack.cpp in Sources */,
DD43F49C231CC36D0000A89F /* MSDKDnsReachability.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains;
- (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType;
- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains;
- (void)msdkDnsSetIPRankData: (NSDictionary *)IPRankData;
- (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive;

- (NSString *) msdkDnsGetMDnsIp;
Expand All @@ -47,5 +48,6 @@
- (NSArray *)msdkDnsGetPreResolvedDomains;
- (HttpDnsAddressType)msdkDnsGetAddressType;
- (NSArray *)msdkDnsGetKeepAliveDomains;
- (NSDictionary *)msdkDnsGetIPRankData;
- (BOOL)msdkDnsGetEnableKeepDomainsAlive;
@end
11 changes: 11 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +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) NSDictionary* IPRankData;
@property (assign, nonatomic, readwrite) BOOL enableKeepDomainsAlive;

@end
Expand Down Expand Up @@ -166,6 +167,12 @@ - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains {
});
}

- (void)msdkDnsSetIPRankData: (NSDictionary *)IPRankData {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
self.IPRankData = [IPRankData copy];
});
}

- (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
self.enableKeepDomainsAlive = enableKeepDomainsAlive;
Expand Down Expand Up @@ -250,6 +257,10 @@ - (NSArray *)msdkDnsGetKeepAliveDomains {
return _keepAliveDomains;
}

- (NSDictionary *)msdkDnsGetIPRankData {
return _IPRankData;
}

- (BOOL)msdkDnsGetEnableKeepDomainsAlive {
return _enableKeepDomainsAlive;
}
Expand Down
5 changes: 5 additions & 0 deletions MSDKDns/MSDKDns.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ typedef struct DnsConfigStruct {
*/
- (void) WGSetKeepAliveDomains:(NSArray *)domains;

/**
* 启用IP优选,设置域名对应的端口号,对域名解析返回的IP列表进行IP探测,对返回的列表进行动态排序,以保证第一个IP是可用性最好的IP
*/
- (void) WGSetIPRankData:(NSDictionary *)IPRankData;

/**
* 设置开启和关闭缓存启停功能,默认开启
*/
Expand Down
6 changes: 6 additions & 0 deletions MSDKDns/MSDKDns.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ - (void) WGSetKeepAliveDomains:(NSArray *)domains {
}
}

- (void) WGSetIPRankData:(NSDictionary *)IPRankData {
if (IPRankData) {
[[MSDKDnsParamsManager shareInstance] msdkDnsSetIPRankData:IPRankData];
}
}

- (void) WGSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive {
[[MSDKDnsParamsManager shareInstance] msdkDnsSetEnableKeepDomainsAlive:enableKeepDomainsAlive];
}
Expand Down
131 changes: 126 additions & 5 deletions MSDKDns/MSDKDnsService.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "MSDKDnsManager.h"
#import "MSDKDnsNetworkManager.h"
#import "MSDKDnsParamsManager.h"
#import "MSDKDnsTCPSpeedTester.h"
#import "AttaReport.h"

@interface MSDKDnsService () <MSDKDnsResolverDelegate>
Expand Down Expand Up @@ -213,30 +214,144 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do
}
}];
}

if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_BOTH) {
NSDictionary *IPRankData = [[MSDKDnsParamsManager shareInstance] msdkDnsGetIPRankData];
if (IPRankData) {
[domainInfo enumerateKeysAndObjectsUsingBlock:^(id _Nonnull domain, id _Nonnull obj, BOOL * _Nonnull stop) {
if (!domain) {
return;
}
NSArray *allHost = [IPRankData allKeys];

if (!allHost || allHost.count == 0) {
return;
}
if (![allHost containsObject:domain]) {
return;
}
@try {
if(resolver == self.httpDnsResolver_BOTH){
NSDictionary *domainResult = domainInfo[domain];
if (domainResult) {
NSDictionary *ipv4Value = [domainResult objectForKey:@"ipv4"];
if (ipv4Value) {
NSArray *ips = [ipv4Value objectForKey:kIP];
if(ips){
[self aysncUpdateIPRankingWithResult:ips forHost:domain];
}
}
}
}else{
NSDictionary *domainResult = domainInfo[domain];
if (domainResult) {
NSArray *ips = [domainResult objectForKey:kIP];
if(ips){
[self aysncUpdateIPRankingWithResult:ips forHost:domain];
}
}
}
} @catch (NSException *exception) {}
}];
}
}
});

// 正常解析结果上报,上报解析耗时
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A || resolver == self.httpDnsResolver_BOTH) {
if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport] && [[AttaReport sharedInstance] shoulReportDnsSpend]) {
if ([[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableReport] && [[AttaReport sharedInstance] shoulReportDnsSpend]) {
NSDictionary *domainDic = [domainInfo objectForKey:[self.toCheckDomains firstObject]];
NSString* routeip = [[MSDKDnsParamsManager shareInstance] msdkDnsGetRouteIp];
if (!routeip) {
routeip = @"";
}
HttpsDnsResolver *httpResolver = (HttpsDnsResolver *)resolver;
NSString *timeConsuming = @"";
if (domainDic) {
if(resolver == self.httpDnsResolver_BOTH){
NSDictionary *ipv4CacheValue = [domainDic objectForKey:@"ipv4"];
NSDictionary *ipv6CacheValue = [domainDic objectForKey:@"ipv6"];
if (ipv4CacheValue) {
timeConsuming = [ipv4CacheValue objectForKey:kDnsTimeConsuming];
}
if (ipv6CacheValue) {
timeConsuming = [ipv6CacheValue objectForKey:kDnsTimeConsuming];
}
}else{
timeConsuming = [domainDic objectForKey:kDnsTimeConsuming];
}
}
NSString *req_type = @"a";
if (resolver == self.httpDnsResolver_4A) {
req_type = @"aaaa";
}else if (resolver == self.httpDnsResolver_BOTH) {
req_type = @"addrs";
}
[[AttaReport sharedInstance] reportEvent:@{
@"eventName": MSDKDnsEventHttpDnsSpend,
@"dnsIp": [[MSDKDnsManager shareInstance] currentDnsServer],
@"req_dn": [self.toCheckDomains componentsJoinedByString:@","],
@"req_type": resolver == self.httpDnsResolver_4A ? @"aaaa" : @"a",
@"req_type": req_type,
@"req_timeout": @(self.timeOut * 1000),
@"req_ttl": @1,
@"req_query": @1,
@"req_ip": routeip,
@"spend": [domainDic objectForKey:kDnsTimeConsuming],
@"spend": timeConsuming,
@"statusCode": @(httpResolver.statusCode),
}];
}
}
}
}

- (void)aysncUpdateIPRankingWithResult:(NSArray *)IPStrings forHost:(NSString *)host {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[self syncUpdateIPRankingWithResult:IPStrings forHost:host];
});
}

- (void)syncUpdateIPRankingWithResult:(NSArray *)IPStrings forHost:(NSString *)host {
NSArray *sortedIps = [[MSDKDnsTCPSpeedTester new] ipRankingWithIPs:IPStrings host:host];
[self updateHostManagerDictWithIPs:sortedIps host:host];
}

- (void)updateHostManagerDictWithIPs:(NSArray *)IPs host:(NSString *)host {
if(!IPs){
return;
}
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
NSDictionary * tempDict = [[[MSDKDnsManager shareInstance] domainDict] objectForKey:host];
NSMutableDictionary *cacheDict;


if (tempDict) {
cacheDict = [NSMutableDictionary dictionaryWithDictionary:tempDict];

if (self.httpDnsResolver_A && self.httpDnsResolver_A.domainInfo) {

NSDictionary *cacheValue = [self.httpDnsResolver_A.domainInfo objectForKey:host];
if (cacheValue) {
NSMutableDictionary *newCacheValue = [NSMutableDictionary dictionaryWithDictionary:cacheValue];
[newCacheValue setValue:IPs forKey:kIP];
[cacheDict setObject:newCacheValue forKey:kMSDKHttpDnsCache_A];
}

} else if (self.httpDnsResolver_BOTH && self.httpDnsResolver_BOTH.domainInfo) {
NSDictionary *cacheValue = [self.httpDnsResolver_BOTH.domainInfo objectForKey:host];
if (cacheValue) {
NSDictionary *ipv4CacheValue = [cacheValue objectForKey:@"ipv4"];
if (ipv4CacheValue) {
NSMutableDictionary *newCacheValue = [NSMutableDictionary dictionaryWithDictionary:ipv4CacheValue];
[newCacheValue setValue:IPs forKey:kIP];
[cacheDict setObject:newCacheValue forKey:kMSDKHttpDnsCache_A];
}
}
}

if (cacheDict && host) {
[[MSDKDnsManager shareInstance] cacheDomainInfo:cacheDict Domain:host];
}
}
});
}

- (void)resolver:(MSDKDnsResolver *)resolver getDomainError:(NSString *)error retry:(BOOL)retry {
Expand Down Expand Up @@ -282,11 +397,17 @@ - (void) retryHttpDns:(MSDKDnsResolver *)resolver {
routeip = @"";
}
HttpsDnsResolver *httpResolver = (HttpsDnsResolver *)resolver;
NSString *req_type = @"a";
if (resolver == self.httpDnsResolver_4A) {
req_type = @"aaaa";
}else if (resolver == self.httpDnsResolver_BOTH) {
req_type = @"addrs";
}
[[AttaReport sharedInstance] reportEvent:@{
@"eventName": MSDKDnsEventHttpDnsfail,
@"dnsIp": [[MSDKDnsManager shareInstance] currentDnsServer],
@"req_dn": [self.toCheckDomains componentsJoinedByString:@","],
@"req_type": resolver == self.httpDnsResolver_4A ? @"aaaa" : @"a",
@"req_type": req_type,
@"req_timeout": @(self.timeOut * 1000),
@"req_ttl": @1,
@"req_query": @1,
Expand Down
11 changes: 11 additions & 0 deletions MSDKDns/MSDKDnsSpeedTester.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Tencent. All rights reserved.
*/

@protocol MSDKDnsSpeedTester <NSObject>

- (float)testSpeedOf:(NSString *)ip port:(int16_t)port;
- (NSArray<NSString *> *)ipRankingWithIPs:(NSArray<NSString *> *)IPs host:(NSString *)host;

@end

13 changes: 13 additions & 0 deletions MSDKDns/MSDKDnsTCPSpeedTester.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Tencent. All rights reserved.
*/

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

#define MSDKDns_SOCKET_CONNECT_TIMEOUT 10 //单位秒
#define MSDKDns_SOCKET_CONNECT_TIMEOUT_RTT 600000//10分钟 单位毫秒

@interface MSDKDnsTCPSpeedTester : NSObject <MSDKDnsSpeedTester>

@end
Loading

0 comments on commit 4c699c8

Please sign in to comment.