From cc6a45b1271cbfd6c93b18895d32f6b5dbe8dd75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 14 Jun 2024 22:52:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/requests/cert/cert_store.go | 4 +-- app/http/requests/cert/cert_update.go | 4 +-- app/models/cert.go | 24 ++++++++---------- app/models/cert_dns.go | 16 ++++-------- app/models/cert_user.go | 20 ++++++--------- app/models/cron.go | 20 ++++++--------- app/models/database.go | 20 +++++++-------- app/models/monitor.go | 8 +++--- app/models/plugin.go | 14 +++++------ app/models/setting.go | 10 +++----- app/models/task.go | 16 +++++------- app/models/user.go | 12 ++++----- app/models/website.go | 20 ++++++--------- internal/services/cert.go | 36 +++++++++++++-------------- 14 files changed, 95 insertions(+), 129 deletions(-) diff --git a/app/http/requests/cert/cert_store.go b/app/http/requests/cert/cert_store.go index ba040964..6d3131ec 100644 --- a/app/http/requests/cert/cert_store.go +++ b/app/http/requests/cert/cert_store.go @@ -11,8 +11,8 @@ type CertStore struct { Domains []string `form:"domains" json:"domains"` AutoRenew bool `form:"auto_renew" json:"auto_renew"` UserID uint `form:"user_id" json:"user_id"` - DNSID *uint `form:"dns_id" json:"dns_id"` - WebsiteID *uint `form:"website_id" json:"website_id"` + DNSID uint `form:"dns_id" json:"dns_id"` + WebsiteID uint `form:"website_id" json:"website_id"` } func (r *CertStore) Authorize(ctx http.Context) error { diff --git a/app/http/requests/cert/cert_update.go b/app/http/requests/cert/cert_update.go index 05d9adfc..05bc6307 100644 --- a/app/http/requests/cert/cert_update.go +++ b/app/http/requests/cert/cert_update.go @@ -12,8 +12,8 @@ type CertUpdate struct { Domains []string `form:"domains" json:"domains"` AutoRenew bool `form:"auto_renew" json:"auto_renew"` UserID uint `form:"user_id" json:"user_id"` - DNSID *uint `form:"dns_id" json:"dns_id"` - WebsiteID *uint `form:"website_id" json:"website_id"` + DNSID uint `form:"dns_id" json:"dns_id"` + WebsiteID uint `form:"website_id" json:"website_id"` } func (r *CertUpdate) Authorize(ctx http.Context) error { diff --git a/app/models/cert.go b/app/models/cert.go index d013fd50..bdcaab65 100644 --- a/app/models/cert.go +++ b/app/models/cert.go @@ -1,22 +1,20 @@ package models import ( - "github.com/goravel/framework/support/carbon" + "github.com/goravel/framework/database/orm" ) type Cert struct { - ID uint `gorm:"primaryKey" json:"id"` - UserID uint `gorm:"default:null" json:"user_id"` // 关联的 ACME 用户 ID - WebsiteID *uint `gorm:"default:null" json:"website_id"` // 关联的网站 ID - DNSID *uint `gorm:"column:dns_id;default:null" json:"dns_id"` // 关联的 DNS ID - Type string `gorm:"not null" json:"type"` // 证书类型 (P256, P384, 2048, 4096) - Domains []string `gorm:"type:json;serializer:json" json:"domains"` - AutoRenew bool `gorm:"default:true" json:"auto_renew"` // 自动续签 - CertURL *string `gorm:"default:null" json:"cert_url"` // 证书 URL (续签时使用) - Cert string `gorm:"default:null" json:"cert"` // 证书内容 - Key string `gorm:"default:null" json:"key"` // 私钥内容 - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + UserID uint `gorm:"not null" json:"user_id"` // 关联的 ACME 用户 ID + WebsiteID uint `gorm:"not null" json:"website_id"` // 关联的网站 ID + DNSID uint `gorm:"not null" json:"dns_id"` // 关联的 DNS ID + Type string `gorm:"not null" json:"type"` // 证书类型 (P256, P384, 2048, 4096) + Domains []string `gorm:"not null;serializer:json" json:"domains"` + AutoRenew bool `gorm:"not null" json:"auto_renew"` // 自动续签 + CertURL string `gorm:"not null" json:"cert_url"` // 证书 URL (续签时使用) + Cert string `gorm:"not null" json:"cert"` // 证书内容 + Key string `gorm:"not null" json:"key"` // 私钥内容 Website *Website `gorm:"foreignKey:WebsiteID" json:"website"` User *CertUser `gorm:"foreignKey:UserID" json:"user"` diff --git a/app/models/cert_dns.go b/app/models/cert_dns.go index 64aa9519..74bd269e 100644 --- a/app/models/cert_dns.go +++ b/app/models/cert_dns.go @@ -1,22 +1,16 @@ package models import ( - "github.com/goravel/framework/support/carbon" + "github.com/goravel/framework/database/orm" "github.com/TheTNB/panel/pkg/acme" ) type CertDNS struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"not null" json:"name"` // 备注名称 - Type string `gorm:"not null" json:"type"` // DNS 提供商 (dnspod, aliyun, cloudflare) - Data acme.DNSParam `gorm:"type:json;serializer:json" json:"dns_param"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Name string `gorm:"not null" json:"name"` // 备注名称 + Type string `gorm:"not null" json:"type"` // DNS 提供商 (dnspod, aliyun, cloudflare) + Data acme.DNSParam `gorm:"not null;serializer:json" json:"dns_param"` Certs []*Cert `gorm:"foreignKey:DNSID" json:"-"` } - -func (CertDNS) TableName() string { - return "cert_dns" -} diff --git a/app/models/cert_user.go b/app/models/cert_user.go index e7cc6c20..9a9c881c 100644 --- a/app/models/cert_user.go +++ b/app/models/cert_user.go @@ -1,19 +1,15 @@ package models -import ( - "github.com/goravel/framework/support/carbon" -) +import "github.com/goravel/framework/database/orm" type CertUser struct { - ID uint `gorm:"primaryKey" json:"id"` - Email string `gorm:"not null" json:"email"` - CA string `gorm:"not null" json:"ca"` // CA 提供商 (letsencrypt, zerossl, sslcom, google, buypass) - Kid *string `gorm:"default:null" json:"kid"` - HmacEncoded *string `gorm:"default:null" json:"hmac_encoded"` - PrivateKey string `gorm:"not null" json:"private_key"` - KeyType string `gorm:"not null" json:"key_type"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Email string `gorm:"not null" json:"email"` + CA string `gorm:"not null" json:"ca"` // CA 提供商 (letsencrypt, zerossl, sslcom, google, buypass) + Kid string `gorm:"not null" json:"kid"` + HmacEncoded string `gorm:"not null" json:"hmac_encoded"` + PrivateKey string `gorm:"not null" json:"private_key"` + KeyType string `gorm:"not null" json:"key_type"` Certs []*Cert `gorm:"foreignKey:UserID" json:"-"` } diff --git a/app/models/cron.go b/app/models/cron.go index 859e0e26..5d22f5eb 100644 --- a/app/models/cron.go +++ b/app/models/cron.go @@ -1,17 +1,13 @@ package models -import ( - "github.com/goravel/framework/support/carbon" -) +import "github.com/goravel/framework/database/orm" type Cron struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"not null;unique" json:"name"` - Status bool `gorm:"not null;default:false" json:"status"` - Type string `gorm:"not null" json:"type"` - Time string `gorm:"not null" json:"time"` - Shell string `gorm:"default:''" json:"shell"` - Log string `gorm:"default:''" json:"log"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Name string `gorm:"not null;unique" json:"name"` + Status bool `gorm:"not null" json:"status"` + Type string `gorm:"not null" json:"type"` + Time string `gorm:"not null" json:"time"` + Shell string `gorm:"not null" json:"shell"` + Log string `gorm:"not null" json:"log"` } diff --git a/app/models/database.go b/app/models/database.go index e4176f14..57c3e85e 100644 --- a/app/models/database.go +++ b/app/models/database.go @@ -1,16 +1,14 @@ package models -import "github.com/goravel/framework/support/carbon" +import "github.com/goravel/framework/database/orm" type Database struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"unique;not null" json:"name"` - Type string `gorm:"not null;index" json:"type"` - Host string `gorm:"not null" json:"host"` - Port int `gorm:"not null" json:"port"` - Username string `gorm:"not null" json:"username"` - Password string `gorm:"default:''" json:"password"` - Remark string `gorm:"default:''" json:"remark"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Name string `gorm:"not null;unique" json:"name"` + Type string `gorm:"not null" json:"type"` + Host string `gorm:"not null" json:"host"` + Port int `gorm:"not null" json:"port"` + Username string `gorm:"not null" json:"username"` + Password string `gorm:"not null" json:"password"` + Remark string `gorm:"not null" json:"remark"` } diff --git a/app/models/monitor.go b/app/models/monitor.go index b2101e86..2691afc6 100644 --- a/app/models/monitor.go +++ b/app/models/monitor.go @@ -1,14 +1,12 @@ package models import ( - "github.com/goravel/framework/support/carbon" + "github.com/goravel/framework/database/orm" "github.com/TheTNB/panel/pkg/tools" ) type Monitor struct { - ID uint `gorm:"primaryKey" json:"id"` - Info tools.MonitoringInfo `gorm:"type:json;serializer:json" json:"info"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Info tools.MonitoringInfo `gorm:"not null;serializer:json" json:"info"` } diff --git a/app/models/plugin.go b/app/models/plugin.go index f4177319..87ac7370 100644 --- a/app/models/plugin.go +++ b/app/models/plugin.go @@ -1,13 +1,11 @@ package models -import "github.com/goravel/framework/support/carbon" +import "github.com/goravel/framework/database/orm" type Plugin struct { - ID uint `gorm:"primaryKey" json:"id"` - Slug string `gorm:"unique;not null" json:"slug"` - Version string `gorm:"not null" json:"version"` - Show bool `gorm:"default:false;not null" json:"show"` - ShowOrder int `gorm:"default:0;not null" json:"show_order"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Slug string `gorm:"not null;unique" json:"slug"` + Version string `gorm:"not null" json:"version"` + Show bool `gorm:"not null" json:"show"` + ShowOrder int `gorm:"not null" json:"show_order"` } diff --git a/app/models/setting.go b/app/models/setting.go index 5a01ce95..04081bd2 100644 --- a/app/models/setting.go +++ b/app/models/setting.go @@ -1,6 +1,6 @@ package models -import "github.com/goravel/framework/support/carbon" +import "github.com/goravel/framework/database/orm" const ( SettingKeyName = "name" @@ -17,9 +17,7 @@ const ( ) type Setting struct { - ID uint `gorm:"primaryKey" json:"id"` - Key string `gorm:"unique;not null" json:"key"` - Value string `gorm:"default:''" json:"value"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Key string `gorm:"not null;unique" json:"key"` + Value string `gorm:"not null" json:"value"` } diff --git a/app/models/task.go b/app/models/task.go index 860d31e9..87896048 100644 --- a/app/models/task.go +++ b/app/models/task.go @@ -1,8 +1,6 @@ package models -import ( - "github.com/goravel/framework/support/carbon" -) +import "github.com/goravel/framework/database/orm" const ( TaskStatusWaiting = "waiting" @@ -12,11 +10,9 @@ const ( ) type Task struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"not null" json:"name"` - Status string `gorm:"not null;default:'waiting'" json:"status"` - Shell string `gorm:"default:''" json:"shell"` - Log string `gorm:"default:''" json:"log"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Name string `gorm:"not null;index" json:"name"` + Status string `gorm:"not null;default:'waiting'" json:"status"` + Shell string `gorm:"not null" json:"shell"` + Log string `gorm:"not null" json:"log"` } diff --git a/app/models/user.go b/app/models/user.go index c93a83a0..55095690 100644 --- a/app/models/user.go +++ b/app/models/user.go @@ -1,12 +1,10 @@ package models -import "github.com/goravel/framework/support/carbon" +import "github.com/goravel/framework/database/orm" type User struct { - ID uint `gorm:"primaryKey" json:"id"` - Username string `gorm:"unique;not null" json:"username"` - Password string `gorm:"not null" json:"password"` - Email string `gorm:"default:''" json:"email"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Username string `gorm:"not null;unique" json:"username"` + Password string `gorm:"not null" json:"password"` + Email string `gorm:"not null" json:"email"` } diff --git a/app/models/website.go b/app/models/website.go index 141c3e71..36d69fd8 100644 --- a/app/models/website.go +++ b/app/models/website.go @@ -1,19 +1,15 @@ package models -import ( - "github.com/goravel/framework/support/carbon" -) +import "github.com/goravel/framework/database/orm" type Website struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `json:"name"` - Status bool `gorm:"default:true" json:"status"` - Path string `json:"path"` - Php int `gorm:"default:0;not null;index" json:"php"` - Ssl bool `gorm:"default:false;not null;index" json:"ssl"` - Remark string `gorm:"default:''" json:"remark"` - CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"` - UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"` + orm.Model + Name string `gorm:"not null;unique" json:"name"` + Status bool `gorm:"not null;default:true" json:"status"` + Path string `gorm:"not null" json:"path"` + Php int `gorm:"not null" json:"php"` + Ssl bool `gorm:"not null" json:"ssl"` + Remark string `gorm:"not null" json:"remark"` Cert *Cert `gorm:"foreignKey:WebsiteID" json:"cert"` } diff --git a/internal/services/cert.go b/internal/services/cert.go index a16e41f7..90e88fe1 100644 --- a/internal/services/cert.go +++ b/internal/services/cert.go @@ -28,8 +28,8 @@ func (s *CertImpl) UserStore(request requests.UserStore) error { var user models.CertUser user.CA = request.CA user.Email = request.Email - user.Kid = &request.Kid - user.HmacEncoded = &request.HmacEncoded + user.Kid = request.Kid + user.HmacEncoded = request.HmacEncoded user.KeyType = request.KeyType var err error @@ -40,11 +40,11 @@ func (s *CertImpl) UserStore(request requests.UserStore) error { case "buypass": client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CABuypass, nil, acme.KeyType(user.KeyType)) case "zerossl": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAZeroSSL, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAZeroSSL, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) case "sslcom": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CASSLcom, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CASSLcom, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) case "google": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAGoogle, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAGoogle, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) default: return errors.New("CA 提供商不支持") } @@ -72,8 +72,8 @@ func (s *CertImpl) UserUpdate(request requests.UserUpdate) error { user.CA = request.CA user.Email = request.Email - user.Kid = &request.Kid - user.HmacEncoded = &request.HmacEncoded + user.Kid = request.Kid + user.HmacEncoded = request.HmacEncoded user.KeyType = request.KeyType var client *acme.Client @@ -83,11 +83,11 @@ func (s *CertImpl) UserUpdate(request requests.UserUpdate) error { case "buypass": client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CABuypass, nil, acme.KeyType(user.KeyType)) case "zerossl": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAZeroSSL, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAZeroSSL, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) case "sslcom": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CASSLcom, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CASSLcom, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) case "google": - client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAGoogle, &acme.EAB{KeyID: *user.Kid, MACKey: *user.HmacEncoded}, acme.KeyType(user.KeyType)) + client, err = acme.NewRegisterAccount(context.Background(), user.Email, acme.CAGoogle, &acme.EAB{KeyID: user.Kid, MACKey: user.HmacEncoded}, acme.KeyType(user.KeyType)) default: return errors.New("CA 提供商不支持") } @@ -262,7 +262,7 @@ func (s *CertImpl) ObtainAuto(ID uint) (acme.Certificate, error) { return acme.Certificate{}, err } - cert.CertURL = &ssl.URL + cert.CertURL = ssl.URL cert.Cert = string(ssl.ChainPEM) cert.Key = string(ssl.PrivateKey) err = facades.Orm().Query().Save(&cert) @@ -302,7 +302,7 @@ func (s *CertImpl) ObtainManual(ID uint) (acme.Certificate, error) { return acme.Certificate{}, err } - cert.CertURL = &ssl.URL + cert.CertURL = ssl.URL cert.Cert = string(ssl.ChainPEM) cert.Key = string(ssl.PrivateKey) err = facades.Orm().Query().Save(&cert) @@ -363,7 +363,7 @@ func (s *CertImpl) Renew(ID uint) (acme.Certificate, error) { return acme.Certificate{}, err } - if cert.CertURL == nil { + if cert.CertURL == "" { return acme.Certificate{}, errors.New("该证书没有签发成功,无法续签") } @@ -382,12 +382,12 @@ func (s *CertImpl) Renew(ID uint) (acme.Certificate, error) { } } - ssl, err := client.RenewSSL(context.Background(), *cert.CertURL, cert.Domains, acme.KeyType(cert.Type)) + ssl, err := client.RenewSSL(context.Background(), cert.CertURL, cert.Domains, acme.KeyType(cert.Type)) if err != nil { return acme.Certificate{}, err } - cert.CertURL = &ssl.URL + cert.CertURL = ssl.URL cert.Cert = string(ssl.ChainPEM) cert.Key = string(ssl.PrivateKey) err = facades.Orm().Query().Save(&cert) @@ -451,13 +451,13 @@ func (s *CertImpl) getClient(cert models.Cert) (*acme.Client, error) { ca = acme.CABuypass case "zerossl": ca = acme.CAZeroSSL - eab = &acme.EAB{KeyID: *cert.User.Kid, MACKey: *cert.User.HmacEncoded} + eab = &acme.EAB{KeyID: cert.User.Kid, MACKey: cert.User.HmacEncoded} case "sslcom": ca = acme.CASSLcom - eab = &acme.EAB{KeyID: *cert.User.Kid, MACKey: *cert.User.HmacEncoded} + eab = &acme.EAB{KeyID: cert.User.Kid, MACKey: cert.User.HmacEncoded} case "google": ca = acme.CAGoogle - eab = &acme.EAB{KeyID: *cert.User.Kid, MACKey: *cert.User.HmacEncoded} + eab = &acme.EAB{KeyID: cert.User.Kid, MACKey: cert.User.HmacEncoded} } return acme.NewPrivateKeyAccount(cert.User.Email, cert.User.PrivateKey, ca, eab)