前言
- 背景:隧道APP请求我们自己接口的都不走隧道
- 原理
如果使用的是NSURLSession,把NSURLSessionConfiguration的connectionProxyDictionary属性设置成空字典,请求就不会走全局设置的proxy
可结合NSURLProtocol 进行实现。
I APP请求我们自己接口的都不走隧道
- 修改configuration.connectionProxyDictionary
//APP请求我们自己接口的都不走隧道
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.connectionProxyDictionary = @{};
// 2/ AFHTTPSessionManager 创建NSURLSessionDataTask
AFHTTPSessionManager *mgr = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];
II 利用NSURLProtocol
针对NSURLSessionConfiguration设置代理和端口
+ (QNSURLSessionDemux *)sharedDemux
{
static dispatch_once_t sOnceToken;
static QNSURLSessionDemux * sDemux;
dispatch_once(&sOnceToken, ^{
NSURLSessionConfiguration * config;
config = [NSURLSessionConfiguration defaultSessionConfiguration];
// You have to explicitly configure the session to use your own protocol subclass here
// otherwise you don't see redirects <rdar://problem/17384498>.
#pragma mark - ******** 针对NSURLSessionConfiguration设置代理和端口
NSString* proxyHost = @"socks-";
// NSString* proxyHost = @"192.168.2.166";
NSNumber* proxyPort = [NSNumber numberWithInt: 8830 ];
// NSNumber* proxyPort = [NSNumber numberWithInt: 8888 ];
// Create an NSURLSessionConfiguration that uses the proxy
NSDictionary *proxyDict = @{
@"HTTPEnable" : @1,
(NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPort,
@"HTTPSEnable" : @(1),
(NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
};
// NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.connectionProxyDictionary = proxyDict;
[config setHTTPAdditionalHeaders:@{
Authorizationkey:Authorizationvalue,
}
];
config.protocolClasses = @[ self ];
sDemux = [[QNSURLSessionDemux alloc] initWithConfiguration:config];
});
return sDemux;
}
see also
更多内容请关注 #公号:iOS逆向
,只为你呈现有价值的信息,专注于移动端技术研究领域;