forked from katakonst/go-dns-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (49 loc) · 1.32 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
)
type Config struct {
DNSConfigs map[string]interface{}
CacheExpiration int64
UseOutbound bool
LogLevel string
}
func InitConfig() (Config, error) {
fileName := flag.String("file", "config.json", "config filename")
logLevel := flag.String("log-level", "info", "log level")
expiration := flag.Int64("expiration", -1, "expiration time in seconds")
useOutbound := flag.Bool("use-outbound", false, "use outbound address")
cliConfigs := flag.String("json-config", "", "config in json format")
flag.Parse()
dnsConfigs := make(map[string]interface{})
if *cliConfigs == "" {
var err error
dnsConfigs, err = parseFile(*fileName)
if err != nil {
return Config{}, err
}
} else {
if err := json.Unmarshal([]byte(*cliConfigs), &dnsConfigs); err != nil {
return Config{}, err
}
}
return Config{
DNSConfigs: dnsConfigs,
CacheExpiration: *expiration * 1000000000,
UseOutbound: *useOutbound,
LogLevel: *logLevel,
}, nil
}
func parseFile(filePath string) (map[string]interface{}, error) {
fileContents := make(map[string]interface{})
body, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &fileContents); err != nil {
return nil, err
}
return fileContents, nil
}