背景

最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/AFNetworking/issues/2954,不过最终还是靠着stackoverflow上的一丁点提示,顺利找到并汇集成了一个可用的解决方案.大喜,与君共享!

问题描述

通过IP直接访问网站,可以解决DNS劫持问题.DNS劫持,可以通过修改电脑的host文件模拟.如果是HTTP请求,使用ip地址直接访问接口,配合header中Host字段带上原来的域名信息即可;如果是 https请求,会很麻烦,需要 Overriding TLS Chain Validation Correctly;curl 中有一个 -resolve 方法可以实现使用指定ip访问https网站,iOS中集成curl库应该也可以,不过改动太大,未验证;对于服务器IP经常变的情况,可能需要使用httpDNS服务,参见:https://www.dnspod.cn/httpdns.

解决方案讨论

1. 最直接的方式是允许无效的SSL证书,生产环境不建议使用;

2.一个需要部分重写AFN源码的方法.

  • 在Info.plist中添加NSAppTransportSecurity类型Dictionary,在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES.这些本来是用来解决iOS9下,允许HTTP请求访问网络的,当然作用不止这些.具体原因感兴趣的自行google.

  • 给 AFURLConnectionOperation 类添加新属性:

/** 可信任的域名,用于支持通过ip访问此域名下的https链接.
Trusted domain, this domain for support via IP access HTTPS links.
*/
@property(nonatomic, strong) NSMutableArray * trustHostnames;
  • 给 AFURLConnectionOperation 实现的代理方法: - (void)connection:(NSURLConnection *)connection

    willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 添加添加可信任的域名的相关逻辑代码:
