/
config.go
91 lines (80 loc) · 1.97 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
package main
import (
"os"
"sync"
"time"
"gopkg.in/yaml.v2"
)
type RequestConfig struct {
Host string `yaml:"host"`
Domains []string `yaml:"domains"`
Path string `yaml:"path"`
Https bool `yaml:"https"`
}
type Config struct {
fname string
CollectDuration int `yaml:"collect_duration"`
CertificateDomains []string `yaml:"certificate_domains"`
WhoisDomains []string `yaml:"whois_domains"`
ResolveDomains []string `yaml:"resolve_domains"`
RequestDomains []RequestConfig `yaml:"request_domains"`
lock sync.RWMutex
}
func NewConfig(fname string) (*Config, error) {
cfg := &Config{
fname: fname,
CollectDuration: 3600,
CertificateDomains: []string{},
WhoisDomains: []string{},
ResolveDomains: []string{},
RequestDomains: []RequestConfig{},
}
err := cfg.Reload()
return cfg, err
}
func (c *Config) Reload() error {
file, err := os.Open(c.fname)
if err != nil {
return err
}
defer file.Close()
dec := yaml.NewDecoder(file)
cfg := &Config{}
err = dec.Decode(cfg)
if err != nil {
return err
}
c.lock.Lock()
if cfg.CollectDuration >= 60 {
c.CollectDuration = cfg.CollectDuration
}
c.CertificateDomains = cfg.CertificateDomains
c.WhoisDomains = cfg.WhoisDomains
c.ResolveDomains = cfg.ResolveDomains
c.RequestDomains = cfg.RequestDomains
c.lock.Unlock()
return nil
}
func (c *Config) GetCertificateDomains() []string {
c.lock.RLock()
defer c.lock.RUnlock()
return c.CertificateDomains
}
func (c *Config) GetWhoisDomains() []string {
c.lock.RLock()
defer c.lock.RUnlock()
return c.WhoisDomains
}
func (c *Config) GetResolveDomains() []string {
c.lock.RLock()
defer c.lock.RUnlock()
return c.ResolveDomains
}
func (c *Config) GetRequestDomains() []RequestConfig {
c.lock.RLock()
defer c.lock.RUnlock()
return c.RequestDomains
}
func (c *Config) GetDuration() time.Duration {
return time.Second * time.Duration(c.CollectDuration)
}