forked from neex/http2smugl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http2.go
183 lines (152 loc) · 4.36 KB
/
http2.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bytes"
"crypto/tls"
"fmt"
"github.com/fatih/color"
"net"
"strconv"
"time"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
)
func sendHTTP2Request(connectAddr, serverName string, noTLS bool, request *HTTPMessage, timeout time.Duration) (rawRequest string, response *HTTPMessage, err error) {
address := connectAddr
if _, _, err := net.SplitHostPort(connectAddr); err != nil {
address = net.JoinHostPort(address, "443")
}
rawRequest = requestRaw(request.Headers, request.Body)
name, port, err := net.SplitHostPort(address)
if err != nil {
return rawRequest,nil, fmt.Errorf("invalid address %v: %w", address, err)
}
ip, err := DefaultDNSCache.Lookup(name)
if err != nil {
return rawRequest,nil, fmt.Errorf("lookup for %v failed: %w", name, err)
}
address = net.JoinHostPort(ip.String(), port)
tcpConn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return rawRequest,nil, err
}
defer func() { _ = tcpConn.Close() }()
_ = tcpConn.SetDeadline(time.Now().Add(timeout))
var c net.Conn
if noTLS {
c = tcpConn
} else {
c = tls.Client(tcpConn, &tls.Config{
NextProtos: []string{"h2"},
ServerName: serverName,
InsecureSkipVerify: true,
})
}
if _, err := c.Write(prepareHTTP2Request(request)); err != nil {
return rawRequest,nil, err
}
response = &HTTPMessage{}
headersDecoder := hpack.NewDecoder(^uint32(0), func(f hpack.HeaderField) {
response.Headers = append(response.Headers, Header{f.Name, f.Value})
})
framer := http2.NewFramer(nil, c)
hasBody := false
bodyRead := false
headersDone := false
for !headersDone || (hasBody && !bodyRead) {
var f http2.Frame
f, err = framer.ReadFrame()
if err != nil {
return
}
if ga, ok := f.(*http2.GoAwayFrame); ok {
err = ConnDropError{
Wrapped: fmt.Errorf("received GOAWAY: error code %v", ga.ErrCode),
}
return
}
if f.Header().StreamID != 1 {
continue
}
switch f := f.(type) {
case *http2.HeadersFrame:
if _, err := headersDecoder.Write(f.HeaderBlockFragment()); err != nil {
return rawRequest,nil, err
}
headersDone = f.HeadersEnded()
hasBody = !f.StreamEnded()
case *http2.ContinuationFrame:
if _, err := headersDecoder.Write(f.HeaderBlockFragment()); err != nil {
return rawRequest,nil, err
}
headersDone = f.HeadersEnded()
case *http2.DataFrame:
// we should send window update, but who cares
response.Body = append(response.Body, f.Data()...)
bodyRead = f.StreamEnded()
case *http2.RSTStreamFrame:
err = ConnDropError{Wrapped: fmt.Errorf("error code %v", f.ErrCode)}
return
}
}
return
}
func prepareHTTP2Request(request *HTTPMessage) []byte {
var hpackBuf []byte
for i := range request.Headers {
hpackBuf = hpackAppendHeader(hpackBuf, &request.Headers[i])
}
requestBuf := bytes.NewBuffer(nil)
requestBuf.Write([]byte(http2.ClientPreface))
framer := http2.NewFramer(requestBuf, nil)
_ = framer.WriteSettings(http2.Setting{
ID: http2.SettingInitialWindowSize,
Val: (1 << 30) - 1,
})
_ = framer.WriteWindowUpdate(0, (1<<30)-(1<<16)-1)
_ = framer.WriteHeaders(http2.HeadersFrameParam{
StreamID: 1,
BlockFragment: hpackBuf,
EndStream: len(request.Body) == 0,
EndHeaders: true,
})
start := 0
for start < len(request.Body) {
end := start + 65536
if end > len(request.Body) {
end = len(request.Body)
}
_ = framer.WriteData(1, end == len(request.Body), request.Body[start:end])
start = end
}
_ = framer.WriteSettingsAck()
return requestBuf.Bytes()
}
func hpackAppendHeader(dst []byte, h *Header) []byte {
dst = append(dst, 0x10)
dst = hpackAppendVarInt(dst, 7, uint64(len(h.Name)))
dst = append(dst, h.Name...)
dst = hpackAppendVarInt(dst, 7, uint64(len(h.Value)))
dst = append(dst, h.Value...)
return dst
}
func hpackAppendVarInt(dst []byte, n byte, val uint64) []byte {
k := uint64((1 << n) - 1)
if val < k {
return append(dst, byte(val))
}
dst = append(dst, byte(k))
val -= k
for ; val >= 128; val >>= 7 {
dst = append(dst, byte(0x80|(val&0x7f)))
}
return append(dst, byte(val))
}
func requestRaw(headers Headers, body []byte) (request string) {
var s string
for _,v := range headers{
s += fmt.Sprintf("%s: %s\n", color.CyanString(strconv.Quote(v.Name)), color.GreenString(strconv.Quote(v.Value)))
}
s += "\r\n"
s += color.GreenString(color.YellowString(strconv.Quote(string(body))))
return s
}