-
Notifications
You must be signed in to change notification settings - Fork 181
/
config.go
161 lines (140 loc) · 3.95 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"crypto/tls"
"errors"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
type PassConfig struct {
Password string
}
// TLSListenConfig defines configuration options for listening on TLS
type TLSListenConfig struct {
Cert string
Key string
}
// Certificate returns the TLS certificate assicated with this TLSListenConfig
func (conf *TLSListenConfig) Config() (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key)
if err != nil {
return nil, errors.New("tls cert+key: invalid pair")
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, err
}
func (conf *PassConfig) PasswordBytes() []byte {
bytes, err := DecodePassword(conf.Password)
if err != nil {
log.Fatal("decode password error: ", err)
}
return bytes
}
type AccountRegistrationConfig struct {
Enabled bool
EnabledCallbacks []string `yaml:"enabled-callbacks"`
Callbacks struct {
Mailto struct {
Server string
Port int
TLS struct {
Enabled bool
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
ServerName string `yaml:"servername"`
}
Username string
Password string
Sender string
VerifyMessageSubject string `yaml:"verify-message-subject"`
VerifyMessage string `yaml:"verify-message"`
}
}
}
type Config struct {
Network struct {
Name string
}
Server struct {
PassConfig
Password string
Name string
Listen []string
Wslisten string `yaml:"ws-listen"`
TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
CheckIdent bool `yaml:"check-ident"`
Log string
MOTD string
ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
}
Datastore struct {
Path string
}
Registration struct {
Accounts AccountRegistrationConfig
}
Operator map[string]*PassConfig
Limits struct {
NickLen int `yaml:"nicklen"`
ChannelLen int `yaml:"channellen"`
AwayLen int `yaml:"awaylen"`
KickLen int `yaml:"kicklen"`
TopicLen int `yaml:"topiclen"`
WhowasEntries uint `yaml:"whowas-entries"`
}
}
func (conf *Config) Operators() map[Name][]byte {
operators := make(map[Name][]byte)
for name, opConf := range conf.Operator {
operators[NewName(name)] = opConf.PasswordBytes()
}
return operators
}
func (conf *Config) TLSListeners() map[Name]*tls.Config {
tlsListeners := make(map[Name]*tls.Config)
for s, tlsListenersConf := range conf.Server.TLSListeners {
config, err := tlsListenersConf.Config()
if err != nil {
log.Fatal(err)
}
tlsListeners[NewName(s)] = config
}
return tlsListeners
}
func LoadConfig(filename string) (config *Config, err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
// we need this so PasswordBytes returns the correct info
if config.Server.Password != "" {
config.Server.PassConfig.Password = config.Server.Password
}
if config.Network.Name == "" {
return nil, errors.New("Network name missing")
}
if config.Server.Name == "" {
return nil, errors.New("Server name missing")
}
if !IsHostname(config.Server.Name) {
return nil, errors.New("Server name must match the format of a hostname")
}
if config.Datastore.Path == "" {
return nil, errors.New("Datastore path missing")
}
if len(config.Server.Listen) == 0 {
return nil, errors.New("Server listening addresses missing")
}
if config.Limits.NickLen < 1 || config.Limits.ChannelLen < 2 || config.Limits.AwayLen < 1 || config.Limits.TopicLen < 1 || config.Limits.TopicLen < 1 {
return nil, errors.New("Limits aren't setup properly, check them and make them sane")
}
return config, nil
}