forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
63 lines (54 loc) · 1.44 KB
/
util.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
package proxy
import (
"errors"
"log"
"net"
"net/http"
"github.com/eBay/fabio/config"
"github.com/eBay/fabio/route"
)
// addHeaders adds/updates headers in request
//
// * add/update `Forwarded` header
// * ClientIPHeader == "X-Forwarded-For": add/update `X-Forwarded-For` header
// * ClientIPHeader != "": Set header with that name to <remote ip>
// * TLS connection: Set header with name from `cfg.TLSHeader` to `cfg.TLSHeaderValue`
//
func addHeaders(r *http.Request, cfg config.Proxy) error {
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return errors.New("cannot parse " + r.RemoteAddr)
}
if cfg.ClientIPHeader != "" {
r.Header.Set(cfg.ClientIPHeader, remoteIP)
}
xff := r.Header.Get("X-Forwarded-For")
if xff != "" && cfg.LocalIP != "" {
r.Header.Set("X-Forwarded-For", xff+", "+cfg.LocalIP)
}
fwd := r.Header.Get("Forwarded")
if fwd == "" {
fwd = "for=" + remoteIP
if r.TLS != nil {
fwd += "; proto=https"
} else {
fwd += "; proto=http"
}
}
if cfg.LocalIP != "" {
fwd += "; by=" + cfg.LocalIP
}
r.Header.Set("Forwarded", fwd)
if cfg.TLSHeader != "" && r.TLS != nil {
r.Header.Set(cfg.TLSHeader, cfg.TLSHeaderValue)
}
return nil
}
// target looks up a target URL for the request from the current routing table.
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.Host, r.URL)
}
return t
}