-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
99 lines (82 loc) · 2.11 KB
/
config.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
package main
import (
"bytes"
"io/ioutil"
"net"
"net/http"
"regexp"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
networks map[string]*Network
sortedNetworks []*Network
}
func NewConfig(filename string) (*Config, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
networks := make(map[string]*Network)
err = yaml.Unmarshal(content, networks)
if err != nil {
return nil, err
}
c := &Config{
networks: networks,
sortedNetworks: make([]*Network, 0, len(networks)),
}
for networkString, networkConfig := range networks {
_, networkConfig.ipnet, err = net.ParseCIDR(networkString)
if err != nil {
return nil, err
}
networkConfig.serveMux = http.NewServeMux()
networkConfig.nodeDB = NewNodeDB(*updateInterval, networkConfig.Nodes, networkConfig.Graph)
for path, pathConfig := range networkConfig.Routes {
for _, rule := range pathConfig.Rules {
for _, condition := range rule.When {
condition.re, err = regexp.Compile(strings.ToLower(condition.Match))
if err != nil {
return nil, err
}
}
}
networkConfig.serveMux.Handle(path, PathHandler(path, pathConfig, networkConfig.nodeDB))
}
c.sortedNetworks = append(c.sortedNetworks, networkConfig)
}
sort.Sort(ByNetmask(c.sortedNetworks))
return c, nil
}
type Network struct {
Nodes, Graph string
Routes map[string]*PathConfig
ipnet *net.IPNet `yaml:"-"`
nodeDB *NodeDB `yaml:"-"`
serveMux *http.ServeMux `yaml:"-"`
}
type PathConfig struct {
Default string
Rules []*Rule
}
type Rule struct {
When []*Condition
Path string
Careful bool
Disabled bool
}
type ByNetmask []*Network
// Sort ByNetmask largest to smallest
func (a ByNetmask) Len() int { return len(a) }
func (a ByNetmask) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByNetmask) Less(i, j int) bool { return bytes.Compare(a[i].ipnet.IP, a[j].ipnet.IP) > 0 }
func (c *Config) GetServeMux(ip net.IP) *http.ServeMux {
for _, network := range c.sortedNetworks {
if network.ipnet.Contains(ip) {
return network.serveMux
}
}
return nil
}