-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f356464
commit f534849
Showing
13 changed files
with
191 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package proxy | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"time" | ||
|
||
"github.com/eBay/fabio/config" | ||
) | ||
|
||
type httpProxy struct { | ||
tr http.RoundTripper | ||
cfg config.Proxy | ||
} | ||
|
||
func newHTTPProxy(tr http.RoundTripper, cfg config.Proxy) http.Handler { | ||
return &httpProxy{tr, cfg} | ||
} | ||
|
||
func (p *httpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
t := target(r) | ||
if t == nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
|
||
if p.cfg.ClientIPHeader != "" { | ||
ip, _, err := net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
http.Error(w, "cannot parse "+r.RemoteAddr, http.StatusInternalServerError) | ||
return | ||
} | ||
r.Header.Set(p.cfg.ClientIPHeader, ip) | ||
} | ||
|
||
if p.cfg.TLSHeader != "" && r.TLS != nil { | ||
r.Header.Set(p.cfg.TLSHeader, p.cfg.TLSHeaderValue) | ||
} | ||
|
||
start := time.Now() | ||
rp := httputil.NewSingleHostReverseProxy(t.URL) | ||
rp.Transport = p.tr | ||
rp.ServeHTTP(w, r) | ||
t.Timer.UpdateSince(start) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package proxy | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
gometrics "github.com/eBay/fabio/_third_party/github.com/rcrowley/go-metrics" | ||
"github.com/eBay/fabio/config" | ||
"github.com/eBay/fabio/route" | ||
) | ||
|
||
// Proxy is a dynamic reverse proxy. | ||
type Proxy struct { | ||
httpProxy http.Handler | ||
wsProxy http.Handler | ||
requests gometrics.Timer | ||
} | ||
|
||
func New(tr http.RoundTripper, cfg config.Proxy) *Proxy { | ||
return &Proxy{ | ||
httpProxy: newHTTPProxy(tr, cfg), | ||
wsProxy: newWSProxy(), | ||
requests: gometrics.GetOrRegisterTimer("requests", gometrics.DefaultRegistry), | ||
} | ||
} | ||
|
||
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if ShuttingDown() { | ||
http.Error(w, "shutting down", http.StatusServiceUnavailable) | ||
return | ||
} | ||
|
||
h := p.httpProxy | ||
if r.Header.Get("Upgrade") == "websocket" { | ||
h = p.wsProxy | ||
} | ||
|
||
start := time.Now() | ||
h.ServeHTTP(w, r) | ||
p.requests.UpdateSince(start) | ||
} | ||
|
||
func target(r *http.Request) *route.Target { | ||
t := route.GetTable().Lookup(r, r.Header.Get("trace")) | ||
if t == nil { | ||
log.Print("[WARN] No route for ", r.URL) | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package route | ||
package proxy | ||
|
||
import "sync/atomic" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package proxy | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/eBay/fabio/_third_party/golang.org/x/net/websocket" | ||
) | ||
|
||
type wsProxy struct{} | ||
|
||
func newWSProxy() http.Handler { | ||
return &wsProxy{} | ||
} | ||
|
||
func (p *wsProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
websocket.Handler(wsHandler).ServeHTTP(w, r) | ||
} | ||
|
||
func wsHandler(client *websocket.Conn) { | ||
r := client.Request() | ||
t := target(r) | ||
if t == nil { | ||
client.WriteClose(http.StatusNotFound) | ||
return | ||
} | ||
|
||
logerr := func(err error) { | ||
if err != nil && err != io.EOF { | ||
log.Printf("[INFO] WS error for %s. %s", r.URL, err) | ||
} | ||
} | ||
|
||
// dial the server | ||
origin := r.Header.Get("Origin") | ||
targetURL := "ws://" + t.URL.Host + r.RequestURI | ||
server, err := websocket.Dial(targetURL, "", origin) | ||
if err != nil { | ||
logerr(err) | ||
return | ||
} | ||
|
||
// send data from client to server | ||
cerr := make(chan error) | ||
go func() { | ||
_, err := io.Copy(server, client) | ||
cerr <- err | ||
}() | ||
|
||
// send data from server to client | ||
serr := make(chan error) | ||
go func() { | ||
_, err = io.Copy(client, server) | ||
serr <- err | ||
}() | ||
|
||
// wait for either server or client to exit | ||
// and then close the other side | ||
start := time.Now() | ||
select { | ||
case err := <-cerr: | ||
logerr(err) | ||
server.Close() | ||
case err := <-serr: | ||
logerr(err) | ||
client.Close() | ||
} | ||
t.Timer.UpdateSince(start) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.