forked from andybalholm/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
352 lines (303 loc) · 8.65 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"bufio"
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"log"
"mime"
"net"
"net/http"
"net/textproto"
"path"
"reflect"
"strconv"
"strings"
"time"
ftp "github.com/remogatto/ftpget"
)
var dialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
var httpTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
func init() {
httpTransport.RegisterProtocol("ftp", FTPTransport{})
}
var insecureHTTPTransport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
func dialWithExtraRootCerts(network, addr string) (net.Conn, error) {
// Dial a TLS connection, and make sure it is valid against either the system default
// roots or conf.ExtraRootCerts.
serverName, _, _ := net.SplitHostPort(addr)
conn, err := tls.DialWithDialer(dialer, network, addr, &tls.Config{
ServerName: serverName,
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
state := conn.ConnectionState()
serverCert := state.PeerCertificates[0]
chains, err := serverCert.Verify(x509.VerifyOptions{
Intermediates: certPoolWith(state.PeerCertificates[1:]),
DNSName: serverName,
})
if err == nil {
state.VerifiedChains = chains
return conn, nil
}
if conf := getConfig(); conf.ExtraRootCerts != nil {
chains, err = serverCert.Verify(x509.VerifyOptions{
Intermediates: certPoolWith(state.PeerCertificates[1:]),
DNSName: serverName,
Roots: conf.ExtraRootCerts,
})
if err == nil {
state.VerifiedChains = chains
return conn, nil
}
}
conn.Close()
return nil, err
}
var transportWithExtraRootCerts = &http.Transport{
DialTLS: dialWithExtraRootCerts,
}
// A hardValidationTransport wraps another (insecure) RoundTripper and checks
// the server certificates various ways, including against an earlier
// connection's certificates. If any of the checks pass, the certificate is
// accepted.
type hardValidationTransport struct {
rt http.RoundTripper
originalCertificates []*x509.Certificate
originalServerName string
// originalCertPool is a CertPool containing the certificates from originalCertificates
originalCertPool *x509.CertPool
// expectedErrDefault is the error that was received when validating the
// original certificate against the system default CAs.
expectedErrDefault error
// expectedErrOriginal is the error that was received when validating
// the original certificate against its own certificate chain.
// It should normally be nil, but not always.
expectedErrOriginal error
}
var errCouldNotVerify = errors.New("server certificate changed; can't verify the new certificate")
func newHardValidationTransport(rt http.RoundTripper, serverName string, certificates []*x509.Certificate) *hardValidationTransport {
t := &hardValidationTransport{
rt: rt,
originalCertificates: certificates,
originalServerName: serverName,
originalCertPool: x509.NewCertPool(),
}
for _, cert := range certificates {
t.originalCertPool.AddCert(cert)
}
_, t.expectedErrDefault = certificates[0].Verify(x509.VerifyOptions{
Intermediates: t.originalCertPool,
DNSName: serverName,
})
_, t.expectedErrOriginal = certificates[0].Verify(x509.VerifyOptions{
Roots: t.originalCertPool,
DNSName: serverName,
})
return t
}
func sameType(a, b interface{}) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}
func (t *hardValidationTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL.Scheme == "http" {
req.URL.Scheme = "https"
}
resp, err := t.rt.RoundTrip(req)
if err != nil {
return resp, err
}
// Check for public key equality first, since it's cheap.
if bytes.Equal(resp.TLS.PeerCertificates[0].RawSubjectPublicKeyInfo, t.originalCertificates[0].RawSubjectPublicKeyInfo) {
return resp, nil
}
serverCert := resp.TLS.PeerCertificates[0]
intermediates := x509.NewCertPool()
for _, ic := range resp.TLS.PeerCertificates[1:] {
intermediates.AddCert(ic)
}
_, err = serverCert.Verify(x509.VerifyOptions{
Intermediates: intermediates,
DNSName: req.Host,
})
if err == nil || sameType(err, t.expectedErrDefault) {
return resp, nil
}
_, err = serverCert.Verify(x509.VerifyOptions{
Intermediates: intermediates,
DNSName: req.Host,
Roots: t.originalCertPool,
})
if err == nil || sameType(err, t.expectedErrOriginal) {
return resp, nil
}
if req.Host != t.originalServerName {
_, err := serverCert.Verify(x509.VerifyOptions{
Intermediates: intermediates,
DNSName: t.originalServerName,
})
if err == nil || sameType(err, t.expectedErrDefault) {
return resp, nil
}
_, err = serverCert.Verify(x509.VerifyOptions{
Intermediates: intermediates,
DNSName: t.originalServerName,
Roots: t.originalCertPool,
})
if err == nil || sameType(err, t.expectedErrOriginal) {
return resp, nil
}
}
resp.Body.Close()
return resp, errCouldNotVerify
}
// A simpleTransport fetches a single file over plain HTTP, as simply as
// possible.
type simpleTransport struct{}
func (simpleTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
if req.Method != "GET" {
return nil, fmt.Errorf("request method not supported by simpleTransport (expected GET, got %s)", req.Method)
}
if req.URL.Scheme != "http" {
return nil, fmt.Errorf("URL scheme not supported by simpleTransport (expected http, got %s)", req.URL.Scheme)
}
host := req.URL.Host
if _, _, err := net.SplitHostPort(host); err != nil {
host = net.JoinHostPort(host, "80")
}
conn, err := dialer.Dial("tcp", host)
if err != nil {
return nil, fmt.Errorf("error connecting to %s: %v", host, err)
}
if err = req.Write(conn); err != nil {
return nil, fmt.Errorf("error sending request for %v: %v", req.URL, err)
}
br := bufio.NewReader(conn)
tp := textproto.NewReader(br)
resp = &http.Response{
Request: req,
}
// Parse the first line of the response.
line, err := tp.ReadLine()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
if i := strings.IndexByte(line, ' '); i == -1 {
return nil, fmt.Errorf("malformed HTTP response: %q", line)
} else {
resp.Proto = line[:i]
resp.Status = strings.TrimLeft(line[i+1:], " ")
}
statusCode := resp.Status
if i := strings.IndexByte(resp.Status, ' '); i != -1 {
statusCode = resp.Status[:i]
}
if len(statusCode) != 3 {
return nil, fmt.Errorf("malformed HTTP status code: %q", statusCode)
}
resp.StatusCode, err = strconv.Atoi(statusCode)
if err != nil || resp.StatusCode < 0 {
return nil, fmt.Errorf("malformed HTTP status code: %q", statusCode)
}
var ok bool
if resp.ProtoMajor, resp.ProtoMinor, ok = http.ParseHTTPVersion(resp.Proto); !ok {
return nil, fmt.Errorf("malformed HTTP version: %q", resp.Proto)
}
// Parse the response headers.
mimeHeader, err := tp.ReadMIMEHeader()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
resp.Header = http.Header(mimeHeader)
cl := resp.Header.Get("Content-Length")
if cl != "" {
if resp.ContentLength, err = strconv.ParseInt(cl, 10, 64); err != nil {
return nil, fmt.Errorf("invalid Content-Length: %q", cl)
}
}
resp.Header.Del("Content-Length")
resp.Header.Del("Trailer")
var body struct {
io.Reader
io.Closer
}
body.Reader = &io.LimitedReader{br, resp.ContentLength}
body.Closer = conn
resp.Body = body
return resp, nil
}
// An FTPTransport fetches files via FTP.
type FTPTransport struct{}
func (FTPTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
if req.Method != "GET" {
return &http.Response{
StatusCode: http.StatusMethodNotAllowed,
Request: req,
}, nil
}
fullPath := req.URL.Host + req.URL.Path
r, w := io.Pipe()
xfer, err := ftp.GetAsync(fullPath, w)
if err != nil {
return nil, err
}
go func() {
for stat := range xfer.Status {
switch stat {
case ftp.COMPLETED:
w.Close()
return
case ftp.ERROR:
err := <-xfer.Error
log.Printf("FTP: error downloading %v: %v", req.URL, err)
w.CloseWithError(err)
return
}
}
}()
resp = &http.Response{
StatusCode: 200,
ProtoMajor: 1,
ProtoMinor: 1,
Request: req,
Body: r,
Header: make(http.Header),
}
ext := path.Ext(req.URL.Path)
if ext != "" {
ct := mime.TypeByExtension(ext)
if ct != "" {
resp.Header.Set("Content-Type", ct)
}
}
return resp, nil
}