Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
Created March 12, 2017 20:02
Show Gist options
  • Save shnhrrsn/45d9eeb3187dbd1bbee02f697daf68a2 to your computer and use it in GitHub Desktop.
Save shnhrrsn/45d9eeb3187dbd1bbee02f697daf68a2 to your computer and use it in GitHub Desktop.
URLSession + MITM Proxy
fileprivate class NetworkingDelegate: NSObject, URLSessionDelegate {
fileprivate func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
private let proxySessionDelegate = NetworkingDelegate()
private func makeProxySession() -> URLSession {
let proxyPort = 8080
let proxyHost = "10.0.1.88"
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
"HTTPEnable": true,
"HTTPProxy": proxyHost,
"HTTPPort": proxyPort,
"HTTPSEnable": true,
"HTTPSProxy": proxyHost,
"HTTPSPort": proxyPort,
]
return URLSession(configuration: configuration, delegate: proxySessionDelegate, delegateQueue: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment