-
Notifications
You must be signed in to change notification settings - Fork 25
/
transport.go
101 lines (82 loc) · 2.11 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package azuretls
import (
"context"
http "github.com/Noooste/fhttp"
"github.com/Noooste/fhttp/http2"
"net"
"net/url"
"time"
)
func (s *Session) InitTransport(browser string) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.Transport == nil {
s.initHTTP1()
}
if s.HTTP2Transport == nil {
if err = s.initHTTP2(browser); err != nil {
return
}
}
return
}
func (s *Session) initHTTP1() {
s.Transport = &http.Transport{
TLSHandshakeTimeout: s.TimeOut,
ResponseHeaderTimeout: s.TimeOut,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
s.Connections.mu.RLock()
defer s.Connections.mu.RUnlock()
rc := s.Connections.hosts[addr]
return rc.TLS, nil
},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: s.TimeOut,
KeepAlive: 30 * time.Second,
}
if s.ModifyDialer != nil {
if err := s.ModifyDialer(dialer); err != nil {
return nil, err
}
}
return dialer.DialContext(s.ctx, network, addr)
},
Proxy: func(*http.Request) (*url.URL, error) {
if s.Proxy == "" {
return nil, nil
}
return url.Parse(s.Proxy)
},
ForceAttemptHTTP2: true,
MaxIdleConns: 1e3,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
func (s *Session) initHTTP2(browser string) error {
tr, err := http2.ConfigureTransports(s.Transport) // upgrade to HTTP2, while keeping http.Transport
if err != nil {
return err
}
tr.Priorities = defaultStreamPriorities(browser)
tr.Settings, tr.SettingsOrder = defaultHeaderSettings(browser)
tr.ConnectionFlow = defaultWindowsUpdate(browser)
if s.HeaderPriority != nil {
tr.HeaderPriority = s.HeaderPriority
} else {
tr.HeaderPriority = defaultHeaderPriorities(browser)
}
tr.StrictMaxConcurrentStreams = true
tr.PushHandler = &http2.DefaultPushHandler{}
for k, v := range tr.Settings {
switch k {
case http2.SettingInitialWindowSize:
tr.InitialWindowSize = v
case http2.SettingHeaderTableSize:
tr.HeaderTableSize = v
}
}
s.HTTP2Transport = tr
return nil
}