使用 MobileSubstrate 修改 iPhoneOS 上的所有 HTTP 请求?
我想写一个 MobileSubstrate (http://www.iphonedevwiki.net/index.php/MobileSubstrate ) 插件接受每个 HTTP 请求并修改 URL。 (原因很复杂,需要一段时间才能解释,所以请相信我,没有其他选择)。基本上,我需要知道什么是最好的 Objective-C 消息或 C 函数,我应该重写它来拦截任何 HTTP 请求,修改 URL,并将请求发送回原始函数(这不应该只是发送的第一个请求)由应用程序,但也包括任何重定向)。我认为它可能在 NSURL 或 CFNetwork 中,但不确定具体在哪里。此功能在替换后应适用于任何应用程序和操作系统本身。
编辑: 好的,这是我想要执行的两个示例场景,请注意,要重写的方法应该是由从应用程序到操作系统的任何 HTTP 请求调用的方法:
1) MobileSafari 中的用户在 aaa.com/a.html 中输入
2) aaa.com/a.html 重定向至 aaa.com/b.html
3) aaa.com/b.html 加载 bbb.com/c.png
我想更改请求,以便添加所有内容 ?i=j。
例如:将所有 aaa.com/a.html 更改为 aaa.com/a.html?i=j, aaa.com/b.html?i=j, bbb.com/c.png?i=j?
还有一个:
1) 用户使用Maps.app
2) Google 地图尝试加载 google.com/map1.png(我知道不是实际图像)
将 google.com/map1.png 更改为 google.com/map1.png?i=j?
I want to write a MobileSubstrate (http://www.iphonedevwiki.net/index.php/MobileSubstrate) addon that takes every HTTP request and modifies the URL. (The reason is very complicated and would take a while to explain, so just trust me that there's not an alternative). Basically, I need to know what's the best Objective-C message or C function that I should overwrite to intercept any HTTP request, modify the URL, and send the request back to the original function (this shouldn't be just the first request sent by an application, but any redirects too). I think it may be in NSURL or CFNetwork, but am not sure where exactly. This function should, when replaced, work with any apps and the OS itself.
EDIT:
Ok, here's two sample scenarios of what I want to do, note that the method to override should be one that is called by any HTTP request anywhere from an app to the OS:
1) User in MobileSafari types in aaa.com/a.html
2) aaa.com/a.html redirects to aaa.com/b.html
3) aaa.com/b.html loads bbb.com/c.png
I want to change the request so everything has ?i=j added.
EX: Change all aaa.com/a.html to aaa.com/a.html?i=j, aaa.com/b.html?i=j, bbb.com/c.png?i=j?
And another one:
1) User uses Maps.app
2) Google Maps tries to load google.com/map1.png (not actual image, I know)
Change google.com/map1.png to google.com/map1.png?i=j?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
挂钩以下方法并用您编辑过的
mutableCopy
替换请求。-[NSURLConnection sendWillSendRequest:redirectResponse:]
-[NSURLConnection initWithRequest:delegate:]
–[NSURLConnection initWithRequest:delegate:startImmediately:]
这不会重定向原始 TCP 套接字连接,仅重定向通过标准 API 传输的 HTTP 请求。
注意:这样做时要非常小心。在我看来,重定向所有 http 请求违反了用户信任
Hook the following methods and swap out the request with a
mutableCopy
that you've edited.-[NSURLConnection sendWillSendRequest:redirectResponse:]
-[NSURLConnection initWithRequest:delegate:]
–[NSURLConnection initWithRequest:delegate:startImmediately:]
This will not redirect raw TCP socket connections, only the HTTP requests that are funnelled through the standard API.
Note: be very wary of doing this. Redirecting all http requests is a violation of user trust in my opinion