-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_single_header.go
More file actions
64 lines (56 loc) · 1.73 KB
/
source_single_header.go
File metadata and controls
64 lines (56 loc) · 1.73 KB
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
package clientip
type singleHeaderPolicy struct {
headerName string
clientIP clientIPPolicy
trustedProxy proxyPolicy
}
type singleHeaderExtractor struct {
policy singleHeaderPolicy
}
func (e singleHeaderExtractor) extract(req requestView, source Source) (Extraction, *extractionFailure) {
headerValues := req.valuesCanonical(e.policy.headerName)
if len(headerValues) == 0 {
return Extraction{}, errSourceUnavailable
}
if len(headerValues) > 1 {
return Extraction{}, &extractionFailure{
kind: failureMultipleHeaders,
source: source,
headerName: e.policy.headerName,
headerCount: len(headerValues),
remoteAddr: req.remoteAddr(),
}
}
headerValue := headerValues[0]
if headerValue == "" {
return Extraction{}, errSourceUnavailable
}
if len(e.policy.trustedProxy.TrustedProxyCIDRs) > 0 {
remoteIP := parseRemoteAddr(req.remoteAddr())
if !isTrustedProxy(remoteIP, e.policy.trustedProxy.TrustedProxyMatch, e.policy.trustedProxy.TrustedProxyCIDRs) {
return Extraction{}, &extractionFailure{
kind: failureUntrustedProxy,
source: source,
headerName: e.policy.headerName,
chain: headerValue,
trustedProxyCount: 0,
minTrustedProxies: e.policy.trustedProxy.MinTrustedProxies,
maxTrustedProxies: e.policy.trustedProxy.MaxTrustedProxies,
}
}
}
ip := parseIP(headerValue)
disposition := evaluateClientIP(ip, e.policy.clientIP)
if disposition != clientIPValid {
return Extraction{}, &extractionFailure{
kind: failureInvalidClientIP,
source: source,
extractedIP: headerValue,
clientIPDisposition: disposition,
}
}
return Extraction{
IP: normalizeIP(ip),
Source: source,
}, nil
}