react-native ios 集成httpdns

react-native 项目 iOS端集成httpdns

  • 官方文档 https://help.aliyun.com/document_detail/30143.html
  • Demo地址 https://github.com/aliyun/alicloud-ios-demo/tree/master/httpdns_ios_demo

集成步骤

  • 手动集成
    • 下载SDK,将组建包拖入项目中
      AlicloudHttpDNS.framework
      AlicloudUtils.framework
      UTDID.framework
    • 添加系统库
      libresolv.tbd
      CoreTelephony.framework
      SystemConfiguration.framework
  • Pod集成
    • 指定Master仓库和阿里云仓库:
      source 'https://github.com/CocoaPods/Specs.git'
      source 'https://github.com/aliyun/aliyun-specs.git'
    • 添加依赖:
      pod 'AlicloudHTTPDNS', '~> 1.5.2'
  • 修改编译选项
    • Target->Build Setting->Linking->Other Linker Flags中添加 -ObjC 选项。

原生入口基本配置

  • AppDelegate 入口处添加如下配置代码
/** httpdns */
  // 初始化HTTPDNS
  // 设置AccoutID
  HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:xxxxx];
  //鉴权方式初始化
  //HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:0000 secretKey:@"XXXX"];
  
  // 为HTTPDNS服务设置降级机制
//  [httpdns setDelegateForDegradationFilter:self];
  // 允许返回过期的IP
  [httpdns setExpiredIPEnabled:YES];
  // 打开HTTPDNS Log,线上建议关闭
  [httpdns setLogEnabled:YES];
  /*
   *  设置HTTPDNS域名解析请求类型(HTTP/HTTPS),若不调用该接口,默认为HTTP请求;
   *  SDK内部HTTP请求基于CFNetwork实现,不受ATS限制。
   */
  //[httpdns setHTTPSRequestEnabled:YES];
  // edited
  NSArray *preResolveHosts = @[ @"www.xxx.cn", @"www.xxx.cn"];
  // NSArray* preResolveHosts = @[@"pic1cdn.igetget.com"];
  // 设置预解析域名列表
  [httpdns setPreResolveHosts:preResolveHosts];
  • 如果需要降级机制实现需要遵循HttpDNSDegradationDelegate协议实现下面的代理方法
/*
 * 降级过滤器,您可以自己定义HTTPDNS降级机制
 */
- (BOOL)shouldDegradeHTTPDNS:(NSString *)hostName {
    NSLog(@"Enters Degradation filter.");
    // 根据HTTPDNS使用说明,存在网络代理情况下需降级为Local DNS
    if ([NetworkManager configureProxies]) {
        NSLog(@"Proxy was set. Degrade!");
        return YES;
    }
    
    // 假设您禁止"www.taobao.com"域名通过HTTPDNS进行解析
    if ([hostName isEqualToString:@"www.taobao.com"]) {
        NSLog(@"The host is in blacklist. Degrade!");
        return YES;
    }
    
    return NO;
}

修改react-native 网络请求库

react-native ios 集成httpdns_第1张图片
WechatIMG52.jpeg
  • 首先根据上图步骤进行到第四步,添加一个路径$(SRCROOT)/../../../../ios(否则无法再Libraries下的依赖库下访问主项目中刚刚添加的SDK)
  • 进入标记5中RCTHTTPRequestHandler.mm文件
    • 首先引入#import 上述步骤路径如果配置错误,这里将会报文件找不到的错误
    • 实现一个NSMutableURLRequest属性用来保存发送的request
      @property (nonatomic, strong) NSMutableURLRequest *request;
    • 在数据发起request请求方法中(- (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request withDelegate:(id)delegate)获取IP并替换
    • 由于ios目前都是https请求,所以进行一步拦截证书的操作,遵循NSURLSessionTaskDelegate协议并实现下面的代理方法
#pragma mark - NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *_Nullable))completionHandler {
  if (!challenge) {
    return;
  }
  NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  NSURLCredential *credential = nil;
  /*
   * 获取原始域名信息。
   */
  NSString *host = [[self.request allHTTPHeaderFields] objectForKey:@"host"];
  if (!host) {
    host = self.request.URL.host;
  }
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    if ([self evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:host]) {
      disposition = NSURLSessionAuthChallengeUseCredential;
      credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    } else {
      disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    }
  } else {
    disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  }
  // 对于其他的challenges直接使用默认的验证方案
  completionHandler(disposition, credential);
}
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                  forDomain:(NSString *)domain {
  /*
   * 创建证书校验策略
   */
  NSMutableArray *policies = [NSMutableArray array];
  if (domain) {
    [policies addObject:(__bridge_transfer id) SecPolicyCreateSSL(true, (__bridge CFStringRef) domain)];
  } else {
    [policies addObject:(__bridge_transfer id) SecPolicyCreateBasicX509()];
  }
  /*
   * 绑定校验策略到服务端的证书上
   */
  SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef) policies);
  /*
   * 评估当前serverTrust是否可信任,
   * 官方建议在result = kSecTrustResultUnspecified 或 kSecTrustResultProceed
   * 的情况下serverTrust可以被验证通过,https://developer.apple.com/library/ios/technotes/tn2232/_index.html
   * 关于SecTrustResultType的详细信息请参考SecTrust.h
   */
  SecTrustResultType result;
  SecTrustEvaluate(serverTrust, &result);
  return (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
}
  • 大功告成,附上这段代码的截图

  • react-native ios 集成httpdns_第2张图片
    WechatIMG53.jpeg
  • react-native ios 集成httpdns_第3张图片
    WechatIMG54.jpeg
  • react-native ios 集成httpdns_第4张图片
    WechatIMG55.jpeg
  • react-native ios 集成httpdns_第5张图片
    WechatIMG56.jpeg

你可能感兴趣的:(react-native ios 集成httpdns)