2015-06-02 12:39:53 +08:00
|
|
|
// This file is adapted from code in the net/http/httputil
|
|
|
|
// package of the Go standard library, which is by the
|
|
|
|
// Go Authors, and bears this copyright and license info:
|
|
|
|
//
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//
|
|
|
|
// This file has been modified from the standard lib to
|
|
|
|
// meet the needs of the application.
|
2015-05-03 13:20:36 +08:00
|
|
|
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// onExitFlushLoop is a callback set by tests to detect the state of the
|
|
|
|
// flushLoop() goroutine.
|
|
|
|
var onExitFlushLoop func()
|
|
|
|
|
|
|
|
// ReverseProxy is an HTTP Handler that takes an incoming request and
|
|
|
|
// sends it to another server, proxying the response back to the
|
|
|
|
// client.
|
|
|
|
type ReverseProxy struct {
|
|
|
|
// Director must be a function which modifies
|
|
|
|
// the request into a new request to be sent
|
|
|
|
// using Transport. Its response is then copied
|
|
|
|
// back to the original client unmodified.
|
|
|
|
Director func(*http.Request)
|
|
|
|
|
|
|
|
// The transport used to perform proxy requests.
|
|
|
|
// If nil, http.DefaultTransport is used.
|
|
|
|
Transport http.RoundTripper
|
|
|
|
|
|
|
|
// FlushInterval specifies the flush interval
|
|
|
|
// to flush to the client while copying the
|
|
|
|
// response body.
|
|
|
|
// If zero, no periodic flushing is done.
|
|
|
|
FlushInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func singleJoiningSlash(a, b string) string {
|
|
|
|
aslash := strings.HasSuffix(a, "/")
|
|
|
|
bslash := strings.HasPrefix(b, "/")
|
|
|
|
switch {
|
|
|
|
case aslash && bslash:
|
|
|
|
return a + b[1:]
|
|
|
|
case !aslash && !bslash:
|
|
|
|
return a + "/" + b
|
|
|
|
}
|
|
|
|
return a + b
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSingleHostReverseProxy returns a new ReverseProxy that rewrites
|
|
|
|
// URLs to the scheme, host, and base path provided in target. If the
|
|
|
|
// target's path is "/base" and the incoming request was for "/dir",
|
|
|
|
// the target request will be for /base/dir.
|
2015-06-04 02:06:24 +08:00
|
|
|
// Without logic: target's path is "/", incoming is "/api/messages",
|
|
|
|
// without is "/api", then the target request will be for /messages.
|
|
|
|
func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy {
|
2015-05-03 13:20:36 +08:00
|
|
|
targetQuery := target.RawQuery
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL.Scheme = target.Scheme
|
|
|
|
req.URL.Host = target.Host
|
|
|
|
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
|
|
|
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
|
|
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
|
|
|
} else {
|
|
|
|
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
|
|
|
}
|
2015-06-04 02:06:24 +08:00
|
|
|
if without != "" {
|
2015-06-04 21:57:39 +08:00
|
|
|
req.URL.Path = strings.TrimPrefix(req.URL.Path, without)
|
2015-06-04 02:06:24 +08:00
|
|
|
}
|
2015-05-03 13:20:36 +08:00
|
|
|
}
|
|
|
|
return &ReverseProxy{Director: director}
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyHeader(dst, src http.Header) {
|
|
|
|
for k, vv := range src {
|
|
|
|
for _, v := range vv {
|
|
|
|
dst.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hop-by-hop headers. These are removed when sent to the backend.
|
|
|
|
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
|
|
|
|
var hopHeaders = []string{
|
|
|
|
"Connection",
|
|
|
|
"Keep-Alive",
|
|
|
|
"Proxy-Authenticate",
|
|
|
|
"Proxy-Authorization",
|
|
|
|
"Te", // canonicalized version of "TE"
|
|
|
|
"Trailers",
|
|
|
|
"Transfer-Encoding",
|
|
|
|
"Upgrade",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, extraHeaders http.Header) error {
|
|
|
|
transport := p.Transport
|
|
|
|
if transport == nil {
|
|
|
|
transport = http.DefaultTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
outreq := new(http.Request)
|
|
|
|
*outreq = *req // includes shallow copies of maps, but okay
|
|
|
|
|
|
|
|
p.Director(outreq)
|
|
|
|
outreq.Proto = "HTTP/1.1"
|
|
|
|
outreq.ProtoMajor = 1
|
|
|
|
outreq.ProtoMinor = 1
|
|
|
|
outreq.Close = false
|
|
|
|
|
|
|
|
// Remove hop-by-hop headers to the backend. Especially
|
|
|
|
// important is "Connection" because we want a persistent
|
|
|
|
// connection, regardless of what the client sent to us. This
|
|
|
|
// is modifying the same underlying map from req (shallow
|
|
|
|
// copied above) so we only copy it if necessary.
|
|
|
|
copiedHeaders := false
|
|
|
|
for _, h := range hopHeaders {
|
|
|
|
if outreq.Header.Get(h) != "" {
|
|
|
|
if !copiedHeaders {
|
|
|
|
outreq.Header = make(http.Header)
|
|
|
|
copyHeader(outreq.Header, req.Header)
|
|
|
|
copiedHeaders = true
|
|
|
|
}
|
|
|
|
outreq.Header.Del(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
|
|
|
// If we aren't the first proxy retain prior
|
|
|
|
// X-Forwarded-For information as a comma+space
|
|
|
|
// separated list and fold multiple headers into one.
|
|
|
|
if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
|
|
|
|
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
|
|
|
}
|
|
|
|
outreq.Header.Set("X-Forwarded-For", clientIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if extraHeaders != nil {
|
|
|
|
for k, v := range extraHeaders {
|
|
|
|
outreq.Header[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := transport.RoundTrip(outreq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2015-06-02 10:29:32 +08:00
|
|
|
if res.StatusCode == http.StatusSwitchingProtocols && res.Header.Get("Upgrade") == "websocket" {
|
2015-05-30 10:21:50 +08:00
|
|
|
hj, ok := rw.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, _, err := hj.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-02 12:39:53 +08:00
|
|
|
defer conn.Close()
|
2015-05-30 10:21:50 +08:00
|
|
|
|
|
|
|
backendConn, err := net.Dial("tcp", outreq.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-02 12:39:53 +08:00
|
|
|
defer backendConn.Close()
|
2015-05-30 10:21:50 +08:00
|
|
|
|
|
|
|
outreq.Write(backendConn)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
io.Copy(backendConn, conn) // write tcp stream to backend.
|
|
|
|
}()
|
|
|
|
io.Copy(conn, backendConn) // read tcp stream from backend.
|
|
|
|
} else {
|
2015-06-02 01:23:57 +08:00
|
|
|
for _, h := range hopHeaders {
|
|
|
|
res.Header.Del(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
copyHeader(rw.Header(), res.Header)
|
|
|
|
|
2015-05-30 10:21:50 +08:00
|
|
|
rw.WriteHeader(res.StatusCode)
|
|
|
|
p.copyResponse(rw, res.Body)
|
|
|
|
}
|
|
|
|
|
2015-05-03 13:20:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
|
|
|
|
if p.FlushInterval != 0 {
|
|
|
|
if wf, ok := dst.(writeFlusher); ok {
|
|
|
|
mlw := &maxLatencyWriter{
|
|
|
|
dst: wf,
|
|
|
|
latency: p.FlushInterval,
|
|
|
|
done: make(chan bool),
|
|
|
|
}
|
|
|
|
go mlw.flushLoop()
|
|
|
|
defer mlw.stop()
|
|
|
|
dst = mlw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(dst, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeFlusher interface {
|
|
|
|
io.Writer
|
|
|
|
http.Flusher
|
|
|
|
}
|
|
|
|
|
|
|
|
type maxLatencyWriter struct {
|
|
|
|
dst writeFlusher
|
|
|
|
latency time.Duration
|
|
|
|
|
|
|
|
lk sync.Mutex // protects Write + Flush
|
|
|
|
done chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maxLatencyWriter) Write(p []byte) (int, error) {
|
|
|
|
m.lk.Lock()
|
|
|
|
defer m.lk.Unlock()
|
|
|
|
return m.dst.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maxLatencyWriter) flushLoop() {
|
|
|
|
t := time.NewTicker(m.latency)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.done:
|
|
|
|
if onExitFlushLoop != nil {
|
|
|
|
onExitFlushLoop()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
m.lk.Lock()
|
|
|
|
m.dst.Flush()
|
|
|
|
m.lk.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *maxLatencyWriter) stop() { m.done <- true }
|