-
Notifications
You must be signed in to change notification settings - Fork 175
/
forwarding.go
348 lines (317 loc) · 9.42 KB
/
forwarding.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
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
/*
* Copyright 2018 The Trickster Authors
*
* 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 headers
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/trickstercache/trickster/v2/pkg/appinfo"
)
const (
// NameVia represents the HTTP Header Name of "Via"
NameVia = "Via"
// NameForwarded reqresents the HTTP Header Name of "Forwarded"
NameForwarded = "Forwarded"
// NameXForwardedFor represents the HTTP Header Name of "X-Forwarded-For"
NameXForwardedFor = "X-Forwarded-For"
// NameXForwardedServer represents the HTTP Header Name of "X-Forwarded-Server"
NameXForwardedServer = "X-Forwarded-Server"
// NameXForwardedHost represents the HTTP Header Name of "X-Forwarded-Host"
NameXForwardedHost = "X-Forwarded-Host"
// NameXForwardedProto represents the HTTP Header Name of "X-Forwarded-Proto"
NameXForwardedProto = "X-Forwarded-Proto"
)
// Hop describes a collection of data about the forwarded request
// to be used in Via, Forwarded and X-Forwarded-* headers
type Hop struct {
// RemoteAddr is the client address for which the request is being forwarded.
RemoteAddr string
// Host header as received by the proxy
Host string
// Scheme is the protocol scheme requested of the proxy
Scheme string
// Server is an identifier for the server running the Trickster process
Server string
// protocol indicates the HTTP protocol Version in proper format (.eg., "HTTP/1.1")
// requested by the client
Protocol string
// Hops is a list of previous X-Forwarded-For or Forwarded headers to which we can append our hop
Hops Hops
// Via is the previous Via header, we will append ours to it.
Via string
}
// Hops defines a list of Hop References
type Hops []*Hop
// HopHeaders defines a list of headers that Proxies should not pass through
var HopHeaders = []string{
NameAcceptEncoding,
NameConnection,
NameProxyConnection,
NameKeepAlive,
NameProxyAuthenticate,
NameProxyAuthorization,
NameTe,
NameTrailer,
NameTransferEncoding,
NameUpgrade,
NameAcceptEncoding,
}
// ForwardingHeaders defines a list of headers that Proxies use to identify themselves in a request
var ForwardingHeaders = []string{
NameXForwardedFor,
NameXForwardedHost,
NameXForwardedProto,
NameXForwardedServer,
NameForwarded,
NameVia,
}
// MergeRemoveHeaders defines a list of headers that should be removed when Merging time series results
var MergeRemoveHeaders = []string{
NameLastModified,
NameDate,
NameContentLength,
NameContentType,
NameTransferEncoding,
}
var forwardingFuncs = map[string]func(*http.Request, *Hop){
"standard": AddForwarded,
"x": AddXForwarded,
"none": nil,
"both": AddForwardedAndX,
}
// IsValidForwardingType returns true if the input is a valid Forwarding Type name
// Valid names comprise the keys of the forwardingFuncs map
func IsValidForwardingType(input string) bool {
_, ok := forwardingFuncs[input]
return ok
}
// AddForwardingHeaders sets or appends to the forwarding headers to the provided request
func AddForwardingHeaders(r *http.Request, headerType string) {
if r == nil {
return
}
hop := HopsFromRequest(r)
// Now we can safely remove any pre-existing Forwarding headers before we set them fresh
StripClientHeaders(r.Header)
StripForwardingHeaders(r.Header)
SetVia(r, hop)
if f, ok := forwardingFuncs[headerType]; ok && f != nil {
f(r, hop)
}
}
// String returns a "Forwarded" Header value
func (hop *Hop) String(expand ...bool) string {
parts := make([]string, 0, 4)
if hop.Server != "" {
parts = append(parts, fmt.Sprintf("by=%s", formatForwardedAddress(hop.Server)))
}
if hop.RemoteAddr != "" {
parts = append(parts, fmt.Sprintf("for=%s", formatForwardedAddress(hop.RemoteAddr)))
}
if hop.Host != "" {
parts = append(parts, fmt.Sprintf("host=%s", formatForwardedAddress(hop.Host)))
}
if hop.Scheme != "" {
parts = append(parts, fmt.Sprintf("proto=%s", formatForwardedAddress(hop.Scheme)))
}
currentHop := strings.Join(parts, ";")
if (len(expand) == 1 && !expand[0]) || len(hop.Hops) == 0 {
return currentHop
}
l := len(hop.Hops)
parts = make([]string, l+1)
for i := 0; i < l; i++ {
parts[i] = hop.Hops[i].String(false)
}
parts[l] = currentHop
return strings.Join(parts, ", ")
}
// XHeader returns an http.Header containing the "X-Forwarded-*" headers
func (hop *Hop) XHeader() http.Header {
h := make(http.Header)
if hop.Server != "" {
h.Set(NameXForwardedServer, hop.Server)
}
if hop.RemoteAddr != "" {
l := len(hop.Hops)
if l > 0 {
Hops := make([]string, l+1)
for i, hop := range hop.Hops {
Hops[i] = hop.RemoteAddr
}
Hops[l] = hop.RemoteAddr
h.Set(NameXForwardedFor, strings.Join(Hops, ", "))
} else {
h.Set(NameXForwardedFor, hop.RemoteAddr)
}
}
if hop.Host != "" {
h.Set(NameXForwardedHost, hop.Host)
}
if hop.Scheme != "" {
h.Set(NameXForwardedProto, hop.Scheme)
}
return h
}
// AddForwarded sets or appends to the standard Forwarded header to the provided request
func AddForwarded(r *http.Request, hop *Hop) {
r.Header.Set(NameForwarded, hop.String())
}
// AddXForwarded sets or appends to the "X-Forwarded-*" headers to the provided request
func AddXForwarded(r *http.Request, hop *Hop) {
h := hop.XHeader()
Merge(r.Header, h)
}
// AddForwardedAndX sets or appends to the to the "X-Forwarded-*" headers
// headers, and to the standard Forwarded header to the provided request
func AddForwardedAndX(r *http.Request, hop *Hop) {
h := hop.XHeader()
h.Set(NameForwarded, hop.String())
Merge(r.Header, h)
}
// SetVia sets the "Via" header to the provided request
func SetVia(r *http.Request, hop *Hop) {
if r == nil || r.Header == nil || hop == nil {
return
}
if hop.Via != "" {
r.Header.Set(NameVia, hop.Via+", "+hop.Protocol+" "+appinfo.Server)
} else {
r.Header.Set(NameVia, hop.Protocol+" "+appinfo.Server)
}
}
// HopsFromRequest extracts a Hop reference that includes a list of any previous hops
func HopsFromRequest(r *http.Request) *Hop {
clientIP, _, _ := net.SplitHostPort(r.RemoteAddr)
hop := &Hop{
RemoteAddr: clientIP,
Scheme: r.URL.Scheme,
Protocol: r.Proto,
}
if r.Header == nil {
return hop
}
hop.Via = r.Header.Get(NameVia)
hop.Hops = HopsFromHeader(r.Header)
return hop
}
// HopsFromHeader extracts a hop from the header
func HopsFromHeader(h http.Header) Hops {
if _, ok := h[NameForwarded]; ok {
return parseForwardHeaders(h)
} else if _, ok := h[NameXForwardedFor]; ok {
return parseXForwardHeaders(h)
}
return nil
}
func parseForwardHeaders(h http.Header) Hops {
var hops Hops
fh := h.Get(NameForwarded)
if fh != "" {
fwds := strings.Split(strings.Replace(fh, " ", "", -1), ",")
hops = make(Hops, 0, len(fwds))
for _, f := range fwds {
hop := &Hop{}
var ok bool
parts := strings.Split(f, ";")
for _, p := range parts {
subparts := strings.Split(p, "=")
if len(subparts) == 2 {
switch subparts[0] {
case "for":
hop.RemoteAddr = subparts[1]
ok = true
case "by":
hop.Server = subparts[1]
case "proto":
hop.Protocol = subparts[1]
case "host":
hop.Host = subparts[1]
}
}
}
if ok {
hop.normalizeAddresses()
hops = append(hops, hop)
}
}
}
return hops
}
func (hop *Hop) normalizeAddresses() {
hop.Host = normalizeAddress(hop.Host)
hop.RemoteAddr = normalizeAddress(hop.RemoteAddr)
hop.Server = normalizeAddress(hop.Server)
}
const v6LB = `["`
const v6RB = `"]`
func normalizeAddress(input string) string {
input = strings.TrimPrefix(input, v6LB)
input = strings.TrimSuffix(input, v6RB)
return input
}
func formatForwardedAddress(input string) string {
if isV6Address(input) {
input = v6LB + input + v6RB
}
return input
}
func parseXForwardHeaders(h http.Header) Hops {
xff := h.Get(NameXForwardedFor)
if xff != "" {
fwds := strings.Split(strings.Replace(xff, " ", "", -1), ",")
hops := make(Hops, len(fwds))
for i, f := range fwds {
hop := &Hop{RemoteAddr: f}
hop.Host = h.Get(NameXForwardedHost)
hop.Protocol = h.Get(NameXForwardedProto)
hop.Server = h.Get(NameXForwardedServer)
hop.normalizeAddresses()
hops[i] = hop
}
return hops
}
return nil
}
// AddResponseHeaders injects standard Trickster headers into downstream HTTP responses
func AddResponseHeaders(h http.Header) {
// We're read only and a harmless API, so allow all CORS
h.Set(NameAllowOrigin, "*")
}
// StripClientHeaders strips certain headers from the HTTP request to facililate acceleration
func StripClientHeaders(h http.Header) {
for _, k := range HopHeaders {
h.Del(k)
}
}
// StripForwardingHeaders strips certain headers from the HTTP request to facililate acceleration
func StripForwardingHeaders(h http.Header) {
for _, k := range ForwardingHeaders {
h.Del(k)
}
}
func isV6Address(input string) bool {
ip := net.ParseIP(input)
return ip != nil && strings.Contains(input, ":")
}
// StripMergeHeaders strips certain headers from the HTTP request to facililate acceleration when
// merging HTTP responses from multiple origins
func StripMergeHeaders(h http.Header) {
for _, k := range MergeRemoveHeaders {
h.Del(k)
}
}