QNDnspodEnterprise.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "QNDnspodEnterprise.h"
  9. #import <CommonCrypto/CommonCryptor.h>
  10. #import "QNDes.h"
  11. #import "QNDomain.h"
  12. #import "QNHex.h"
  13. #import "QNIP.h"
  14. #import "QNRecord.h"
  15. const int kQN_ENCRYPT_FAILED = -10001;
  16. const int kQN_DECRYPT_FAILED = -10002;
  17. @interface QNDnspodEnterprise ()
  18. @property (readonly, strong) NSString *server;
  19. @property (nonatomic, strong) NSString *userId;
  20. @property (nonatomic, strong) QNDes *des;
  21. @property (nonatomic) NSUInteger timeout;
  22. @end
  23. @implementation QNDnspodEnterprise
  24. - (instancetype)initWithId:(NSString *)userId
  25. key:(NSString *)key {
  26. return [self initWithId:userId key:key server:@"119.29.29.29"];
  27. }
  28. - (instancetype)initWithId:(NSString *)userId
  29. key:(NSString *)key
  30. server:(NSString *)server {
  31. return [self initWithId:userId key:key server:@"119.29.29.29" timeout:QN_DNS_DEFAULT_TIMEOUT];
  32. }
  33. - (instancetype)initWithId:(NSString *)userId
  34. key:(NSString *)key
  35. server:(NSString *)server
  36. timeout:(NSUInteger)time {
  37. if (self = [super init]) {
  38. _server = server;
  39. _userId = userId;
  40. _des = [[QNDes alloc] init:[key dataUsingEncoding:NSUTF8StringEncoding]];
  41. _timeout = time;
  42. }
  43. return self;
  44. }
  45. - (NSString *)encrypt:(NSString *)domain {
  46. NSData *data = [_des encrypt:[domain dataUsingEncoding:NSUTF8StringEncoding]];
  47. if (data == nil) {
  48. return nil;
  49. }
  50. NSString *str = [QNHex encodeHexData:data];
  51. return str;
  52. }
  53. - (NSString *)decrypt:(NSData *)raw {
  54. NSData *enc = [QNHex decodeHexString:[[NSString alloc] initWithData:raw
  55. encoding:NSUTF8StringEncoding]];
  56. if (enc == nil) {
  57. return nil;
  58. }
  59. NSData *data = [_des decrpyt:enc];
  60. if (data == nil) {
  61. return nil;
  62. }
  63. return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  64. }
  65. - (NSArray *)query:(QNDomain *)domain networkInfo:(QNNetworkInfo *)netInfo error:(NSError *__autoreleasing *)error {
  66. NSString *encrypt = [self encrypt:domain.domain];
  67. if (encrypt == nil) {
  68. if (error != nil) {
  69. *error = [[NSError alloc] initWithDomain:domain.domain code:kQN_ENCRYPT_FAILED userInfo:nil];
  70. }
  71. return nil;
  72. }
  73. NSString *url = [NSString stringWithFormat:@"http://%@/d?ttl=1&dn=%@&id=%@", [QNIP ipHost:_server], encrypt, _userId];
  74. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:_timeout];
  75. NSHTTPURLResponse *response = nil;
  76. NSError *httpError = nil;
  77. NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
  78. returningResponse:&response
  79. error:&httpError];
  80. if (httpError != nil) {
  81. if (error != nil) {
  82. *error = httpError;
  83. }
  84. return nil;
  85. }
  86. if (response.statusCode != 200) {
  87. return nil;
  88. }
  89. NSString *raw = [self decrypt:data];
  90. if (raw == nil) {
  91. if (error != nil) {
  92. *error = [[NSError alloc] initWithDomain:domain.domain code:kQN_DECRYPT_FAILED userInfo:nil];
  93. }
  94. return nil;
  95. }
  96. NSArray *ip1 = [raw componentsSeparatedByString:@","];
  97. if (ip1.count != 2) {
  98. return nil;
  99. }
  100. NSString *ttlStr = [ip1 objectAtIndex:1];
  101. int ttl = [ttlStr intValue];
  102. if (ttl <= 0) {
  103. return nil;
  104. }
  105. NSString *ips = [ip1 objectAtIndex:0];
  106. NSArray *ipArray = [ips componentsSeparatedByString:@";"];
  107. NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity:ipArray.count];
  108. for (int i = 0; i < ipArray.count; i++) {
  109. QNRecord *record = [[QNRecord alloc] init:[ipArray objectAtIndex:i] ttl:ttl type:kQNTypeA];
  110. [ret addObject:record];
  111. }
  112. return ret;
  113. }
  114. @end