- 新增 ACME客户端功能,支持域名注册和证书申请 - 添加数据库模型和操作,用于存储和管理域名信息 - 实现 API 接口,提供域名注册、获取和分页查询功能 -集成全局错误处理和 panic捕获 - 添加单元测试和集成测试
77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package acme
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"github.com/go-acme/lego/v4/registration"
|
|
)
|
|
|
|
type User struct {
|
|
Email string
|
|
Registration *registration.Resource
|
|
Key crypto.PrivateKey
|
|
}
|
|
|
|
func (u *User) GetEmail() string {
|
|
return u.Email
|
|
}
|
|
|
|
func (u *User) GetRegistration() *registration.Resource {
|
|
return u.Registration
|
|
}
|
|
|
|
func (u *User) GetPrivateKey() crypto.PrivateKey {
|
|
return u.Key
|
|
}
|
|
|
|
func (u *User) ToRegister() *RegisterRes {
|
|
registrationJson, err := json.Marshal(u.Registration)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
privateDER, err := x509.MarshalPKCS8PrivateKey(u.Key)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
privateBase64 := base64.StdEncoding.EncodeToString(privateDER)
|
|
return &RegisterRes{
|
|
Email: u.Email,
|
|
Registration: string(registrationJson),
|
|
PrivateKey: privateBase64,
|
|
}
|
|
}
|
|
|
|
func (u *User) FromRegister(register *RegisterRes) *User {
|
|
u.Email = register.Email
|
|
err := json.Unmarshal([]byte(register.Registration), &u.Registration)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
privateDER, err := base64.StdEncoding.DecodeString(register.PrivateKey)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
key, err := x509.ParsePKCS8PrivateKey(privateDER)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
privateKey, e := key.(*ecdsa.PrivateKey)
|
|
if !e {
|
|
panic(e)
|
|
}
|
|
u.Key = privateKey
|
|
return u
|
|
}
|
|
|
|
type RegisterRes struct {
|
|
Email string
|
|
Registration string
|
|
PrivateKey string
|
|
}
|