-
Notifications
You must be signed in to change notification settings - Fork 7
/
rules.go
54 lines (43 loc) · 1002 Bytes
/
rules.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
package trauth
import (
"net"
"net/http"
"regexp"
"strings"
)
type Exclude struct {
Path string `yaml:"path"`
IPNet string `yaml:"ipnet"`
// "computed" values from configuration parsing
regexPath *regexp.Regexp
ipNet *net.IPNet
}
// Rule defines a trauth rule to exclude authentication
type Rule struct {
Domain string `yaml:"domain"`
Excludes []Exclude `yaml:"excludes"`
}
func skipViaRule(rules []Rule, req *http.Request) bool {
for _, rule := range rules {
// skip processing rules for domains that dont match
if req.Host != rule.Domain {
continue
}
source := net.ParseIP(strings.Split(req.RemoteAddr, ":")[0])
for _, exclude := range rule.Excludes {
// check source ip rules
if source != nil && exclude.ipNet != nil {
if exclude.ipNet.Contains(source) {
return true
}
}
// check path rules
if exclude.regexPath != nil {
if exclude.regexPath.MatchString(req.URL.Path) {
return true
}
}
}
}
return false
}