iOS 抓包原理

2023-06-29  本文已影响0人  jazzfly

拦截原理:

// 1.注册我们的协议类
[NSURLProtocol registerClass:[DLCustomURLProtocol class]];

### 拦截方法的介绍:
// 2.是否能够处理给定的请求
+ (BOOL)canInitWithRequest:(NSURLRequest *)request;

// 3.处理URL转换为IP地址
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;

// 4.开始网络请求
- (void)startLoading;

// 5.结束网络请求
- (void)stopLoading;

// 6.处理网络请求的code
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
                                 didReceiveResponse:(NSURLResponse *)response
                                  completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler;

// 7.处理网络请求的返回信息
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error;

WKWebView使用私有API的原因:

#pragma mark - WKWebView的私有API
#pragma mark - 以下方法为iOS的私有方法,建议上线时注释————以下全部代码
+ (void)DL_unregisterScheme {
    [self wk_registerScheme:@"http"];
    [self wk_registerScheme:@"https"];
}

FOUNDATION_STATIC_INLINE Class ContextControllerClass() {
    static Class cls;
    if (!cls) {
        cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];
    }
    return cls;
}

FOUNDATION_STATIC_INLINE SEL RegisterSchemeSelector() {
    return NSSelectorFromString(@"registerSchemeForCustomProtocol:");
}

+ (void)wk_registerScheme:(NSString *)scheme {
    Class cls = ContextControllerClass();
    SEL sel = RegisterSchemeSelector();
    if ([(id)cls respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [(id)cls performSelector:sel withObject:scheme];
#pragma clang diagnostic pop
    }
}

抓取第三方AFN请求

+ (void)exchangeAFNSessionConfiguration {
    Class cls = NSClassFromString(@"__NSCFURLSessionConfiguration") ?: NSClassFromString(@"NSURLSessionConfiguration");
    Method originalMethod = class_getInstanceMethod(cls, @selector(protocolClasses));
    Method stubMethod = class_getInstanceMethod([DLCustomURLProtocol class], @selector(protocolClasses));
    if (!originalMethod || !stubMethod) {
        CLog(@"Couldn't load NEURLSessionConfiguration");
        return;
    }
    method_exchangeImplementations(originalMethod, stubMethod);
}
上一篇下一篇

猜你喜欢

热点阅读