-
Notifications
You must be signed in to change notification settings - Fork 127
/
rule.go
49 lines (42 loc) · 1.07 KB
/
rule.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
//
// date : 2016-05-13
// author: xjdrew
//
package kone
type Rule struct {
directDomains map[string]bool // always direct connect for proxy domain
patterns []Pattern
}
func (rule *Rule) DirectDomain(domain string) {
logger.Debugf("[rule] add direct domain: %s", domain)
rule.directDomains[domain] = true
}
// match a proxy for target `val`
func (rule *Rule) Proxy(val interface{}) string {
if domain, ok := val.(string); ok {
if rule.directDomains[domain] {
logger.Debugf("[rule match] %v, proxy %q", val, "DIRECT")
return "DIRECT" // direct
}
}
for _, pattern := range rule.patterns {
if pattern.Match(val) {
proxy := pattern.Proxy()
logger.Debugf("[rule match] %v, proxy %s", val, proxy)
return proxy
}
}
logger.Debugf("[rule final] %v, proxy %q", val, "")
return "DIRECT" // direct connect
}
func NewRule(rcs []RuleConfig) *Rule {
rule := &Rule{
directDomains: map[string]bool{},
}
for _, rc := range rcs {
if pattern := CreatePattern(rc); pattern != nil {
rule.patterns = append(rule.patterns, pattern)
}
}
return rule
}