SwiftyURLProtocol is an URLProtocol
wrapper. Can be used in conjunction with UIWebView
to provide http/sock proxy support.
- Multiple proxies
- Router to define which proxy to use for a request
- fixes SOCKS proxy bug on iOS (hosts are resolved locally instead of via proxy)
- iOS 10.0+ / macOS 10.12+
- Xcode 8.1+
- Swift 3.0+
Include SwiftyURLProtocol
as an framework into your project.
To use SwiftyURLProtocol
with URLSession
add it into URLSessionConfiguration.protocolClasses
import SwiftyURLProtocol
SwiftyURLProtocol.setRouter { (request) -> ProxyURLProtocol.Proxy? in
return ProxyURLProtocol.Proxy.socks(host: "127.0.0.1", port: 9050, probe: nil)
}
let config = URLSessionConfiguration.default
config.protocolClasses = [SwiftyURLProtocol.self]
let task = session.dataTask(with: URL(string: "http://google.com")!) { (data, response, error) in
...
}
task.resume()
URLProtocol.registerClass(ProxyURLProtocol.self)
To use SwiftyURLProtocol
with UIWebView
register it in URLProtocol
import SwiftyURLProtocol
// route all .onion hosts via socks proxy. Everything else will be accessed directly.
SwiftyURLProtocol.setRouter { (request) -> ProxyURLProtocol.Proxy? in
if request.url?.host?.hasSuffix(".onion") ?? false {
return ProxyURLProtocol.Proxy.socks(host: "127.0.0.1", port: 9050, probe: nil)
}
else {
return nil
}
}
URLProtocol.registerClass(SwiftyURLProtocol.self)