/
xforward.go
55 lines (45 loc) · 1.43 KB
/
xforward.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
package xforward
import (
"github.com/zalando/skipper/filters"
snet "github.com/zalando/skipper/net"
)
const (
// Deprecated, use filters.XforwardName instead
Name = filters.XforwardName
// Deprecated, use filters.XforwardFirstName instead
NameFirst = filters.XforwardFirstName
)
type filter struct {
headers *snet.ForwardedHeaders
}
// New creates a specification for the xforward filter
// that appends the remote IP of the incoming request to the
// X-Forwarded-For header, and sets the X-Forwarded-Host header
// to the value of the incoming request's Host header.
func New() filters.Spec {
return filter{headers: &snet.ForwardedHeaders{For: true, Host: true}}
}
// NewFirst creates a specification for the xforwardFirst filter
// that prepends the remote IP of the incoming request to the
// X-Forwarded-For header, and sets the X-Forwarded-Host header
// to the value of the incoming request's Host header.
func NewFirst() filters.Spec {
return filter{headers: &snet.ForwardedHeaders{PrependFor: true, Host: true}}
}
func (f filter) Name() string {
if f.headers.PrependFor {
return filters.XforwardFirstName
}
return filters.XforwardName
}
func (f filter) CreateFilter([]interface{}) (filters.Filter, error) {
return filter(f), nil
}
func (f filter) Request(ctx filters.FilterContext) {
req := ctx.OriginalRequest()
if req == nil {
req = ctx.Request()
}
f.headers.Set(req)
}
func (filter) Response(filters.FilterContext) {}