-
Notifications
You must be signed in to change notification settings - Fork 34
/
ssl_acme_request.go
210 lines (177 loc) · 4.66 KB
/
ssl_acme_request.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
package teaconfigs
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"errors"
"github.com/go-acme/lego/acme"
"github.com/go-acme/lego/certcrypto"
"github.com/go-acme/lego/certificate"
"github.com/go-acme/lego/challenge/dns01"
"github.com/go-acme/lego/lego"
"github.com/go-acme/lego/log"
"github.com/go-acme/lego/registration"
"github.com/iwind/TeaGo/utils/time"
"io/ioutil"
golog "log"
"strings"
)
// ACME DNS Request
type ACMERequest struct {
User *ACMELocalUser `yaml:"user" json:"user"`
Domains []string `yaml:"domains" json:"domains"`
DNSRecords []*ACMEDNSRecord `yaml:"dnsRecords" json:"dnsRecords"`
CertURL string `yaml:"certURL" json:"certURL"`
Cert string `yaml:"cert" json:"cert"`
Key string `yaml:"key" json:"key"`
}
// 获取新对象
func NewACMERequest() *ACMERequest {
return &ACMERequest{}
}
// 获取连接客户端
func (this *ACMERequest) Client() (client *lego.Client, err error) {
log.Logger.(*golog.Logger).SetOutput(ioutil.Discard)
if len(this.User.Email) == 0 {
return nil, errors.New("user email should not be empty")
}
if len(this.User.Key) == 0 {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
privateKeyData, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
this.User.Key = base64.StdEncoding.EncodeToString(privateKeyData)
}
userKeyBytes, err := base64.StdEncoding.DecodeString(this.User.Key)
if err != nil {
return nil, err
}
userKey, err := x509.ParsePKCS8PrivateKey(userKeyBytes)
if err != nil {
return nil, err
}
user := ACMEUser{
Email: this.User.Email,
Key: userKey,
}
if len(this.User.URI) > 0 {
user.Registration = ®istration.Resource{
Body: acme.Account{
Status: "valid",
Contact: []string{"mailto:" + this.User.Email,},
TermsOfServiceAgreed: true,
},
URI: this.User.URI,
}
}
config := lego.NewConfig(&user)
config.Certificate.KeyType = certcrypto.RSA2048
client, err = lego.NewClient(config)
if err != nil {
return
}
err = client.Challenge.SetDNS01Provider(NewACMEDNSProvider("teaweb"), dns01.WrapPreCheck(func(domain, fqdn, value string, check dns01.PreCheckFunc) (b bool, e error) {
b = true
return
}))
if err != nil {
return
}
if len(this.User.URI) == 0 {
reg, err := client.Registration.Register(registration.RegisterOptions{
TermsOfServiceAgreed: true,
})
if err != nil {
return client, err
}
user.Registration = reg
this.User.URI = reg.URI
}
return
}
// 获取要设置的DNS记录
func (this *ACMERequest) RetrieveDNSRecords(client *lego.Client) (records []*ACMEDNSRecord, err error) {
if len(this.Domains) == 0 {
return nil, errors.New("domains should not be empty")
}
recordStrings, err := client.Certificate.GetRecords(certificate.ObtainRequest{
Domains: this.Domains,
Bundle: true,
})
if err != nil {
return nil, err
}
for _, recordString := range recordStrings {
pieces := strings.SplitN(recordString, "|", 2)
if len(pieces) == 2 {
records = append(records, &ACMEDNSRecord{
FQDN: pieces[0],
Value: pieces[1],
})
}
}
return
}
// 获取证书信息
func (this *ACMERequest) Retrieve(client *lego.Client) error {
if len(this.Domains) == 0 {
return errors.New("domains should not be empty")
}
request := certificate.ObtainRequest{
Domains: this.Domains,
Bundle: true,
}
resource, err := client.Certificate.Obtain(request)
if err != nil {
return err
}
this.Cert = string(resource.Certificate)
this.Key = string(resource.PrivateKey)
this.CertURL = resource.CertURL
return nil
}
// 更新
func (this *ACMERequest) Renew(client *lego.Client) error {
return this.Retrieve(client)
}
// 获取证书对象
func (this *ACMERequest) CertObject() (*tls.Certificate, error) {
if len(this.Cert) == 0 || len(this.Key) == 0 {
return nil, nil
}
cert, err := tls.X509KeyPair([]byte(this.Cert), []byte(this.Key))
if err != nil {
return nil, err
}
return &cert, nil
}
// 获取证书时间
func (this *ACMERequest) CertDate() [2]string {
cert, err := this.CertObject()
if err != nil {
return [2]string{}
}
for _, data := range cert.Certificate {
c, err := x509.ParseCertificate(data)
if err != nil {
continue
}
return [2]string{timeutil.Format("Y-m-d", c.NotBefore), timeutil.Format("Y-m-d", c.NotAfter)}
}
return [2]string{}
}
// 写入证书文件
func (this *ACMERequest) WriteCertFile(path string) error {
return ioutil.WriteFile(path, []byte(this.Cert), 0666)
}
// 写入密钥文件
func (this *ACMERequest) WriteKeyFile(path string) error {
return ioutil.WriteFile(path, []byte(this.Key), 0666)
}