88
99#import " QCloudHttpDNS.h"
1010#import " QCloudHosts.h"
11-
11+ #import " QCloudLogger.h"
12+ #import " NSError+QCloudNetworking.h"
1213#pragma GCC diagnostic push
1314#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1415
@@ -49,29 +50,20 @@ - (instancetype) init
4950
5051}
5152
52- - (BOOL ) resolveDomain : (NSString *)domain error : (NSError * __autoreleasing *)error
53+ - (BOOL ) resolveDomain : (NSString *)domain error : (NSError **)error
5354{
54- #warning this host & port is not set
55- NSString * reqstr = [NSString stringWithFormat: @" http://**.**.**.**/?dn=%@ " , domain];
56- NSURL * url = [NSURL URLWithString: reqstr];
57- NSURLRequest * request = [NSURLRequest requestWithURL: url];
58- NSHTTPURLResponse * response;
59- NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: error];
60- if (response.statusCode != 200 ) {
61- return NO ;
55+ NSString *ip;
56+ if (self.delegate && [self .delegate respondsToSelector: @selector (resolveDomain: )]) {
57+ ip = [self .delegate resolveDomain: domain];
6258 }
63- if (error != NULL ) {
64- if (*error) {
65- return NO ;
66- }
59+ if (!ip ) {
60+ QCloudLogDebug ( @" Cannot resolve domain %@ " ,domain);
61+ *error = [ NSError qcloud_errorWithCode: QCloudNetworkErrorCodeCannotResloveDomain message: [ NSString stringWithFormat: @" 无法解析域名 %@ " ,domain]] ;
62+ return NO ;
6763 }
6864
69- NSString * str = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
70- NSArray * array = [str componentsSeparatedByString: @" " ];
71- for (NSString * ip in array) {
72- if (QCloudCheckIPVaild (ip)) {
73- [_hosts putDomain: domain ip: [ip stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" " ]]];
74- }
65+ if (QCloudCheckIPVaild (ip)) {
66+ [_hosts putDomain: domain ip: [ip stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" " ]]];
7567 }
7668 [[NSNotificationCenter defaultCenter ] postNotificationName: kQCloudHttpDNSCacheReady object: nil userInfo: @{
7769 kQCloudHttpDNSHost :domain
@@ -83,9 +75,9 @@ - (BOOL) resolveDomain:(NSString*)domain error:(NSError* __autoreleasing*)error
8375- (NSString *) queryIPForHost : (NSString *)host
8476{
8577 NSArray * hosts = [_hosts queryIPForDomain: host];
86- // always use the first one
78+ // always use the last(lastest) one
8779 if (hosts.count ) {
88- return hosts.firstObject ;
80+ return hosts.lastObject ;
8981 }
9082 return nil ;
9183}
@@ -94,17 +86,22 @@ - (NSMutableURLRequest*) resolveURLRequestIfCan:(NSMutableURLRequest*)request
9486 if (!request) {
9587 return request;
9688 }
97-
98- NSString * ip = [self queryIPForHost: request.URL.host];
89+ NSString *host = request.URL .host ;
90+ NSString * ip = [self queryIPForHost: host];
91+ // Give it second chance to reslove domain by itself
92+ if (!ip) {
93+ NSError * resolveError;
94+ [self resolveDomain: request.URL.host error: &resolveError];
95+ }
96+ ip = [self queryIPForHost: host];
9997 if (!ip) {
10098 return request;
10199 }
102-
103100 NSString * url = request.URL .absoluteString ;
104- NSRange range = [url rangeOfString: request.URL. host];
101+ NSRange range = [url rangeOfString: host];
105102 NSString * originHost = request.URL .host ;
106103 if (range.location != NSNotFound && range.length > 0 ) {
107- url = [url stringByReplacingOccurrencesOfString: request.URL. host withString: ip options: 0 range: range];
104+ url = [url stringByReplacingOccurrencesOfString: host withString: ip options: 0 range: range];
108105 NSMutableURLRequest * mReq = [request mutableCopy ];
109106 mReq.URL = [NSURL URLWithString: url];
110107 [mReq setValue: originHost forHTTPHeaderField: @" Host" ] ;
@@ -114,6 +111,11 @@ - (NSMutableURLRequest*) resolveURLRequestIfCan:(NSMutableURLRequest*)request
114111 }
115112}
116113
114+ - (void )setIp : (NSString *)ip forDomain : (NSString *)domain {
115+ if (QCloudCheckIPVaild (ip)) {
116+ [_hosts putDomain: domain ip: ip];
117+ }
118+ }
117119
118120- (BOOL ) isTrustIP : (NSString *)ip
119121{
0 commit comments