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
|
package main
import (
"errors"
"io/ioutil"
"os/user"
"path"
"strings"
"time"
"dim13.org/acme"
"gopkg.in/yaml.v2"
)
const (
defKeySize = 2048
day = time.Hour * 24
week = day * 7
defGrace = week
keyPath = "private"
crtPath = "certs"
)
type Config struct {
Gracetime time.Duration
Listen string
ListenTLS string
BaseDir string
KeySize int
Provider []provider
Hook map[string]string
}
type provider struct {
Directory string
Account []account
}
type account struct {
Mail string
Phone string
KeySize int
KeyFile string
Domain []domain
}
type domain struct {
Altnames []string
KeySize int
KeyFile string
CrtFile string
Webroot string
Hook []string
}
var (
errNoKey = errors.New("no key file specified")
errNoAltNames = errors.New("no altnames specified")
errNoMail = errors.New("no mail specified")
)
func expandHome(p string) (string, error) {
if strings.HasPrefix(p, "~") {
usr, err := user.Current()
if err != nil {
return p, err
}
return path.Join(usr.HomeDir, p[1:]), nil
}
return p, nil
}
func LoadConfig(fname string) (*Config, error) {
conf, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
c := new(Config)
err = yaml.Unmarshal(conf, c)
if err != nil {
return nil, err
}
c.BaseDir, err = expandHome(c.BaseDir)
if err != nil {
return nil, err
}
// apply defaults
if c.Gracetime == 0 {
c.Gracetime = defGrace
}
if c.KeySize == 0 {
c.KeySize = defKeySize
}
for i, pro := range c.Provider {
if pro.Directory == "" {
pro.Directory = acme.LE1
}
c.Provider[i] = pro
for i, acc := range pro.Account {
if acc.KeySize == 0 {
acc.KeySize = c.KeySize
}
if acc.Mail == "" {
return nil, errNoMail
}
if acc.KeyFile == "" {
return nil, errNoKey
}
if c.BaseDir != "" {
acc.KeyFile = path.Join(c.BaseDir, acc.KeyFile)
}
pro.Account[i] = acc
for i, dom := range acc.Domain {
if dom.KeySize == 0 {
dom.KeySize = c.KeySize
}
if len(dom.Altnames) == 0 {
return nil, errNoAltNames
}
dom.Altnames = checkWWW(dom.Altnames)
d := dom.Altnames[0]
if dom.KeyFile == "" {
dom.KeyFile = path.Join(keyPath, replace(d)+".key")
}
if dom.CrtFile == "" {
dom.CrtFile = path.Join(crtPath, replace(d)+".pem")
}
if c.BaseDir != "" {
dom.KeyFile = path.Join(c.BaseDir, dom.KeyFile)
dom.CrtFile = path.Join(c.BaseDir, dom.CrtFile)
}
acc.Domain[i] = dom
}
}
}
return c, nil
}
func replace(s string) string {
return strings.Replace(s, ".", "_", -1)
}
func checkWWW(altnames []string) []string {
ch := make(chan string)
go func(ch chan string, s []string) {
for _, an := range s {
if strings.HasPrefix(an, "www.") {
ch <- an[4:]
}
}
close(ch)
}(ch, altnames)
has := func(s string) bool {
for _, an := range altnames {
if an == s {
return true
}
}
return false
}
for d := range ch {
if !has(d) {
altnames = append(altnames, d)
}
}
return altnames
}
|