-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
67 lines (53 loc) · 1.71 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
package main
import (
"fmt"
"io/ioutil"
"net"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Verbose bool `yaml:"verbose" json:"verbose"`
Interfaces []string `yaml:"interfaces" json:"interfaces"`
ProxyAddress string `yaml:"proxy_address" json:"proxy_address"`
AdminAddress string `yaml:"admin_address" json:"admin_address"`
ProxyProxyConfigs ProxyProxyConfigs `yaml:"proxy_proxy_configs" json:"proxy_proxy_configs"`
}
func NewConfig(path string) (*Config, error) {
c := &Config{}
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
errOut := fmt.Errorf("Error while reading config file %s: %s\n", path, err)
return c, errOut
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
errOut := fmt.Errorf("Error while unmarshalling config file %s: %s\n", path, err)
return c, errOut
}
for name, ppc := range c.ProxyProxyConfigs {
_, ipnet, err := net.ParseCIDR(ppc.InNet)
if err != nil {
errOut := fmt.Errorf("'in_net' (%s) of proxy_proxy_config %s could not be parsed as CIDR: %s\n", ppc.InNet, name, err)
return c, errOut
}
c.ProxyProxyConfigs[name].inNet = ipnet
}
return c, nil
}
type ProxyProxyConfig struct {
RemoteProxy string `yaml:"remote_proxy" json:"remote_proxy"`
Verbose bool `json:"verbose" yaml:"verbose"`
InNet string `yaml:"in_net" json:"in_net"`
inNet *net.IPNet `json:"-" yaml:"-"`
}
type ProxyProxyConfigs map[string]*ProxyProxyConfig
func (configs ProxyProxyConfigs) FindMatch(ips []net.IP) (string, *ProxyProxyConfig) {
for name, config := range configs {
for _, ip := range ips {
if config.inNet.Contains(ip) {
return name, config
}
}
}
return "none", &ProxyProxyConfig{}
}