gpt4 book ai didi

http - 发送请求时如何使用uTLS连接?

转载 作者:行者123 更新时间:2023-12-01 22:21:45 28 4
gpt4 key购买 nike

我正在尝试通过uTLS库使用Hello Chrome指纹发送请求,但是在浏览了文档之后,我试图弄清楚如何在发送请求时利用uTLS连接。
我已经在下面的代码的两个部分,但不确定如何将它们放在一起,或者我是否正在正确的方式。

package main

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"

tls "github.com/refraction-networking/utls"
)

func main() {

tcpConn, err := net.Dial("tcp", "151.101.65.69:443")
if err != nil {
fmt.Printf("net.Dial() failed: %+v\n", err)
return
}
config := tls.Config{ServerName: "www.stackoverflow.com"}
tlsConn := tls.UClient(tcpConn, &config, tls.HelloChrome_Auto)
defer tlsConn.Close()

err = tlsConn.Handshake()
if err != nil {
fmt.Printf("uTlsConn.Handshake() error: %+v", err)
} else {
fmt.Println("Handshake met")
}

cookieJar, _ := cookiejar.New(nil)

client := &http.Client{
Jar: cookieJar,
Transport: &http.Transport{},
}

req, err := http.NewRequest("GET", "https://ja3er.com/json", nil)
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
}

最佳答案

只需将您的代码移到Transport.DialTLSContext:

DialTLSContext指定用于创建的可选拨号功能
非代理HTTPS请求的TLS连接。
[...]
返回的net.Conn假定已经是
经过TLS握手。

client := &http.Client{
Jar: cookieJar,
Transport: &http.Transport{
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// Note that hardcoding the address is not necessary here. Only
// do that if you want to ignore the DNS lookup that already
// happened behind the scenes.

tcpConn, err := (&net.Dialer{}).DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
config := tls.Config{ServerName: "www.stackoverflow.com"}
tlsConn := tls.UClient(tcpConn, &config, tls.HelloChrome_Auto)

err = tlsConn.Handshake()
if err != nil {
return nil, fmt.Errorf("uTlsConn.Handshake() error: %w", err)
}

return tlsConn, nil
},
},
}

关于http - 发送请求时如何使用uTLS连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63054147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com