/
autocert.go
155 lines (134 loc) · 4.32 KB
/
autocert.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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2024, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package autocert
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"github.com/caddyserver/certmagic"
"github.com/libdns/cloudflare"
"github.com/mholt/acmez"
"github.com/mholt/acmez/acme"
"github.com/pkg/errors"
"go.uber.org/atomic"
)
type Autocert struct {
domains []string
ownerEmail []string
cloudflareAPIToken string
privateKey *ecdsa.PrivateKey
certs []acme.Certificate
inProcess *atomic.Bool
prod bool
}
func NewAutocert(domains []string, ownerEmail []string, cloudflareAPIToken string, prod bool) *Autocert {
return &Autocert{
domains: domains,
ownerEmail: ownerEmail,
cloudflareAPIToken: cloudflareAPIToken,
inProcess: atomic.NewBool(false),
prod: prod,
}
}
func (a *Autocert) Start() {}
func (a *Autocert) Shutdown() {}
func (a *Autocert) RequestCertificate(ctx context.Context) (err error) {
if !a.inProcess.CompareAndSwap(false, true) {
return
}
defer a.inProcess.Store(false)
log.Infof("request certificate for %v", a.domains)
//fmt.Println(a.domains, len(a.domains), a.ownerEmail, len(a.ownerEmail), a.cloudflareAPIToken)
// Initialize a DNS-01 solver, using Cloudflare APIs
solver := &certmagic.DNS01Solver{
DNSProvider: &cloudflare.Provider{
APIToken: a.cloudflareAPIToken,
},
}
// The CA endpoint to use (prod or staging)
// switch to Production once fully tested
// otherwise you might get rate-limited in Production
// before you've had a chance to test that your client
// works as expected
caLocation := certmagic.LetsEncryptStagingCA
if a.prod {
caLocation = certmagic.LetsEncryptProductionCA
}
// Initialize an acmez client
client := acmez.Client{
Client: &acme.Client{
Directory: caLocation,
Logger: log.Loggert(),
},
ChallengeSolvers: map[string]acmez.Solver{
acme.ChallengeTypeDNS01: solver,
},
}
// Generate a private key for your Let's Encrypt account
var accountPrivateKey *ecdsa.PrivateKey
accountPrivateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
err = errors.Wrap(err, "could not generate an account key")
return
}
// Create a Let's Encrypt account
account := acme.Account{
Contact: a.ownerEmail,
TermsOfServiceAgreed: true,
PrivateKey: accountPrivateKey,
}
var acc acme.Account
acc, err = client.NewAccount(ctx, account)
if err != nil {
err = errors.Wrap(err, "could not create new account:")
return
}
// Generate a private key for the certificate
a.privateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
err = errors.Wrap(err, "generating certificate key")
return
}
a.certs, err = client.ObtainCertificate(ctx, acc, a.privateKey, a.domains)
if err != nil {
err = errors.Wrap(err, "could not obtain certificate")
return
}
// since the client returns more than one cert, it is up to you
// to choose the most appropriate one (such as one which contains
// the full chain, including any intermediate certificates)
//for _, cert := range a.certs {
// fmt.Println(string(cert.ChainPEM))
//}
log.Info("successful")
return nil
}
func (a *Autocert) PrivateKey() (pemEncoded []byte) {
x509Encoded, _ := x509.MarshalECPrivateKey(a.privateKey)
pemEncoded = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
return
}
func (a *Autocert) PublicKey() (pemEncodedPub []byte) {
for _, cert := range a.certs {
pemEncodedPub = append(pemEncodedPub, cert.ChainPEM...)
}
return
}