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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package martian
import (
"bufio"
"bytes"
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"sync"
"time"
"github.com/google/martian/v3/log"
"github.com/google/martian/v3/mitm"
"github.com/google/martian/v3/nosigpipe"
"github.com/google/martian/v3/proxyutil"
"github.com/google/martian/v3/trafficshape"
)
var errClose = errors.New("closing connection")
var noop = Noop("martian")
func isCloseable(err error) bool {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
return true
}
switch err {
case io.EOF, io.ErrClosedPipe, errClose:
return true
}
return false
}
// Proxy is an HTTP proxy with support for TLS MITM and customizable behavior.
type Proxy struct {
roundTripper http.RoundTripper
dial func(string, string) (net.Conn, error)
timeout time.Duration
mitm *mitm.Config
proxyURL *url.URL
conns sync.WaitGroup
connsMu sync.Mutex // protects conns.Add/Wait from concurrent access
closing chan bool
reqmod RequestModifier
resmod ResponseModifier
}
// NewProxy returns a new HTTP proxy.
func NewProxy() *Proxy {
proxy := &Proxy{
roundTripper: &http.Transport{
// TODO(adamtanner): This forces the http.Transport to not upgrade requests
// to HTTP/2 in Go 1.6+. Remove this once Martian can support HTTP/2.
TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper),
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: time.Second,
},
timeout: 5 * time.Minute,
closing: make(chan bool),
reqmod: noop,
resmod: noop,
}
proxy.SetDial((&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial)
return proxy
}
// GetRoundTripper gets the http.RoundTripper of the proxy.
func (p *Proxy) GetRoundTripper() http.RoundTripper {
return p.roundTripper
}
// SetRoundTripper sets the http.RoundTripper of the proxy.
func (p *Proxy) SetRoundTripper(rt http.RoundTripper) {
p.roundTripper = rt
if tr, ok := p.roundTripper.(*http.Transport); ok {
tr.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper)
tr.Proxy = http.ProxyURL(p.proxyURL)
tr.Dial = p.dial
}
}
// SetDownstreamProxy sets the proxy that receives requests from the upstream
// proxy.
func (p *Proxy) SetDownstreamProxy(proxyURL *url.URL) {
p.proxyURL = proxyURL
if tr, ok := p.roundTripper.(*http.Transport); ok {
tr.Proxy = http.ProxyURL(p.proxyURL)
}
}
// SetTimeout sets the request timeout of the proxy.
func (p *Proxy) SetTimeout(timeout time.Duration) {
p.timeout = timeout
}
// SetMITM sets the config to use for MITMing of CONNECT requests.
func (p *Proxy) SetMITM(config *mitm.Config) {
p.mitm = config
}
// SetDial sets the dial func used to establish a connection.
func (p *Proxy) SetDial(dial func(string, string) (net.Conn, error)) {
p.dial = func(a, b string) (net.Conn, error) {
c, e := dial(a, b)
nosigpipe.IgnoreSIGPIPE(c)
return c, e
}
if tr, ok := p.roundTripper.(*http.Transport); ok {
tr.Dial = p.dial
}
}
// Close sets the proxy to the closing state so it stops receiving new connections,
// finishes processing any inflight requests, and closes existing connections without
// reading anymore requests from them.
func (p *Proxy) Close() {
log.Infof("martian: closing down proxy")
close(p.closing)
log.Infof("martian: waiting for connections to close")
p.connsMu.Lock()
p.conns.Wait()
p.connsMu.Unlock()
log.Infof("martian: all connections closed")
}
// Closing returns whether the proxy is in the closing state.
func (p *Proxy) Closing() bool {
select {
case <-p.closing:
return true
default:
return false
}
}
// SetRequestModifier sets the request modifier.
func (p *Proxy) SetRequestModifier(reqmod RequestModifier) {
if reqmod == nil {
reqmod = noop
}
p.reqmod = reqmod
}
// SetResponseModifier sets the response modifier.
func (p *Proxy) SetResponseModifier(resmod ResponseModifier) {
if resmod == nil {
resmod = noop
}
p.resmod = resmod
}
// Serve accepts connections from the listener and handles the requests.
func (p *Proxy) Serve(l net.Listener) error {
defer l.Close()
var delay time.Duration
for {
if p.Closing() {
return nil
}
conn, err := l.Accept()
nosigpipe.IgnoreSIGPIPE(conn)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
if delay == 0 {
delay = 5 * time.Millisecond
} else {
delay *= 2
}
if max := time.Second; delay > max {
delay = max
}
log.Debugf("martian: temporary error on accept: %v", err)
time.Sleep(delay)
continue
}
if errors.Is(err, net.ErrClosed) {
log.Debugf("martian: listener closed, returning")
return err
}
log.Errorf("martian: failed to accept: %v", err)
return err
}
delay = 0
log.Debugf("martian: accepted connection from %s", conn.RemoteAddr())
if tconn, ok := conn.(*net.TCPConn); ok {
tconn.SetKeepAlive(true)
tconn.SetKeepAlivePeriod(3 * time.Minute)
}
go p.handleLoop(conn)
}
}
func (p *Proxy) handleLoop(conn net.Conn) {
p.connsMu.Lock()
p.conns.Add(1)
p.connsMu.Unlock()
defer p.conns.Done()
defer conn.Close()
if p.Closing() {
return
}
brw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
s, err := newSession(conn, brw)
if err != nil {
log.Errorf("martian: failed to create session: %v", err)
return
}
ctx, err := withSession(s)
if err != nil {
log.Errorf("martian: failed to create context: %v", err)
return
}
for {
deadline := time.Now().Add(p.timeout)
conn.SetDeadline(deadline)
if err := p.handle(ctx, conn, brw); isCloseable(err) {
log.Debugf("martian: closing connection: %v", conn.RemoteAddr())
return
}
}
}
func (p *Proxy) readRequest(ctx *Context, conn net.Conn, brw *bufio.ReadWriter) (*http.Request, error) {
var req *http.Request
reqc := make(chan *http.Request, 1)
errc := make(chan error, 1)
go func() {
r, err := http.ReadRequest(brw.Reader)
if err != nil {
errc <- err
return
}
reqc <- r
}()
select {
case err := <-errc:
if isCloseable(err) {
log.Debugf("martian: connection closed prematurely: %v", err)
} else {
log.Errorf("martian: failed to read request: %v", err)
}
// TODO: TCPConn.WriteClose() to avoid sending an RST to the client.
return nil, errClose
case req = <-reqc:
case <-p.closing:
return nil, errClose
}
return req, nil
}
func (p *Proxy) handleConnectRequest(ctx *Context, req *http.Request, session *Session, brw *bufio.ReadWriter, conn net.Conn) error {
if err := p.reqmod.ModifyRequest(req); err != nil {
log.Errorf("martian: error modifying CONNECT request: %v", err)
proxyutil.Warning(req.Header, err)
}
if session.Hijacked() {
log.Debugf("martian: connection hijacked by request modifier")
return nil
}
if p.mitm != nil {
log.Debugf("martian: attempting MITM for connection: %s / %s", req.Host, req.URL.String())
res := proxyutil.NewResponse(200, nil, req)
if err := p.resmod.ModifyResponse(res); err != nil {
log.Errorf("martian: error modifying CONNECT response: %v", err)
proxyutil.Warning(res.Header, err)
}
if session.Hijacked() {
log.Infof("martian: connection hijacked by response modifier")
return nil
}
if err := res.Write(brw); err != nil {
log.Errorf("martian: got error while writing response back to client: %v", err)
}
if err := brw.Flush(); err != nil {
log.Errorf("martian: got error while flushing response back to client: %v", err)
}
log.Debugf("martian: completed MITM for connection: %s", req.Host)
b := make([]byte, 1)
if _, err := brw.Read(b); err != nil {
log.Errorf("martian: error peeking message through CONNECT tunnel to determine type: %v", err)
}
// Drain all of the rest of the buffered data.
buf := make([]byte, brw.Reader.Buffered())
brw.Read(buf)
// 22 is the TLS handshake.
// https://tools.ietf.org/html/rfc5246#section-6.2.1
if b[0] == 22 {
// Prepend the previously read data to be read again by
// http.ReadRequest.
tlsconn := tls.Server(&peekedConn{conn, io.MultiReader(bytes.NewReader(b), bytes.NewReader(buf), conn)}, p.mitm.TLSForHost(req.Host))
if err := tlsconn.Handshake(); err != nil {
p.mitm.HandshakeErrorCallback(req, err)
return err
}
if tlsconn.ConnectionState().NegotiatedProtocol == "h2" {
return p.mitm.H2Config().Proxy(p.closing, tlsconn, req.URL)
}
var nconn net.Conn
nconn = tlsconn
// If the original connection is a traffic shaped connection, wrap the tls
// connection inside a traffic shaped connection too.
if ptsconn, ok := conn.(*trafficshape.Conn); ok {
nconn = ptsconn.Listener.GetTrafficShapedConn(tlsconn)
}
brw.Writer.Reset(nconn)
brw.Reader.Reset(nconn)
return p.handle(ctx, nconn, brw)
}
// Prepend the previously read data to be read again by http.ReadRequest.
brw.Reader.Reset(io.MultiReader(bytes.NewReader(b), bytes.NewReader(buf), conn))
return p.handle(ctx, conn, brw)
}
log.Debugf("martian: attempting to establish CONNECT tunnel: %s", req.URL.Host)
res, cconn, cerr := p.connect(req)
if cerr != nil {
log.Errorf("martian: failed to CONNECT: %v", cerr)
res = proxyutil.NewResponse(502, nil, req)
proxyutil.Warning(res.Header, cerr)
if err := p.resmod.ModifyResponse(res); err != nil {
log.Errorf("martian: error modifying CONNECT response: %v", err)
proxyutil.Warning(res.Header, err)
}
if session.Hijacked() {
log.Infof("martian: connection hijacked by response modifier")
return nil
}
if err := res.Write(brw); err != nil {
log.Errorf("martian: got error while writing response back to client: %v", err)
}
err := brw.Flush()
if err != nil {
log.Errorf("martian: got error while flushing response back to client: %v", err)
}
return err
}
defer res.Body.Close()
defer cconn.Close()
if err := p.resmod.ModifyResponse(res); err != nil {
log.Errorf("martian: error modifying CONNECT response: %v", err)
proxyutil.Warning(res.Header, err)
}
if session.Hijacked() {
log.Infof("martian: connection hijacked by response modifier")
return nil
}
res.ContentLength = -1
if err := res.Write(brw); err != nil {
log.Errorf("martian: got error while writing response back to client: %v", err)
}
if err := brw.Flush(); err != nil {
log.Errorf("martian: got error while flushing response back to client: %v", err)
}
cbw := bufio.NewWriter(cconn)
cbr := bufio.NewReader(cconn)
defer cbw.Flush()
copySync := func(w io.Writer, r io.Reader, donec chan<- bool) {
if _, err := io.Copy(w, r); err != nil && err != io.EOF {
log.Errorf("martian: failed to copy CONNECT tunnel: %v", err)
}
log.Debugf("martian: CONNECT tunnel finished copying")
donec <- true
}
donec := make(chan bool, 2)
go copySync(cbw, brw, donec)
go copySync(brw, cbr, donec)
log.Debugf("martian: established CONNECT tunnel, proxying traffic")
<-donec
<-donec
log.Debugf("martian: closed CONNECT tunnel")
return errClose
}
func (p *Proxy) handle(ctx *Context, conn net.Conn, brw *bufio.ReadWriter) error {
log.Debugf("martian: waiting for request: %v", conn.RemoteAddr())
req, err := p.readRequest(ctx, conn, brw)
if err != nil {
return err
}
defer req.Body.Close()
session := ctx.Session()
ctx, err = withSession(session)
if err != nil {
log.Errorf("martian: failed to build new context: %v", err)
return err
}
link(req, ctx)
defer unlink(req)
if tsconn, ok := conn.(*trafficshape.Conn); ok {
wrconn := tsconn.GetWrappedConn()
if sconn, ok := wrconn.(*tls.Conn); ok {
session.MarkSecure()
cs := sconn.ConnectionState()
req.TLS = &cs
}
}
if tconn, ok := conn.(*tls.Conn); ok {
session.MarkSecure()
cs := tconn.ConnectionState()
req.TLS = &cs
}
req.URL.Scheme = "http"
if session.IsSecure() {
log.Infof("martian: forcing HTTPS inside secure session")
req.URL.Scheme = "https"
}
req.RemoteAddr = conn.RemoteAddr().String()
if req.URL.Host == "" {
req.URL.Host = req.Host
}
if req.Method == "CONNECT" {
return p.handleConnectRequest(ctx, req, session, brw, conn)
}
// Not a CONNECT request
if err := p.reqmod.ModifyRequest(req); err != nil {
log.Errorf("martian: error modifying request: %v", err)
proxyutil.Warning(req.Header, err)
}
if session.Hijacked() {
return nil
}
// perform the HTTP roundtrip
res, err := p.roundTrip(ctx, req)
if err != nil {
log.Errorf("martian: failed to round trip: %v", err)
res = proxyutil.NewResponse(502, nil, req)
proxyutil.Warning(res.Header, err)
}
defer res.Body.Close()
// set request to original request manually, res.Request may be changed in transport.
// see https://github.com/google/martian/issues/298
res.Request = req
if err := p.resmod.ModifyResponse(res); err != nil {
log.Errorf("martian: error modifying response: %v", err)
proxyutil.Warning(res.Header, err)
}
if session.Hijacked() {
log.Infof("martian: connection hijacked by response modifier")
return nil
}
var closing error
if req.Close || res.Close || p.Closing() {
log.Debugf("martian: received close request: %v", req.RemoteAddr)
res.Close = true
closing = errClose
}
// check if conn is a traffic shaped connection.
if ptsconn, ok := conn.(*trafficshape.Conn); ok {
ptsconn.Context = &trafficshape.Context{}
// Check if the request URL matches any URLRegex in Shapes. If so, set the connections's Context
// with the required information, so that the Write() method of the Conn has access to it.
for urlregex, buckets := range ptsconn.LocalBuckets {
if match, _ := regexp.MatchString(urlregex, req.URL.String()); match {
if rangeStart := proxyutil.GetRangeStart(res); rangeStart > -1 {
dump, err := httputil.DumpResponse(res, false)
if err != nil {
return err
}
ptsconn.Context = &trafficshape.Context{
Shaping: true,
Buckets: buckets,
GlobalBucket: ptsconn.GlobalBuckets[urlregex],
URLRegex: urlregex,
RangeStart: rangeStart,
ByteOffset: rangeStart,
HeaderLen: int64(len(dump)),
HeaderBytesWritten: 0,
}
// Get the next action to perform, if there.
ptsconn.Context.NextActionInfo = ptsconn.GetNextActionFromByte(rangeStart)
// Check if response lies in a throttled byte range.
ptsconn.Context.ThrottleContext = ptsconn.GetCurrentThrottle(rangeStart)
if ptsconn.Context.ThrottleContext.ThrottleNow {
ptsconn.Context.Buckets.WriteBucket.SetCapacity(
ptsconn.Context.ThrottleContext.Bandwidth)
}
log.Infof(
"trafficshape: Request %s with Range Start: %d matches a Shaping request %s. Enforcing Traffic shaping.",
req.URL, rangeStart, urlregex)
}
break
}
}
}
err = res.Write(brw)
if err != nil {
log.Errorf("martian: got error while writing response back to client: %v", err)
if _, ok := err.(*trafficshape.ErrForceClose); ok {
closing = errClose
}
}
err = brw.Flush()
if err != nil {
log.Errorf("martian: got error while flushing response back to client: %v", err)
if _, ok := err.(*trafficshape.ErrForceClose); ok {
closing = errClose
}
}
return closing
}
// A peekedConn subverts the net.Conn.Read implementation, primarily so that
// sniffed bytes can be transparently prepended.
type peekedConn struct {
net.Conn
r io.Reader
}
// Read allows control over the embedded net.Conn's read data. By using an
// io.MultiReader one can read from a conn, and then replace what they read, to
// be read again.
func (c *peekedConn) Read(buf []byte) (int, error) { return c.r.Read(buf) }
func (p *Proxy) roundTrip(ctx *Context, req *http.Request) (*http.Response, error) {
if ctx.SkippingRoundTrip() {
log.Debugf("martian: skipping round trip")
return proxyutil.NewResponse(200, nil, req), nil
}
return p.roundTripper.RoundTrip(req)
}
func (p *Proxy) connect(req *http.Request) (*http.Response, net.Conn, error) {
if p.proxyURL != nil {
log.Debugf("martian: CONNECT with downstream proxy: %s", p.proxyURL.Host)
conn, err := p.dial("tcp", p.proxyURL.Host)
if err != nil {
return nil, nil, err
}
pbw := bufio.NewWriter(conn)
pbr := bufio.NewReader(conn)
req.Write(pbw)
pbw.Flush()
res, err := http.ReadResponse(pbr, req)
if err != nil {
return nil, nil, err
}
return res, conn, nil
}
log.Debugf("martian: CONNECT to host directly: %s", req.URL.Host)
conn, err := p.dial("tcp", req.URL.Host)
if err != nil {
return nil, nil, err
}
return proxyutil.NewResponse(200, nil, req), conn, nil
}
|