forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acme.go
54 lines (51 loc) · 1.35 KB
/
acme.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
package main
import (
"encoding/base64"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)
func (a *goBlog) getAutocertManager() *autocert.Manager {
if a.tailscaleEnabled() || !a.cfg.Server.PublicHTTPS {
return nil
}
if a.autocertManager != nil {
return a.autocertManager
}
// Not initialized yet
a.autocertInit.Do(func() {
// Create hosts whitelist
hosts := []string{a.cfg.Server.publicHostname}
if shn := a.cfg.Server.shortPublicHostname; shn != "" {
hosts = append(hosts, shn)
}
if mhn := a.cfg.Server.mediaHostname; mhn != "" {
hosts = append(hosts, mhn)
}
// Create autocert manager
acmeDir := acme.LetsEncryptURL
if a.cfg.Server.AcmeDir != "" {
acmeDir = a.cfg.Server.AcmeDir
}
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(hosts...),
Cache: &httpsCache{db: a.db},
Client: &acme.Client{DirectoryURL: acmeDir, HTTPClient: a.httpClient},
}
// Set external account binding
if a.cfg.Server.AcmeEabKid != "" && a.cfg.Server.AcmeEabKey != "" {
key, err := base64.RawURLEncoding.DecodeString(a.cfg.Server.AcmeEabKey)
if err != nil {
return
}
m.ExternalAccountBinding = &acme.ExternalAccountBinding{
KID: a.cfg.Server.AcmeEabKid,
Key: key,
}
}
// Save
a.autocertManager = m
})
// Return
return a.autocertManager
}