QNDnsManager.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // QNDnsManager.m
  3. // HappyDNS
  4. //
  5. // Created by bailong on 15/6/23.
  6. // Copyright (c) 2015年 Qiniu Cloud Storage. All rights reserved.
  7. //
  8. #import "QNDnsManager.h"
  9. #import "QNDomain.h"
  10. #import "QNHosts.h"
  11. #import "QNIP.h"
  12. #import "QNLruCache.h"
  13. #import "QNNetworkInfo.h"
  14. #import "QNRecord.h"
  15. #import "QNResolverDelegate.h"
  16. #include "QNGetAddrInfo.h"
  17. const int kQNDomainHijackingCode = -7001;
  18. const int kQNDomainNotOwnCode = -7002;
  19. const int kQNDomainSeverError = -7003;
  20. @interface QNDnsManager ()
  21. @property (nonatomic, strong) QNLruCache *cache;
  22. @property (atomic) QNNetworkInfo *curNetwork;
  23. @property (nonatomic) NSArray *resolvers;
  24. @property (atomic) UInt32 resolverStatus;
  25. @property (nonatomic) QNHosts *hosts;
  26. @property (nonatomic, strong) id<QNIpSorter> sorter;
  27. @end
  28. //static inline BOOL bits_isSet(UInt32 v, int index) {
  29. // return (v & (1 << index)) != 0;
  30. //}
  31. static inline UInt32 bits_set(UInt32 v, int bitIndex) {
  32. return v |= (1 << bitIndex);
  33. }
  34. static inline UInt32 bits_leadingZeros(UInt32 x) {
  35. UInt32 y;
  36. int n = 32;
  37. y = x >> 16;
  38. if (y != 0) {
  39. n = n - 16;
  40. x = y;
  41. }
  42. y = x >> 8;
  43. if (y != 0) {
  44. n = n - 8;
  45. x = y;
  46. }
  47. y = x >> 4;
  48. if (y != 0) {
  49. n = n - 4;
  50. x = y;
  51. }
  52. y = x >> 2;
  53. if (y != 0) {
  54. n = n - 2;
  55. x = y;
  56. }
  57. y = x >> 1;
  58. if (y != 0) {
  59. return n - 2;
  60. }
  61. return n - x;
  62. }
  63. static NSMutableArray *trimCname(NSArray *records) {
  64. NSMutableArray *array = [[NSMutableArray alloc] init];
  65. for (QNRecord *r in records) {
  66. if (r.type == kQNTypeA || r.type == kQNTypeAAAA) {
  67. [array addObject:r];
  68. }
  69. }
  70. return array;
  71. }
  72. static NSArray *records2Ips(NSArray *records) {
  73. NSMutableArray *array = [[NSMutableArray alloc] init];
  74. for (QNRecord *r in records) {
  75. [array addObject:r.value];
  76. }
  77. return array;
  78. }
  79. @interface DummySorter : NSObject <QNIpSorter>
  80. @end
  81. @implementation DummySorter
  82. //sorted already
  83. - (NSArray *)sort:(NSArray *)ips {
  84. return ips;
  85. }
  86. @end
  87. @implementation QNDnsManager
  88. - (NSArray *)query:(NSString *)domain {
  89. return [self queryWithDomain:[[QNDomain alloc] init:domain]];
  90. }
  91. - (NSArray *)queryWithDomain:(QNDomain *)domain {
  92. if (domain.domain == nil) {
  93. return nil;
  94. }
  95. if ([QNIP mayBeIpV4:domain.domain]) {
  96. return [NSArray arrayWithObject:domain.domain];
  97. }
  98. NSArray *ips = [self queryInternalWithDomain:domain];
  99. return [_sorter sort:ips];
  100. }
  101. - (NSArray *)queryInternalWithDomain:(QNDomain *)domain {
  102. if (domain.hostsFirst) {
  103. NSArray *ret = [_hosts query:domain networkInfo:_curNetwork];
  104. if (ret != nil && ret.count != 0) {
  105. return ret;
  106. }
  107. }
  108. NSMutableArray *result;
  109. if ([_curNetwork isEqualToInfo:[QNNetworkInfo normal]] && [QNNetworkInfo isNetworkChanged]) {
  110. @synchronized(_cache) {
  111. [_cache removeAllObjects];
  112. }
  113. _resolverStatus = 0;
  114. } else {
  115. @synchronized(_cache) {
  116. result = [_cache objectForKey:domain.domain];
  117. if (result != nil && result.count > 1) {
  118. QNRecord *first = [result firstObject];
  119. [result removeObjectAtIndex:0];
  120. [result addObject:first];
  121. }
  122. }
  123. }
  124. @synchronized(_cache) {
  125. if (result != nil && result.count > 0) {
  126. QNRecord *record = [result objectAtIndex:0];
  127. if (![record expired:[[NSDate date] timeIntervalSince1970]]) {
  128. return records2Ips(result);
  129. }
  130. }
  131. }
  132. NSArray *records = nil;
  133. NSError *error = nil;
  134. int firstOk = 32 - bits_leadingZeros(_resolverStatus);
  135. for (int i = 0; i < _resolvers.count; i++) {
  136. int pos = (firstOk + i) % _resolvers.count;
  137. id<QNResolverDelegate> resolver = [_resolvers objectAtIndex:pos];
  138. QNNetworkInfo *previousNetwork = _curNetwork;
  139. NSString *previousIp = [QNNetworkInfo getIp];
  140. records = [resolver query:domain networkInfo:previousNetwork error:&error];
  141. if (error != nil) {
  142. NSError *tmp = error;
  143. error = nil;
  144. if (tmp.code == kQNDomainNotOwnCode) {
  145. continue;
  146. }
  147. }
  148. if (records == nil || records.count == 0) {
  149. if (_curNetwork == previousNetwork && [previousIp isEqualToString:[QNNetworkInfo getIp]]) {
  150. _resolverStatus = bits_set(_resolverStatus, pos);
  151. }
  152. } else {
  153. NSMutableArray *ret = trimCname(records);
  154. if (_curNetwork == previousNetwork && [previousIp isEqualToString:[QNNetworkInfo getIp]]) {
  155. @synchronized(_cache) {
  156. [_cache setObject:ret forKey:domain.domain];
  157. }
  158. }
  159. return records2Ips(ret);
  160. }
  161. }
  162. if (!domain.hostsFirst) {
  163. return [_hosts query:domain networkInfo:_curNetwork];
  164. }
  165. return nil;
  166. }
  167. - (instancetype)init:(NSArray *)resolvers networkInfo:(QNNetworkInfo *)netInfo {
  168. return [self init:resolvers networkInfo:netInfo sorter:nil];
  169. }
  170. - (instancetype)init:(NSArray *)resolvers networkInfo:(QNNetworkInfo *)netInfo sorter:(id<QNIpSorter>)sorter {
  171. if (self = [super init]) {
  172. _cache = [[QNLruCache alloc] init:1024];
  173. _curNetwork = netInfo;
  174. _resolvers = [[NSArray alloc] initWithArray:resolvers];
  175. _hosts = [[QNHosts alloc] init];
  176. if (sorter == nil) {
  177. _sorter = [[DummySorter alloc] init];
  178. } else {
  179. _sorter = sorter;
  180. }
  181. }
  182. return self;
  183. }
  184. - (void)onNetworkChange:(QNNetworkInfo *)netInfo {
  185. @synchronized(_cache) {
  186. [_cache removeAllObjects];
  187. }
  188. _curNetwork = netInfo;
  189. }
  190. - (instancetype)putHosts:(NSString *)domain ip:(NSString *)ip {
  191. [_hosts put:domain ip:ip];
  192. return self;
  193. }
  194. - (instancetype)putHosts:(NSString *)domain ip:(NSString *)ip provider:(int)provider {
  195. [_hosts put:domain ip:ip provider:provider];
  196. return self;
  197. }
  198. - (NSURL *)queryAndReplaceWithIP:(NSURL *)url {
  199. NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:YES];
  200. if (!urlComponents) {
  201. return nil;
  202. }
  203. NSString *host = urlComponents.host;
  204. NSArray *ips = [self query:host];
  205. NSURL *URL = nil;
  206. if (ips && ips.count > 0) {
  207. urlComponents.host = [QNIP ipHost:ips[0]];
  208. }
  209. URL = urlComponents.URL;
  210. return URL;
  211. }
  212. static QNGetAddrInfoCallback getAddrInfoCallback = nil;
  213. static qn_ips_ret *dns_callback_internal(const char *host) {
  214. if (getAddrInfoCallback == nil) {
  215. return NULL;
  216. }
  217. NSString *s = [[NSString alloc] initWithUTF8String:host];
  218. if (s == nil) {
  219. return NULL;
  220. }
  221. NSArray *ips = getAddrInfoCallback(s);
  222. if (ips == nil) {
  223. return NULL;
  224. }
  225. qn_ips_ret *ret = calloc(sizeof(char *), ips.count + 1);
  226. for (int i = 0; i < ips.count; i++) {
  227. NSString *ip = ips[i];
  228. char *ip2 = strdup([ip cStringUsingEncoding:NSUTF8StringEncoding]);
  229. ret->ips[i] = ip2;
  230. }
  231. return ret;
  232. }
  233. static qn_ips_ret *dns_callback(const char *host) {
  234. qn_ips_ret *ret = dns_callback_internal(host);
  235. if (ret == NULL) {
  236. //only for compatible
  237. qn_ips_ret *ret = calloc(sizeof(char *), 2);
  238. ret->ips[0] = strdup(host);
  239. }
  240. return ret;
  241. }
  242. static QNIpStatusCallback ipStatusCallback = nil;
  243. static void ip_status_callback(const char *ip, int code, int time_ms) {
  244. if (ipStatusCallback == nil) {
  245. return;
  246. }
  247. NSString *s = [[NSString alloc] initWithUTF8String:ip];
  248. if (s == nil) {
  249. return;
  250. }
  251. ipStatusCallback(s, code, time_ms);
  252. }
  253. + (void)setGetAddrInfoBlock:(QNGetAddrInfoCallback)block {
  254. if ([QNIP isIpV6FullySupported] || ![QNIP isV6]) {
  255. getAddrInfoCallback = block;
  256. qn_set_dns_callback(dns_callback);
  257. }
  258. }
  259. + (void)setDnsManagerForGetAddrInfo:(QNDnsManager *)dns {
  260. [QNDnsManager setGetAddrInfoBlock:^NSArray *(NSString *host) {
  261. return [dns query:host];
  262. }];
  263. }
  264. + (void)setIpStatusCallback:(QNIpStatusCallback)block {
  265. ipStatusCallback = block;
  266. qn_set_ip_report_callback(ip_status_callback);
  267. }
  268. + (BOOL)needHttpDns {
  269. NSTimeZone *timeZone = [NSTimeZone localTimeZone];
  270. NSString *tzName = [timeZone name];
  271. return [tzName isEqual:@"Asia/Shanghai"] || [tzName isEqual:@"Asia/Chongqing"] || [tzName isEqual:@"Asia/Harbin"] || [tzName isEqual:@"Asia/Urumqi"];
  272. }
  273. @end