Skip to content
zhtut edited this page Dec 17, 2021 · 6 revisions

iOS支持设置https SNI的网络库 用swift源码修改来的,把网络库URLSession抽离出来了,使用的是swift的release/5.6版本,应该是比较安全的 依赖了CFURLSessionInterface库,也是Swift Foundation抽离的一个c库,主要功能是对libcurl的封装 libcurl是自己编译的7.80.0版本

使用方法,先找到host和ip,然后设置resolve到header中

func handlerIP() {
    let host = ""
    let ip = ""
    let resolve = "\(host):443:\(ip)"
    setValue(resolve, forHTTPHeaderField: "resolve")
}

也可以设置connectTo属性

func handlerIP() {
    let host = ""
    let ip = ""
    let connectTo = "\(host):443:\(ip):443"
    setValue(connectTo, forHTTPHeaderField: "connectTo")
}

这两个应该都可以支持sni功能,但哪个好用我也还没试,底下的代码为:

func set(connectTo: String) {
    let list = _CurlStringList([connectTo])
    try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCONNECT_TO, list.asUnsafeMutablePointer).asError()
    connectToList = list
}

func set(resolve: String) {
    let list = _CurlStringList([resolve])
    try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionRESOLVE, list.asUnsafeMutablePointer).asError()
    try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionIPRESOLVE, 0).asError()
    resolveList = list
}
Clone this wiki locally