|
@@ -1,11 +1,13 @@
|
|
package acme
|
|
package acme
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "bytes"
|
|
"crypto"
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509"
|
|
|
|
+ "encoding/gob"
|
|
"encoding/pem"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
@@ -19,7 +21,41 @@ import (
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
-func newCert(email string, httpsAddress string, domain string) (crypto.PrivateKey, *certificate.Resource, error) {
|
|
|
|
|
|
+func saveAccount(dir string, email string, reg *registration.Resource) error {
|
|
|
|
+ filepath := path.Join(dir, fmt.Sprintf("%s.account", email))
|
|
|
|
+
|
|
|
|
+ var buff bytes.Buffer
|
|
|
|
+ enc := gob.NewEncoder(&buff)
|
|
|
|
+ err := enc.Encode(reg)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return os.WriteFile(filepath, buff.Bytes(), 0644)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func loadAccount(dir string, email string) (*registration.Resource, error) {
|
|
|
|
+ filepath := path.Join(dir, fmt.Sprintf("%s.account", email))
|
|
|
|
+ file, err := os.Open(filepath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ defer func() {
|
|
|
|
+ _ = file.Close()
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ var reg registration.Resource
|
|
|
|
+ dec := gob.NewDecoder(file)
|
|
|
|
+
|
|
|
|
+ err = dec.Decode(®)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ®, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func newCert(dir string, email string, httpsAddress string, domain string) (crypto.PrivateKey, *certificate.Resource, error) {
|
|
privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
|
privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|
|
@@ -49,11 +85,19 @@ func newCert(email string, httpsAddress string, domain string) (crypto.PrivateKe
|
|
TermsOfServiceAgreed: true,
|
|
TermsOfServiceAgreed: true,
|
|
}
|
|
}
|
|
|
|
|
|
- reg, err := client.Registration.Register(regOption)
|
|
|
|
|
|
+ reg, err := loadAccount(path.Join(dir, "account"), email)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, nil, err
|
|
|
|
- }
|
|
|
|
|
|
+ // 尝试注册
|
|
|
|
+ reg, err = client.Registration.Register(regOption)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, err
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ err = saveAccount(dir, email, reg)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, nil, err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
user.setRegistration(reg)
|
|
user.setRegistration(reg)
|
|
|
|
|
|
if domain == "" {
|
|
if domain == "" {
|