forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_headers.go
181 lines (162 loc) · 4.41 KB
/
http_headers.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
package proxy
import (
"crypto/tls"
"errors"
"net"
"net/http"
"strings"
"github.com/fabiolb/fabio/config"
)
// addHeaders adds/updates headers in request
//
// * add/update `Forwarded` header
// * add X-Forwarded-Proto header, if not present
// * add X-Real-Ip, if not present
// * 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, stripPath string) error {
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return errors.New("cannot parse " + r.RemoteAddr)
}
// set configurable ClientIPHeader
// X-Real-Ip is set later and X-Forwarded-For is set
// by the Go HTTP reverse proxy.
if cfg.ClientIPHeader != "" &&
cfg.ClientIPHeader != "X-Forwarded-For" &&
cfg.ClientIPHeader != "X-Real-Ip" {
r.Header.Set(cfg.ClientIPHeader, remoteIP)
}
if r.Header.Get("X-Real-Ip") == "" {
r.Header.Set("X-Real-Ip", remoteIP)
}
// set the X-Forwarded-For header for websocket
// connections since they aren't handled by the
// http proxy which sets it.
ws := r.Header.Get("Upgrade") == "websocket"
if ws {
r.Header.Set("X-Forwarded-For", remoteIP)
}
// Issue #133: Setting the X-Forwarded-Proto header to
// anything other than 'http' or 'https' breaks java
// websocket clients which use java.net.URL for composing
// the forwarded URL. Since X-Forwarded-Proto is not
// specified the common practice is to set it to either
// 'http' for 'ws' and 'https' for 'wss' connections.
proto := scheme(r)
if r.Header.Get("X-Forwarded-Proto") == "" {
switch proto {
case "ws":
r.Header.Set("X-Forwarded-Proto", "http")
case "wss":
r.Header.Set("X-Forwarded-Proto", "https")
default:
r.Header.Set("X-Forwarded-Proto", proto)
}
}
if r.Header.Get("X-Forwarded-Port") == "" {
r.Header.Set("X-Forwarded-Port", localPort(r))
}
if stripPath != "" {
r.Header.Set("X-Forwarded-Prefix", stripPath)
}
fwd := r.Header.Get("Forwarded")
if fwd == "" {
fwd = "for=" + remoteIP + "; proto=" + proto
}
if cfg.LocalIP != "" {
fwd += "; by=" + cfg.LocalIP
}
if r.Proto != "" {
fwd += "; httpproto=" + strings.ToLower(r.Proto)
}
if r.TLS != nil && r.TLS.Version > 0 {
v := tlsver[r.TLS.Version]
if v == "" {
v = uint16base16(r.TLS.Version)
}
fwd += "; tlsver=" + v
}
if r.TLS != nil && r.TLS.CipherSuite != 0 {
fwd += "; tlscipher=" + uint16base16(r.TLS.CipherSuite)
}
r.Header.Set("Forwarded", fwd)
if cfg.TLSHeader != "" {
if r.TLS != nil {
r.Header.Set(cfg.TLSHeader, cfg.TLSHeaderValue)
} else {
r.Header.Del(cfg.TLSHeader)
}
}
return nil
}
var tlsver = map[uint16]string{
tls.VersionSSL30: "ssl30",
tls.VersionTLS10: "tls10",
tls.VersionTLS11: "tls11",
tls.VersionTLS12: "tls12",
}
var digit16 = []byte("0123456789abcdef")
// uint16base64 is a faster version of fmt.Sprintf("0x%04x", n)
//
// BenchmarkUint16Base16/fmt.Sprintf-8 10000000 154 ns/op 8 B/op 2 allocs/op
// BenchmarkUint16Base16/uint16base16-8 50000000 35.0 ns/op 8 B/op 1 allocs/op
func uint16base16(n uint16) string {
b := []byte("0x0000")
b[5] = digit16[n&0x000f]
b[4] = digit16[n&0x00f0>>4]
b[3] = digit16[n&0x0f00>>8]
b[2] = digit16[n&0xf000>>12]
return string(b)
}
// scheme derives the request scheme used on the initial
// request first from headers and then from the connection
// using the following heuristic:
//
// If either X-Forwarded-Proto or Forwarded is set then use
// its value to set the other header. If both headers are
// set do not modify the protocol. If none are set derive
// the protocol from the connection.
func scheme(r *http.Request) string {
xfp := r.Header.Get("X-Forwarded-Proto")
fwd := r.Header.Get("Forwarded")
switch {
case xfp != "" && fwd == "":
return xfp
case fwd != "" && xfp == "":
p := strings.SplitAfterN(fwd, "proto=", 2)
if len(p) == 1 {
break
}
n := strings.IndexRune(p[1], ';')
if n >= 0 {
return p[1][:n]
}
return p[1]
}
ws := r.Header.Get("Upgrade") == "websocket"
switch {
case ws && r.TLS != nil:
return "wss"
case ws && r.TLS == nil:
return "ws"
case r.TLS != nil:
return "https"
default:
return "http"
}
}
func localPort(r *http.Request) string {
if r == nil {
return ""
}
n := strings.Index(r.Host, ":")
if n > 0 && n < len(r.Host)-1 {
return r.Host[n+1:]
}
if r.TLS != nil {
return "443"
}
return "80"
}