我想通过带有高级API的HTTP(S)代理建立TCP隧道,即使用Swift中的iOS / macOS SDK和HTTP URLRequest
命令提供的CONNECT
。假设代理在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()
现在构建的HTTP请求如下所示:
CONNECT / HTTP/1.1
Host: localhost:8080
…
所以我的问题是:如何设置请求目标,当前为/
,例如example.com:80
。预期的请求应如下所示:
CONNECT example.com:80 HTTP/1.1
Host: localhost:8080
…
要查看请求,只需运行:
nc -l 8080