forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
116 lines (97 loc) · 2.62 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
package main
import (
"flag"
"github.com/kylelemons/go-gypsy/yaml"
"github.com/rs/zerolog/log"
"os"
"strconv"
"strings"
)
type RttysConfig struct {
addrDev string
addrUser string
sslCert string
sslKey string
httpUsername string
httpPassword string
token string
fontSize int
whiteList map[string]bool
}
func getConfigOpt(yamlCfg *yaml.File, name string, opt interface{}) {
val, err := yamlCfg.Get(name)
if err != nil {
return
}
switch opt := opt.(type) {
case *string:
*opt = val
case *int:
*opt, _ = strconv.Atoi(val)
}
}
func parseConfig() *RttysConfig {
cfg := &RttysConfig{}
cfg.whiteList = make(map[string]bool)
flag.StringVar(&cfg.addrDev, "addr-dev", ":5912", "address to listen device")
flag.StringVar(&cfg.addrUser, "addr-user", ":5913", "address to listen user")
flag.StringVar(&cfg.sslCert, "ssl-cert", "", "certFile Path")
flag.StringVar(&cfg.sslKey, "ssl-key", "", "keyFile Path")
flag.StringVar(&cfg.httpUsername, "http-username", "", "username for http auth")
flag.StringVar(&cfg.httpPassword, "http-password", "", "password for http auth")
flag.StringVar(&cfg.token, "token", "", "token to use")
conf := flag.String("conf", "./rttys.conf", "config file to load")
genToken := flag.Bool("gen-token", false, "generate token")
whiteList := flag.String("white-list", "", "white list(device IDs separated by spaces or *)")
if *whiteList == "*" {
cfg.whiteList = nil
} else {
for _, id := range strings.Fields(*whiteList) {
cfg.whiteList[id] = true
}
}
flag.Parse()
if *genToken {
genTokenAndExit()
}
yamlCfg, err := yaml.ReadFile(*conf)
if err == nil {
getConfigOpt(yamlCfg, "addr-dev", &cfg.addrDev)
getConfigOpt(yamlCfg, "addr-user", &cfg.addrUser)
getConfigOpt(yamlCfg, "ssl-cert", &cfg.sslCert)
getConfigOpt(yamlCfg, "ssl-key", &cfg.sslKey)
getConfigOpt(yamlCfg, "http-username", &cfg.httpUsername)
getConfigOpt(yamlCfg, "http-password", &cfg.httpPassword)
getConfigOpt(yamlCfg, "token", &cfg.token)
getConfigOpt(yamlCfg, "font-size", &cfg.fontSize)
val, err := yamlCfg.Get("white-list")
if err == nil {
if val == "*" || val == "\"*\"" {
cfg.whiteList = nil
} else {
for _, id := range strings.Fields(val) {
cfg.whiteList[id] = true
}
}
}
}
if cfg.fontSize == 0 {
cfg.fontSize = 16
}
if cfg.fontSize < 12 {
cfg.fontSize = 12
}
if cfg.sslCert != "" && cfg.sslKey != "" {
_, err := os.Lstat(cfg.sslCert)
if err != nil {
log.Error().Msg(err.Error())
cfg.sslCert = ""
}
_, err = os.Lstat(cfg.sslKey)
if err != nil {
log.Error().Msg(err.Error())
cfg.sslKey = ""
}
}
return cfg
}