QNZoneInfo.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // QNZoneInfo.m
  3. // QiniuSDK
  4. //
  5. // Created by yangsen on 2020/4/16.
  6. // Copyright © 2020 Qiniu. All rights reserved.
  7. //
  8. #import "QNZoneInfo.h"
  9. NSString * const QNZoneInfoSDKDefaultIOHost = @"sdkDefaultIOHost";
  10. NSString * const QNZoneInfoEmptyRegionId = @"sdkEmptyRegionId";
  11. @interface QNZoneInfo()
  12. @property(nonatomic, strong) NSDate *buildDate;
  13. @property(nonatomic, assign) long ttl;
  14. @property(nonatomic, assign)BOOL http3Enabled;
  15. @property(nonatomic, strong)NSArray<NSString *> *domains;
  16. @property(nonatomic, strong)NSArray<NSString *> *old_domains;
  17. @property(nonatomic, strong)NSArray <NSString *> *allHosts;
  18. @property(nonatomic, strong) NSDictionary *detailInfo;
  19. @end
  20. @implementation QNZoneInfo
  21. + (QNZoneInfo *)zoneInfoWithMainHosts:(NSArray <NSString *> *)mainHosts
  22. regionId:(NSString * _Nullable)regionId{
  23. return [self zoneInfoWithMainHosts:mainHosts oldHosts:nil regionId:regionId];
  24. }
  25. + (QNZoneInfo *)zoneInfoWithMainHosts:(NSArray <NSString *> *)mainHosts
  26. oldHosts:(NSArray <NSString *> * _Nullable)oldHosts
  27. regionId:(NSString * _Nullable)regionId{
  28. if (!mainHosts || ![mainHosts isKindOfClass:[NSArray class]] || mainHosts.count == 0) {
  29. return nil;
  30. }
  31. if (mainHosts && ![mainHosts isKindOfClass:[NSArray class]]) {
  32. mainHosts = nil;
  33. }
  34. QNZoneInfo *zoneInfo = [QNZoneInfo zoneInfoFromDictionary:@{@"ttl" : @(86400*1000),
  35. @"region" : regionId ?: QNZoneInfoEmptyRegionId,
  36. @"up" : @{@"domains" : mainHosts ?: @[],
  37. @"old" : oldHosts ?: @[]},
  38. }];
  39. return zoneInfo;
  40. }
  41. + (QNZoneInfo *)zoneInfoFromDictionary:(NSDictionary *)detailInfo {
  42. if (![detailInfo isKindOfClass:[NSDictionary class]]) {
  43. return nil;
  44. }
  45. NSString *regionId = [detailInfo objectForKey:@"region"];
  46. if (regionId == nil) {
  47. regionId = QNZoneInfoEmptyRegionId;
  48. }
  49. long ttl = [[detailInfo objectForKey:@"ttl"] longValue];
  50. BOOL http3Enabled = false;
  51. if ([detailInfo[@"features"] isKindOfClass:[NSDictionary class]] &&
  52. [detailInfo[@"features"][@"http3"] isKindOfClass:[NSDictionary class]]) {
  53. http3Enabled = [detailInfo[@"features"][@"http3"][@"enabled"] boolValue];
  54. }
  55. NSDictionary *up = [detailInfo objectForKey:@"up"];
  56. NSArray *domains = [up objectForKey:@"domains"];
  57. NSArray *old_domains = [up objectForKey:@"old"];
  58. NSMutableArray *allHosts = [NSMutableArray array];
  59. QNZoneInfo *zoneInfo = [[QNZoneInfo alloc] init:ttl regionId:regionId];
  60. zoneInfo.http3Enabled = http3Enabled;
  61. if ([domains isKindOfClass:[NSArray class]]) {
  62. zoneInfo.domains = domains;
  63. [allHosts addObjectsFromArray:domains];
  64. }
  65. if ([old_domains isKindOfClass:[NSArray class]]) {
  66. zoneInfo.old_domains = old_domains;
  67. [allHosts addObjectsFromArray:old_domains];
  68. }
  69. zoneInfo.allHosts = [allHosts copy];
  70. zoneInfo.detailInfo = detailInfo;
  71. return zoneInfo;
  72. }
  73. - (instancetype)init:(long)ttl
  74. regionId:(NSString *)regionId {
  75. if (self = [super init]) {
  76. _ttl = ttl;
  77. _buildDate = [NSDate date];
  78. _regionId = regionId;
  79. }
  80. return self;
  81. }
  82. - (BOOL)isValid{
  83. NSDate *currentDate = [NSDate date];
  84. return self.ttl > [currentDate timeIntervalSinceDate:self.buildDate];
  85. }
  86. @end
  87. @interface QNZonesInfo()
  88. @property (nonatomic, strong) NSArray<QNZoneInfo *> *zonesInfo;
  89. @end
  90. @implementation QNZonesInfo
  91. - (instancetype)initWithZonesInfo:(NSArray<QNZoneInfo *> *)zonesInfo{
  92. self = [super init];
  93. if (self) {
  94. _zonesInfo = zonesInfo;
  95. }
  96. return self;
  97. }
  98. + (instancetype)infoWithDictionary:(NSDictionary *)dictionary {
  99. NSArray *hosts = dictionary[@"hosts"];
  100. NSMutableArray *zonesInfo = [NSMutableArray array];
  101. if ([hosts isKindOfClass:[NSArray class]]) {
  102. for (NSInteger i = 0; i < hosts.count; i++) {
  103. QNZoneInfo *zoneInfo = [QNZoneInfo zoneInfoFromDictionary:hosts[i]];
  104. if (zoneInfo && [zoneInfo isValid]) {
  105. [zonesInfo addObject:zoneInfo];
  106. }
  107. }
  108. }
  109. return [[[self class] alloc] initWithZonesInfo:zonesInfo];
  110. }
  111. - (BOOL)isValid {
  112. return [self.zonesInfo count] > 0;
  113. }
  114. @end