/
config.go
270 lines (250 loc) · 6.6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) 2012-2013 Toby DiPasquale.
package main
import (
"bufio"
"errors"
"fmt"
"github.com/codeslinger/log"
"github.com/rcrowley/go-metrics"
"io"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
)
type Config interface {
ListenLocal() *net.TCPAddr
MaxIdleSecs() int
MaxMsgSize() int
ServingDomain() string
SoftwareIdent() string
Metrics() metrics.Registry
Cores() int
}
type config struct {
domain string
ident string
listenAddr *net.TCPAddr
loglevel log.Level
maxIdleSecs int
maxMsgSize int
memStatsRefreshSecs int
registry metrics.Registry
cores int
}
const (
defaultListenAddr = "0.0.0.0:1025"
defaultSoftwareIdent = "Go25"
defaultMemStatsRefreshSecs = 10
defaultLogLevel = log.TRACE
defaultMaxIdleSecs = 120
defaultMaxMsgSize = 16777216
)
// Return the local address on which this SMTP service is to listen.
func (c *config) ListenLocal() *net.TCPAddr {
return c.listenAddr
}
// Return the timeout at which a session is considered idle and should be
// terminated, in seconds.
func (c *config) MaxIdleSecs() int {
return c.maxIdleSecs
}
// Return the maximum size of a message allowed, in bytes.
func (c *config) MaxMsgSize() int {
return c.maxMsgSize
}
// Return the registry of metrics for this server instance.
func (c *config) Metrics() metrics.Registry {
return c.registry
}
// Return the domain from which this SMTP service runs.
func (c *config) ServingDomain() string {
return c.domain
}
// Return the software identification string used in SMTP banners.
func (c *config) SoftwareIdent() string {
return c.ident
}
// Return the number of CPU cores to use.
func (c *config) Cores() int {
return c.cores
}
func (c *config) String() string {
return fmt.Sprintf(
"listen=%s domain=%s ident='%s' log=%s maxidle=%ds maxmsg=%dB statsrefresh=%ds cores=%d",
c.listenAddr,
c.domain,
c.ident,
c.loglevel,
c.maxIdleSecs,
c.maxMsgSize,
c.memStatsRefreshSecs,
c.cores)
}
// Return a configuration record populated from the given file. If the file
// path given is an empty string, the default configuration record will be
// returned.
func LoadConfig(path string) (Config, error) {
c := newConfig()
err := c.setDefaults()
if err != nil {
return nil, err
}
if path != "" {
if err = c.readConfig(path); err != nil {
return nil, err
}
}
log.SetLevel(c.loglevel)
return c, nil
}
func newConfig() *config {
return &config{
registry: metrics.NewRegistry(),
domain: "",
ident: "",
listenAddr: nil,
}
}
func (c *config) setDefaults() (err error) {
if c.domain == "" {
c.domain, err = os.Hostname()
if err != nil {
return
}
}
if c.listenAddr == nil {
err = c.setListenAddr(defaultListenAddr)
if err != nil {
return
}
}
c.ident = defaultSoftwareIdent
c.memStatsRefreshSecs = defaultMemStatsRefreshSecs
c.loglevel = defaultLogLevel
c.maxIdleSecs = defaultMaxIdleSecs
c.maxMsgSize = defaultMaxMsgSize
c.cores = runtime.NumCPU()
metrics.RegisterRuntimeMemStats(c.registry)
go c.memStatsRefresh()
return
}
func (c *config) readConfig(path string) (err error) {
file, err := os.Open(path)
if err != nil {
return
}
rd := bufio.NewReader(file)
idx := 0
for {
line, err := rd.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
log.Error("I/O error reading from file: %s: %v", path, err)
return err
}
idx++
line = strings.Trim(line, "\r\n")
// skip comments and blank lines
if len(strings.Trim(line, " ")) == 0 || line[0] == '#' {
continue
}
if err = c.parseLine(line, idx); err != nil {
return err
}
}
return nil
}
func (c *config) parseLine(line string, idx int) (err error) {
parts := strings.SplitN(line, ":", 2)
directive := strings.Trim(parts[0], " ")
argument := strings.Trim(parts[1], " ")
switch strings.ToLower(directive) {
case "cores":
if len(argument) == 0 {
return errors.New(fmt.Sprintf("line %d: argument to 'cores' cannot be blank", idx))
}
c.cores, err = strconv.Atoi(argument)
if err != nil {
return errors.New(fmt.Sprintf("line %d: malformed integer: %s", idx, argument))
}
case "domain":
if len(argument) == 0 {
return errors.New(fmt.Sprintf("line %d: argument to 'domain' cannot be blank", idx))
}
c.domain = argument
case "ident":
if len(argument) == 0 {
return errors.New(fmt.Sprintf("line %d: argument to 'ident' cannot be blank", idx))
}
c.ident = argument
case "listen":
if err = c.setListenAddr(argument); err != nil {
return errors.New(fmt.Sprintf("line %d: failed to resolve 'listen' address: %v", idx, err))
}
case "loglevel":
if len(argument) == 0 {
return errors.New(fmt.Sprintf("line %d: argument to 'loglevel' cannot be blank", idx))
}
c.loglevel, err = c.parseLogLevel(argument)
if err != nil {
return errors.New(fmt.Sprintf("line %d: unknown log level ('%s'): %v", idx, argument, err))
}
case "maxidle":
c.maxIdleSecs, err = strconv.Atoi(argument)
if err != nil {
return errors.New(fmt.Sprintf("line %d: invalid argument to 'maxidle' ('%s'): %v", idx, argument, err))
}
if c.maxIdleSecs < 1 {
return errors.New(fmt.Sprintf("line %d: 'maxidle' value cannot be <1 second", idx))
}
case "maxmsgsize":
c.maxMsgSize, err = strconv.Atoi(argument)
if err != nil {
return errors.New(fmt.Sprintf("line %d: invalid argument to 'maxmsgsize' ('%s'): %v", idx, argument, err))
}
if c.maxMsgSize < 1 {
return errors.New(fmt.Sprintf("line %d: 'maxmsgsize' value cannot be <1 byte", idx))
}
case "statsrefresh":
c.memStatsRefreshSecs, err = strconv.Atoi(argument)
if err != nil {
return errors.New(fmt.Sprintf("line %d: invalid argument to 'statsrefresh' ('%s'): %v", idx, argument, err))
}
if c.memStatsRefreshSecs < 1 {
return errors.New(fmt.Sprintf("line %d: 'statsrefresh' interval cannot be <1 second", idx))
}
default:
return errors.New(fmt.Sprintf("line %d: unrecognized directive: %s", idx, directive))
}
return nil
}
func (c *config) setListenAddr(addr string) (err error) {
c.listenAddr, err = net.ResolveTCPAddr("tcp", addr)
return
}
func (c *config) memStatsRefresh() {
metrics.CaptureRuntimeMemStats(c.registry, time.Second)
time.Sleep(time.Duration(int64(1e9) * int64(c.memStatsRefreshSecs)))
}
func (c *config) parseLogLevel(s string) (log.Level, error) {
switch strings.ToLower(s) {
case "trace":
return log.TRACE, nil
case "debug":
return log.DEBUG, nil
case "info":
return log.INFO, nil
case "warn":
return log.WARN, nil
case "error":
return log.ERROR, nil
case "critical":
return log.CRITICAL, nil
}
return -1, errors.New("unknown log level")
}