- (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if (self.authenticationChallenge) {
self.authenticationChallenge(connection, challenge);
return;
} if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; /* 添加可信任的域名,以支持:直接使用ip访问特定https服务器.
Add trusted domain name to support: direct use of IP access specific HTTPS server.*/
for (NSString * trustHostname in [self trustHostnames]) {
serverTrust = AFChangeHostForTrust(serverTrust, trustHostname);
} ....
  • 参考Apple官方文档,实现自定义的添加可信域名的函数: AFChangeHostForTrust
static inline SecTrustRef AFChangeHostForTrust(SecTrustRef trust, NSString * trustHostname)
{
if ( ! trustHostname || [trustHostname isEqualToString:@""]) {
return trust;
} CFMutableArrayRef newTrustPolicies = CFArrayCreateMutable(
kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); SecPolicyRef sslPolicy = SecPolicyCreateSSL(true, (CFStringRef)trustHostname); CFArrayAppendValue(newTrustPolicies, sslPolicy); #ifdef MAC_BACKWARDS_COMPATIBILITY
/* This technique works in OS X (v10.5 and later) */ SecTrustSetPolicies(trust, newTrustPolicies);
CFRelease(oldTrustPolicies); return trust;
#else
/* This technique works in iOS 2 and later, or
OS X v10.7 and later */ CFMutableArrayRef certificates = CFArrayCreateMutable(
kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); /* Copy the certificates from the original trust object */
CFIndex count = SecTrustGetCertificateCount(trust);
CFIndex i=0;
for (i = 0; i < count; i++) {
SecCertificateRef item = SecTrustGetCertificateAtIndex(trust, i);
CFArrayAppendValue(certificates, item);
} /* Create a new trust object */
SecTrustRef newtrust = NULL;
if (SecTrustCreateWithCertificates(certificates, newTrustPolicies, &newtrust) != errSecSuccess) {
/* Probably a good spot to log something. */ return NULL;
} return newtrust;
#endif
}
  • 使用AOP方法,重写 AFURLConnectionOperation 的trustHostnames属性:
    /* 使用AOP方式,指定可信任的域名, 以支持:直接使用ip访问特定https服务器.*/
[AFURLConnectionOperation aspect_hookSelector:@selector(trustHostnames) withOptions:AspectPositionInstead usingBlock: ^(id<AspectInfo> info){
__autoreleasing NSArray * trustHostnames = @[@"www.example.com"]; NSInvocation *invocation = info.originalInvocation;
[invocation setReturnValue:&trustHostnames];
}error:NULL];

此处用到的是一个 iOS AOP库,不熟悉的点这里: http://www.ios122.com/2015/08/aspects/.

AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?的更多相关文章

  1. 解决python2.7.9以下版本requests访问https的问题

    在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...

  2. ACME[free https] Linux中使用curl命令访问https站点4种常见错误和解决方法

    free https certification generator https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 每一种客户 ...

  3. 解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题

    安装fiddler后,使用Chrome访问https网站时,可能会出现以下错误,本文说明如何解决此类问题: “您的连接不是私密连接”.“NET::ERR_CERT_AUTHORITY_INVALID” ...

  4. 火狐浏览器 访问所有HTTPS网站显示连接不安全解决办法

    当 Firefox 连接到一个安全的网站时(网址最开始为“https://”),它必须确认该网站出具的证书有效且使用足够高的加密强度.如果证书无法通过验证,或加密强度过低,Firefox 会中止连接到 ...

  5. IIS中访问自己开发的Webservice site就自动停止,尝试重启IIS和重启服务器都不能解决。

    今天在加班的时候发现一个奇怪的问题,IIS里面我们自己开发的Webservice site一访问就自动停止.尝试重启IIS和重启服务器都不能解决.后台windows events报错信息是The Mo ...

  6. afnetworking NSCocoaErrorDomain Code=3840 解决

    afnetworking json解析出错 解决方法1 AFURLResponseSerialization.m 258行修改 responseString = [responseString str ...

  7. 解决linux部署项目后,第一次访问初始化数据源的时候很慢的问题

    先说一下场景 测试环境是linux - centos6.5,项目是分布式的,分别搭建到linux上 数据货使用的是阿里巴巴数据源 每次重启后的第一次访问会比较慢,查看日志发现 卡在这个地方 ...{d ...

  8. WCF服务发布到IIS时候,只能根据hostname访问,不能根据IP地址访问的解决办法

    本文转载:http://www.cnblogs.com/deerbox/archive/2013/05/13/3076248.html 环境: VS2010 sp1,.net framework 4. ...

  9. 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)

    最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题,  用   echo   phpinfo() ;  查看, 支持op ...

随机推荐

  1. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)

    继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...

  2. Codeforces Round #384 (Div. 2)B. Chloe and the sequence 数学

    B. Chloe and the sequence 题目链接 http://codeforces.com/contest/743/problem/B 题面 Chloe, the same as Vla ...

  3. js中“==”与"==="的区别

    首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 一言以蔽之:==先转换类型再 ...

  4. gradle介绍

    Gradle是什么? https://gradle.org/whygradle-build-automation/ 自动化编译的工具,可编程,适合各种语言,管理各种依赖,高效,提供分析报告. 我个人觉 ...

  5. LCLFramework框架之Repository模式

    Respository模式在示例中的实际目的小结一下 Repository模式是架构模式,在设计架构时,才有参考价值: Repository模式主要是封装数据查询和存储逻辑: Repository模式 ...

  6. wordpress自动保存远程图片插件 DX-auto-save-images

    wordpress自动保存远程图片插件DX-auto-save-images 解决了保存文章就可以自动将远程图片保存到你的服务器上了. 具体操作步骤如下: 1.安装启用wordpress自动保存远程图 ...

  7. CentOS安装keepalived

    Haproxy.Keepalived双主高可用负载均衡  1.安装keepalived yum install keepalived -y

  8. A little problem for pt-pmp

    https://bugs.launchpad.net/percona-toolkit/+bug/1320168 We use the pt-pmp (a variety for pmp !http:/ ...

  9. Android SDK开发包国内下载地址

    不知道是因为最近kaihui还是怎么的,打开android sdk官方网站特别的慢,想下载最新版本的platform几乎变成不可能完成的任务,不知道为什么Google不像Apache那样在各国设立镜像 ...

  10. vm导入后远程桌面无法登陆域账户

    以本地Admin账户登录,打开C:\Windows\System32\sysprep目录,运行sysprep.exe工具,勾选Generalize,点击OK.系统重启后对Windows重新初始化,生成 ...