/
config.go
129 lines (113 loc) · 3.13 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package config
import (
"errors"
"net"
"github.com/BurntSushi/toml"
)
// DNSSettings described general settings of DNS resolver
type DNSSettings struct {
CustomECS []net.IP `toml:"custom_ecs"`
FallbackNoECS bool `toml:"fallback_no_ecs"`
NoECS bool `toml:"no_ecs"`
UserAgent string `toml:"user_agent"`
NoUserAgent bool `toml:"no_user_agent"`
NoSingleInflight bool `toml:"no_single_inflight"`
}
type typeCustomSpecified struct {
Domain []string `toml:"domain"`
Suffix []string `toml:"suffix"`
}
type typeGeneralConfig struct {
Listen []string `toml:"listen"`
Timeout *uint `toml:"timeout"` // seconds
RoundRobin Selectors `toml:"round_robin"` // default: clock
CacheNoAnswer uint32 `toml:"cache_no_answer"`
NoCache bool `toml:"no_cache"`
DNSSettings
}
type typeUpstreamHTTPS struct {
Host []string `toml:"host"`
Port uint16 `toml:"port"` // default: 443
Hostname string `toml:"hostname"`
Path string `toml:"path"` // default: /dns-query
Google bool `toml:"google"`
Cookie bool `toml:"cookie"`
Weight int32 `toml:"weight"` // default: 1
typeCustomSpecified
DNSSettings
}
type typeUpstreamTLS struct {
Host []string `toml:"host"`
Port uint16 `toml:"port"` // default: 853
Hostname string `toml:"hostname"`
Weight int32 `toml:"weight"` // default: 1
typeCustomSpecified
DNSSettings
}
type typeTraditional struct {
Host []string `toml:"host"`
Port uint16 `toml:"port"` // default: 53
Bootstrap bool `toml:"bootstrap"`
Weight int32 `toml:"weight"` // default: 1
typeCustomSpecified
DNSSettings
}
// Config described user configuration
type Config struct {
ConfigFile string `toml:"-"`
Config typeGeneralConfig `toml:"config"`
HTTPS []typeUpstreamHTTPS `toml:"https"`
TLS []typeUpstreamTLS `toml:"tls"`
Traditional []typeTraditional `toml:"traditional"`
Hosts map[string]map[string][]string `toml:"hosts"`
}
// LoadConfig from configuration file
func LoadConfig(configPath string) (config *Config, err error) {
config = &Config{ConfigFile: configPath}
_, err = toml.DecodeFile(configPath, &config)
if err != nil {
return
}
if len(config.Config.Listen) == 0 {
err = errors.New("no listen address")
return
}
if config.Config.Timeout == nil {
config.Config.Timeout = new(uint)
*config.Config.Timeout = 5
}
if config.Config.RoundRobin == "" {
config.Config.RoundRobin = SelectorClock
}
for index := range config.HTTPS {
https := &config.HTTPS[index]
if https.Port == 0 {
https.Port = 443
}
if https.Path == "" {
https.Path = "/dns-query"
}
if https.Weight < 1 {
https.Weight = 1
}
}
for index := range config.TLS {
tls := &config.TLS[index]
if tls.Port == 0 {
tls.Port = 853
}
if tls.Weight < 1 {
tls.Weight = 1
}
}
for index := range config.Traditional {
traditional := &config.Traditional[index]
if traditional.Port == 0 {
traditional.Port = 53
}
if traditional.Weight < 1 {
traditional.Weight = 1
}
}
return
}