0x00 TL;DR我使用 Go 來實現一個 http 客戶端和 squid 作為轉發代理來向遠程服務器發送請求。當通過代理使用 http/1.1 或 http/1.1, http2 沒有代理時一切順利,但是,當通過代理使用 http2 客戶端時,大多數連接立即關閉,只保留一兩個。不確定這是我的錯誤代碼還是什么。在 http 傳輸上啟用了 idleConn 配置。提前致謝。0x01 環境代碼package mainimport ( "context" "crypto/tls" "fmt" "io" "io/ioutil" "net" "net/http" "net/url" "sync" "time")const ( proxyURL = "http://{{proxy-ip}}:3128")func main() { wg := &sync.WaitGroup{} wg.Add(1) go func() { doSendRequests() wg.Done() }() wg.Wait()}func doSendRequests() { //client := newHTTPClient(nil) client := newHTTPClient(proxy) round := 0 for { round += 1 for i := 0; i < 5; i++ { go func(r int) { start := time.Now() var err error defer func() { duration := time.Since(start) fmt.Printf("Round: %d, A: %v, duration: %v\n", r, err, duration) }() req := newRequest("https://www.google.com") resp, err := client.Do(req) if err == nil { io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() } else { fmt.Printf("A: %v\n", err) } }(round) } time.Sleep(100 * time.Second) }}type TLSDialer struct { net.Dialer}var ( config = &tls.Config{InsecureSkipVerify: true})func (dialer *TLSDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { return tls.DialWithDialer(&dialer.Dialer, network, addr, config)}var ( DefaultDialer = net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, DualStack: true, } DefaultTLSDialer = TLSDialer{DefaultDialer})func proxy(req *http.Request) (*url.URL, error) { uri, err := url.Parse(proxyURL) if err != nil { return nil, nil } return uri, nil}
- 1 回答
- 0 關注
- 172 瀏覽
添加回答
舉報
0/150
提交
取消