-
Notifications
You must be signed in to change notification settings - Fork 343
/
forwarded.go
152 lines (113 loc) · 3.08 KB
/
forwarded.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
/*
Package forwarded implements a set of custom predicate to match routes
based on the standardized Forwarded header.
https://datatracker.ietf.org/doc/html/rfc7239
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
Examples:
// only match requests to "example.com"
example1: ForwardedHost("example.com") -> "http://example.org";
// only match requests to http
example2: ForwardedProtocol("http") -> "http://example.org";
// only match requests to https
example3: ForwardedProtocol("https") -> "http://example.org";
*/
package forwarded
import (
"net/http"
"regexp"
"strings"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
const (
// Deprecated, use predicates.ForwardedHostName instead
NameHost = predicates.ForwardedHostName
// Deprecated, use predicates.ForwardedProtocolName instead
NameProto = predicates.ForwardedProtocolName
)
type hostPredicateSpec struct{}
type protoPredicateSpec struct{}
type hostPredicate struct {
host *regexp.Regexp
}
func (p *hostPredicateSpec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) != 1 {
return nil, predicates.ErrInvalidPredicateParameters
}
value, ok := args[0].(string)
if !ok {
return nil, predicates.ErrInvalidPredicateParameters
}
if value == "" {
return nil, predicates.ErrInvalidPredicateParameters
}
re, err := regexp.Compile(value)
if err != nil {
return nil, err
}
return hostPredicate{host: re}, err
}
type protoPredicate struct {
proto string
}
func (p *protoPredicateSpec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) != 1 {
return nil, predicates.ErrInvalidPredicateParameters
}
value, ok := args[0].(string)
if !ok {
return nil, predicates.ErrInvalidPredicateParameters
}
switch value {
case "http", "https":
return protoPredicate{proto: value}, nil
default:
return nil, predicates.ErrInvalidPredicateParameters
}
}
func NewForwardedHost() routing.PredicateSpec { return &hostPredicateSpec{} }
func NewForwardedProto() routing.PredicateSpec { return &protoPredicateSpec{} }
func (p *hostPredicateSpec) Name() string {
return predicates.ForwardedHostName
}
func (p *protoPredicateSpec) Name() string {
return predicates.ForwardedProtocolName
}
func (p hostPredicate) Match(r *http.Request) bool {
fh := r.Header.Get("Forwarded")
if fh == "" {
return false
}
fw := parseForwarded(fh)
return p.host.MatchString(fw.host)
}
func (p protoPredicate) Match(r *http.Request) bool {
fh := r.Header.Get("Forwarded")
if fh == "" {
return false
}
fw := parseForwarded(fh)
return p.proto == fw.proto
}
type forwarded struct {
host string
proto string
}
func parseForwarded(fh string) *forwarded {
f := &forwarded{}
for _, forwardedFull := range strings.Split(fh, ",") {
for _, forwardedPair := range strings.Split(strings.TrimSpace(forwardedFull), ";") {
token, value, found := strings.Cut(forwardedPair, "=")
value = strings.Trim(value, `"`)
if found && value != "" {
switch token {
case "proto":
f.proto = value
case "host":
f.host = value
}
}
}
}
return f
}