-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
450 lines (422 loc) · 12.6 KB
/
main.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (c) 2016 Moriyoshi Koizumi
* Copyright (c) 2012 Elazar Leibovich.
* Copyright (c) 2012 The Go Authors.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Elazar Leibovich. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
crand "crypto/rand"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"math/big"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/moriyoshi/devproxy/httpx"
"github.com/pkg/errors"
"github.com/shibukawa/configdir"
"github.com/sirupsen/logrus"
)
type TLSConfigFactory func(hostPortPairStr string, proxyCtx *OurProxyCtx) (*tls.Config, error)
type DevProxy struct {
Logger *logrus.Logger
LogWriter io.WriteCloser
StdLogger *log.Logger
Config *Config
DefaultCharset string
CryptoRandReader io.Reader
certCache *CertCache
}
func isTimeout(e error) bool {
t, ok := e.(interface{ Timeout() bool })
if !ok {
return false
}
return t.Timeout()
}
func (ctx *DevProxy) unidiTunnel(connA net.Conn, connB net.Conn) {
n, err := io.Copy(connA, connB)
msg := fmt.Sprintf("%d bytes transferred from %v to %v", n, connA.RemoteAddr(), connB.RemoteAddr())
if err != nil && err != io.EOF && !isTimeout(err) {
ctx.Logger.Errorf("%s; %s", err.Error(), msg)
} else {
ctx.Logger.Debug(msg)
}
}
func (ctx *DevProxy) bidiTunnel(connA net.Conn, connB net.Conn) {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
ctx.unidiTunnel(connA, connB)
connA.SetDeadline(time.Unix(0, 0))
connB.SetDeadline(time.Unix(0, 0))
}()
wg.Add(1)
go func() {
defer wg.Done()
ctx.unidiTunnel(connB, connA)
connA.SetDeadline(time.Unix(0, 0))
connB.SetDeadline(time.Unix(0, 0))
}()
wg.Wait()
}
func (ctx *DevProxy) proxyIsApplicable(req *http.Request) bool {
pcfg := ctx.Config.Proxy
if len(pcfg.IncludedHosts) > 0 {
pairs := toHostPortPairs(req.URL)
for _, a := range pcfg.IncludedHosts {
for _, b := range pairs {
if a == b {
return true
}
}
}
return false
} else if len(pcfg.ExcludedHosts) > 0 {
pairs := toHostPortPairs(req.URL)
for _, a := range pcfg.ExcludedHosts {
for _, b := range pairs {
if a.Matches(b) {
return false
}
}
}
return true
} else {
return true
}
}
func (ctx *DevProxy) getProxyUrlForRequest(req *http.Request) (*url.URL, *tls.Config, error) {
if !ctx.proxyIsApplicable(req) {
ctx.Logger.Debugf("No outbound proxy is applicable for %s", req.URL.String())
return nil, nil, nil
}
if req.URL.Scheme == "https" {
if ctx.Config.Proxy.HTTPSProxy != nil {
return ctx.Config.Proxy.HTTPSProxy, ctx.Config.Proxy.TLSConfig, nil
}
}
// falls back to http
if req.URL.Scheme == "https" || req.URL.Scheme == "http" {
if ctx.Config.Proxy.HTTPProxy != nil {
return ctx.Config.Proxy.HTTPProxy, ctx.Config.Proxy.TLSConfig, nil
}
}
return nil, nil, nil
}
func (ctx *DevProxy) newTLSConfigFactory() TLSConfigFactory {
return func(hostPortPairStr string, proxyCtx *OurProxyCtx) (*tls.Config, error) {
pair := splitHostPort(hostPortPairStr)
if ctx.Config.MITM.ServerTLSConfigTemplate == nil {
return nil, errors.Errorf("no TLS configuration template is available")
}
config := ctx.Config.MITM.ServerTLSConfigTemplate.Clone()
ctx.Logger.Debugf("obtaining temporary certificate for %s", pair.Host)
cert, err := ctx.prepareMITMCertificate([]string{pair.Host})
if err != nil {
return nil, errors.Wrapf(err, "cannot sign host certificate with provided CA")
}
config.Certificates = append(config.Certificates, *cert)
return config, nil
}
}
func (ctx *DevProxy) newProxyURLBuilder() func(*http.Request) (*url.URL, *tls.Config, error) {
return func(req *http.Request) (*url.URL, *tls.Config, error) {
return ctx.getProxyUrlForRequest(req)
}
}
func (ctx *DevProxy) newHttpTransport() *httpx.Transport {
transport := &httpx.Transport{
TLSClientConfig: ctx.Config.MITM.ClientTLSConfigTemplate,
Proxy2: ctx.newProxyURLBuilder(),
}
transport.RegisterProtocol("fastcgi", &fastCGIRoundTripper{Logger: ctx.Logger})
transport.RegisterProtocol("file", NewFileTransport(ctx.Config.FileTransport))
transport.RegisterProtocol("x-http-redirect", &redirector{Logger: ctx.Logger})
return transport
}
var domainNameRegex = regexp.MustCompile("^[A-Za-z](?:[0-9A-Za-z-_]*[0-9A-Za-z])?$")
func validateDomainName(host string) bool {
return domainNameRegex.MatchString(host)
}
func (ctx *DevProxy) searchPreparedCertificate(hosts []string, now time.Time) (*tls.Certificate, bool, error) {
for _, prepared := range ctx.Config.MITM.Prepared {
hostMap := map[string]struct{}{}
for _, host := range hosts {
if !prepared.Pattern.MatchString(host) {
break
}
hostMap[host] = struct{}{}
}
if len(hostMap) != len(hosts) {
continue
}
cert := prepared.Certificate
if cert.Subject.CommonName != "" {
for _, host := range hosts {
if wildMatch(cert.Subject.CommonName, host) {
delete(hostMap, host)
}
}
}
for _, dnsName := range cert.DNSNames {
for _, host := range hosts {
if wildMatch(dnsName, host) {
delete(hostMap, host)
}
}
}
if len(hostMap) == 0 {
return prepared.TLSCertificate, true, nil
}
}
return nil, false, nil
}
func (ctx *DevProxy) generateMITMCertificate(host string, sortedSans []string, now time.Time) (*tls.Certificate, error) {
ca := ctx.Config.MITM.SigningCertificateKeyPair
if ca.Certificate == nil {
return nil, errors.Errorf("no MITM CA is available")
}
ctx.Logger.Debugf("CA: CN=%s", ca.Certificate.Subject.CommonName)
start := now.Add(-time.Minute)
end := now.Add(time.Duration(ctx.Config.MITM.ValidityPeriod) * 24 * time.Hour)
h := sha1.New()
for _, host := range sortedSans {
h.Write([]byte(host))
}
binary.Write(h, binary.BigEndian, start)
binary.Write(h, binary.BigEndian, end)
hash := h.Sum(nil)
serial := big.Int{}
serial.SetBytes(hash)
ctx.Logger.Debugf("Generated serial=%v", serial)
template := x509.Certificate{
SignatureAlgorithm: ca.Certificate.SignatureAlgorithm,
SerialNumber: &serial,
Issuer: ca.Certificate.Subject,
Subject: pkix.Name{
Organization: []string{"GoProxy untrusted MITM proxy"},
CommonName: host,
},
NotBefore: start,
NotAfter: end,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
MaxPathLen: -1,
DNSNames: sortedSans,
}
derBytes, err := x509.CreateCertificate(ctx.CryptoRandReader, &template, ca.Certificate, ca.Certificate.PublicKey, ca.PrivateKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to create a certificate")
}
return &tls.Certificate{
Certificate: [][]byte{derBytes, ca.Certificate.Raw},
PrivateKey: ca.PrivateKey,
}, nil
}
func (ctx *DevProxy) prepareMITMCertificate(hosts []string) (*tls.Certificate, error) {
now, err := ctx.Config.NowGetter()
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve the current time")
}
canonicalizedHosts := make([]string, len(hosts))
for i, host := range hosts {
canonicalizedHosts[i] = strings.TrimRight(strings.ToLower(host), ".")
}
sortedHosts := make([]string, len(hosts))
copy(sortedHosts, canonicalizedHosts)
sort.Strings(sortedHosts)
cert, err := ctx.certCache.Get(sortedHosts, now)
if err != nil {
return nil, errors.Wrapf(err, "failed to reetrieve a certificate that corresponds to [%v] from the cert cache", sortedHosts)
}
if cert != nil {
ctx.Logger.Infof("Obtained certificate from cache")
return cert, nil
}
// try prepared
cert, ok, err := ctx.searchPreparedCertificate(sortedHosts, now)
if err != nil {
return nil, err
}
if !ok {
cert, err = ctx.generateMITMCertificate(canonicalizedHosts[0], sortedHosts, now)
if err != nil {
return nil, err
}
}
if cert == nil {
return nil, errors.Wrapf(err, "failed to prepare MITM certificates")
}
err = ctx.certCache.Put(sortedHosts, cert)
if err != nil {
return nil, errors.Wrapf(err, "failed to store the certificate to the cache")
}
return cert, nil
}
func (ctx *DevProxy) checkIfRequestMatchesToUrl(url_ *url.URL, secureOnly bool, req *http.Request, _ *OurProxyCtx) bool {
if req.Method == "CONNECT" {
return false
}
if secureOnly && url_.Scheme != "https" {
return false
}
if req.URL.Scheme != url_.Scheme {
return false
}
reqPairs := toHostPortPairs(req.URL)
givenPairs := toHostPortPairs(url_)
for _, a := range reqPairs {
for _, b := range givenPairs {
if a.Host == b.Host && a.Port == b.Port {
return true
}
}
}
return false
}
func (ctx *DevProxy) checkIfTunnelRequestMatchesToUrl(url_ *url.URL, req *http.Request, _ *OurProxyCtx) bool {
if req.Method != "CONNECT" || req.URL.Scheme != "" {
return false
}
if url_.Scheme != "https" {
return false
}
a := splitHostPort(req.Host)
if a.Port == "" {
ctx.Logger.Warnf("invalid request")
return false
}
for _, b := range toHostPortPairs(url_) {
if a.Host == b.Host && a.Port == b.Port {
return true
}
}
return false
}
func (ctx *DevProxy) newProxyHttpServer() *OurProxyHttpServer {
return &OurProxyHttpServer{
Ctx: ctx,
Logger: ctx.Logger,
Tr: ctx.newHttpTransport(),
TLSConfigFactory: ctx.newTLSConfigFactory(),
ResponseFilters: ctx.Config.ResponseFilters,
SessionSerial: 0,
}
}
func (ctx *DevProxy) Dispose() {
ctx.LogWriter.Close()
}
func main() {
var listenOn string
var verbose bool
progname := filepath.Base(os.Args[0])
flag.StringVar(&listenOn, "l", ":8080", "\"addr:port\" on which the server listens")
flag.BoolVar(&verbose, "v", false, "verbose output")
flag.Parse()
args := flag.Args()
var config *Config
var err error
if len(args) > 0 {
config, err = loadConfig(args[0], progname)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", progname, err.Error())
os.Exit(1)
}
if len(config.Hosts) == 0 {
fmt.Fprintf(os.Stderr, "%s: warning: no patterns defined\n", progname)
}
} else {
config = &Config{}
}
logger := &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{
ForceColors: false,
DisableColors: false,
DisableTimestamp: false,
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
DisableSorting: false,
},
Level: logrus.InfoLevel,
}
if verbose {
logger.Level = logrus.DebugLevel
}
cacheDir := ""
if !config.MITM.DisableCache {
if config.MITM.CacheDirectory == "" {
c := configdir.New("github.com/moriyoshi", progname).QueryCacheFolder()
if c != nil {
cacheDir = c.Path
}
} else {
cacheDir = config.MITM.CacheDirectory
}
}
logWriter := logger.Writer()
ctx := DevProxy{
Logger: logger,
LogWriter: logWriter,
StdLogger: log.New(logWriter, "", 0),
Config: config,
DefaultCharset: "UTF-8",
CryptoRandReader: crand.Reader,
certCache: NewCertCache(
cacheDir,
logger,
config.MITM.SigningCertificateKeyPair.Certificate,
config.MITM.SigningCertificateKeyPair.PrivateKey,
),
}
defer ctx.Dispose()
proxy := ctx.newProxyHttpServer()
logger.Infof("Listening on %s...", listenOn)
logger.Fatal(http.ListenAndServe(listenOn, proxy))
}