I want to establish a TCP tunnel through an HTTP(S) proxy with high level APIs, i.e. URLRequest
provided by iOS/macOS SDKs in Swift with the HTTP CONNECT
command. I have the following code, assuming the proxy runs on localhost:8080
:
import Foundation
let session = URLSession(configuration: .default)
var request = URLRequest(url: URL(string: "http://localhost:8080")!)
request.httpMethod = "CONNECT"
let semaphore = DispatchSemaphore(value: 0)
let task = session.dataTask(with: request) {
data, response, error in
dump(error)
semaphore.signal()
}
task.resume()
semaphore.wait()
Now the HTTP request constructed looks like:
CONNECT / HTTP/1.1
Host: localhost:8080
…
So my problem is: How do I set the request target, which currently is /
to e.g. example.com:80
. The expected request should look like:
CONNECT example.com:80 HTTP/1.1
Host: localhost:8080
…
To see the request, simply run:
nc -l 8080