QNDnspodFree.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // QNDnspodFree.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 "QNDnspodFree.h"
  9. #import "QNDomain.h"
  10. #import "QNIP.h"
  11. #import "QNRecord.h"
  12. @interface QNDnspodFree ()
  13. @property (readonly, nonatomic, strong) NSString *server;
  14. @property (readonly, nonatomic) NSUInteger timeout;
  15. @end
  16. @implementation QNDnspodFree
  17. - (instancetype)init {
  18. return [self initWithServer:@"119.29.29.29"];
  19. }
  20. - (instancetype)initWithServer:(NSString *)server {
  21. return [self initWithServer:@"119.29.29.29" timeout:QN_DNS_DEFAULT_TIMEOUT];
  22. }
  23. - (instancetype)initWithServer:(NSString *)server
  24. timeout:(NSUInteger)time {
  25. if (self = [super init]) {
  26. _server = server;
  27. _timeout = time;
  28. }
  29. return self;
  30. }
  31. - (NSArray *)query:(QNDomain *)domain networkInfo:(QNNetworkInfo *)netInfo error:(NSError *__autoreleasing *)error {
  32. NSString *url = [NSString stringWithFormat:@"http://%@/d?ttl=1&dn=%@", [QNIP ipHost:_server], domain.domain];
  33. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:_timeout];
  34. NSHTTPURLResponse *response = nil;
  35. NSError *httpError = nil;
  36. NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
  37. returningResponse:&response
  38. error:&httpError];
  39. if (httpError != nil) {
  40. if (error != nil) {
  41. *error = httpError;
  42. }
  43. return nil;
  44. }
  45. if (response.statusCode != 200) {
  46. return nil;
  47. }
  48. NSString *raw = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  49. NSArray *ip1 = [raw componentsSeparatedByString:@","];
  50. if (ip1.count != 2) {
  51. return nil;
  52. }
  53. NSString *ttlStr = [ip1 objectAtIndex:1];
  54. int ttl = [ttlStr intValue];
  55. if (ttl <= 0) {
  56. return nil;
  57. }
  58. NSString *ips = [ip1 objectAtIndex:0];
  59. NSArray *ipArray = [ips componentsSeparatedByString:@";"];
  60. NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity:ipArray.count];
  61. for (int i = 0; i < ipArray.count; i++) {
  62. QNRecord *record = [[QNRecord alloc] init:[ipArray objectAtIndex:i] ttl:ttl type:kQNTypeA];
  63. [ret addObject:record];
  64. }
  65. return ret;
  66. }
  67. @end