QNNiuDns.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // QNNiuDns.m
  3. // HappyDNS
  4. //
  5. // Created by 何昊宇 on 2018/3/8.
  6. // Copyright © 2018年 Qiniu Cloud Storage. All rights reserved.
  7. //
  8. #import "QNNiuDns.h"
  9. #import "QNDes.h"
  10. #import "QNDomain.h"
  11. #import "QNHex.h"
  12. #import "QNNetworkInfo.h"
  13. #import "QNRecord.h"
  14. #import "QNMD5.h"
  15. #define ENDPOINT_SSL @"https://httpdns.qnydns.net:18443/"
  16. #define ENDPOINT @"http://httpdns.qnydns.net:18302/"
  17. @interface QNNiuDns ()
  18. @property (nonatomic, strong) QNDes *des;
  19. @end
  20. @implementation QNNiuDns
  21. - (instancetype)initWithAccountId:(NSString *)accountId
  22. encryptKey:(NSString *)encryptKey
  23. expireTime:(long)expireTime
  24. isHttps:(BOOL)isHttps
  25. isNeedEncrypted:(BOOL)isNeedEncrypted {
  26. if (self = [super init]) {
  27. _accountId = accountId;
  28. _expireTime = expireTime;
  29. _isHttps = isHttps;
  30. _isNeedEncrypted = isNeedEncrypted;
  31. if (encryptKey) {
  32. _encryptKey = encryptKey;
  33. _des = [[QNDes alloc] init:[encryptKey dataUsingEncoding:NSUTF8StringEncoding]];
  34. }
  35. }
  36. return self;
  37. }
  38. - (NSString *)encrypt:(NSString *)domain {
  39. NSData *data = [_des encrypt:[domain dataUsingEncoding:NSUTF8StringEncoding]];
  40. if (data == nil) {
  41. return nil;
  42. }
  43. NSString *str = [QNHex encodeHexData:data];
  44. return str;
  45. }
  46. - (NSArray *)decrypt:(NSString *)raw {
  47. NSData *enc = [QNHex decodeHexString:raw];
  48. if (enc == nil) {
  49. return nil;
  50. }
  51. NSData *data = [_des decrpyt:enc];
  52. if (data == nil) {
  53. return nil;
  54. }
  55. return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  56. }
  57. - (NSArray *)query:(QNDomain *)domain networkInfo:(QNNetworkInfo *)netInfo error:(NSError *__autoreleasing *)error {
  58. NSString *realDomain = domain.domain;
  59. if (self.isNeedEncrypted) {
  60. realDomain = [self encrypt:[NSString stringWithFormat:@"%@", domain.domain]];
  61. if (realDomain == nil) {
  62. if (error != nil) {
  63. *error = [[NSError alloc] initWithDomain:domain.domain code:kQN_ENCRYPT_FAILED userInfo:nil];
  64. }
  65. return nil;
  66. }
  67. }
  68. NSString * md = [NSString stringWithFormat:@"%@-%@-%ld",domain.domain,self.encryptKey,self.expireTime];
  69. NSString *s = [QNMD5 MD5:md];
  70. NSString *url = [NSString stringWithFormat:@"%@%@/d?dn=%@&e=%@&s=%@&ttl=1&echo=1", self.isHttps? ENDPOINT_SSL : ENDPOINT, self.accountId, realDomain,[NSString stringWithFormat:@"%ld",self.expireTime],s];
  71. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:QN_DNS_DEFAULT_TIMEOUT];
  72. NSHTTPURLResponse *response = nil;
  73. NSError *httpError = nil;
  74. NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
  75. returningResponse:&response
  76. error:&httpError];
  77. if (httpError != nil) {
  78. if (error != nil) {
  79. *error = httpError;
  80. }
  81. return nil;
  82. }
  83. if (response.statusCode != 200) {
  84. return nil;
  85. }
  86. NSDictionary *raw = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  87. if (raw == nil) {
  88. if (error != nil) {
  89. *error = [[NSError alloc] initWithDomain:domain.domain code:kQN_DECRYPT_FAILED userInfo:nil];
  90. }
  91. return nil;
  92. }
  93. NSArray *rawArray;
  94. if (self.isNeedEncrypted) {
  95. rawArray = [self decrypt:raw[@"data"]][0];
  96. } else {
  97. rawArray = raw[@"data"][0];
  98. }
  99. if (rawArray.count <= 0) {
  100. return nil;
  101. }
  102. NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity:rawArray.count];
  103. for (int i = 0; i < rawArray.count; i++) {
  104. NSDictionary *dataDic = [rawArray objectAtIndex:i];
  105. if ([[dataDic objectForKey:@"TTL"] longValue] <= 0) {
  106. continue;
  107. }
  108. QNRecord *record = [[QNRecord alloc] init:[dataDic objectForKey:@"data"] ttl:[[dataDic objectForKey:@"TTL"] intValue] type:kQNTypeA];
  109. [ret addObject:record];
  110. }
  111. return ret;
  112. }
  113. @end