refactor: refresh 25.04

This commit is contained in:
Jacky 2025-04-19 17:55:17 +08:00
parent 818bf9bcf6
commit b63dbe1e50
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
98 changed files with 5283 additions and 4817 deletions

View file

@ -57,21 +57,25 @@ func Transformer(certModel *model.Cert) (certificate *APICertificate) {
}
func GetCertList(c *gin.Context) {
cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any {
info, _ := cert.GetCertInfo(m.SSLCertificatePath)
return APICertificate{
Cert: m,
CertificateInfo: info,
}
}).PagingList()
cosy.Core[model.Cert](c).SetFussy("name", "domain").
SetTransformer(func(m *model.Cert) any {
info, _ := cert.GetCertInfo(m.SSLCertificatePath)
return APICertificate{
Cert: m,
CertificateInfo: info,
}
}).PagingList()
}
func GetCert(c *gin.Context) {
q := query.Cert
certModel, err := q.FirstByID(cast.ToUint64(c.Param("id")))
id := cast.ToUint64(c.Param("id"))
if contextId, ok := c.Get("id"); ok {
id = cast.ToUint64(contextId)
}
certModel, err := q.FirstByID(id)
if err != nil {
cosy.ErrHandler(c, err)
@ -81,167 +85,128 @@ func GetCert(c *gin.Context) {
c.JSON(http.StatusOK, Transformer(certModel))
}
type certJson struct {
Name string `json:"name" binding:"required"`
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,certificate_path"`
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"`
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,certificate"`
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
KeyType certcrypto.KeyType `json:"key_type" binding:"omitempty,auto_cert_key_type"`
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID uint64 `json:"dns_credential_id"`
ACMEUserID uint64 `json:"acme_user_id"`
SyncNodeIds []uint64 `json:"sync_node_ids"`
RevokeOld bool `json:"revoke_old"`
}
func AddCert(c *gin.Context) {
var json certJson
if !cosy.BindAndValid(c, &json) {
return
}
certModel := &model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
KeyType: json.KeyType,
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
SyncNodeIds: json.SyncNodeIds,
}
err := certModel.Insert()
if err != nil {
cosy.ErrHandler(c, err)
return
}
content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}
err = content.WriteFile()
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Detect and set certificate type
if len(json.SSLCertificate) > 0 {
keyType, err := cert.GetKeyType(json.SSLCertificate)
if err == nil && keyType != "" {
// Set KeyType based on certificate type
switch keyType {
case "2048":
certModel.KeyType = certcrypto.RSA2048
case "3072":
certModel.KeyType = certcrypto.RSA3072
case "4096":
certModel.KeyType = certcrypto.RSA4096
case "P256":
certModel.KeyType = certcrypto.EC256
case "P384":
certModel.KeyType = certcrypto.EC384
cosy.Core[model.Cert](c).
SetValidRules(gin.H{
"name": "omitempty",
"ssl_certificate_path": "required,certificate_path",
"ssl_certificate_key_path": "required,privatekey_path",
"ssl_certificate": "omitempty,certificate",
"ssl_certificate_key": "omitempty,privatekey",
"key_type": "omitempty,auto_cert_key_type",
"challenge_method": "omitempty,oneof=http01 dns01",
"dns_credential_id": "omitempty",
"acme_user_id": "omitempty",
"sync_node_ids": "omitempty",
"must_staple": "omitempty",
"lego_disable_cname_support": "omitempty",
"revoke_old": "omitempty",
}).
BeforeExecuteHook(func(ctx *cosy.Ctx[model.Cert]) {
sslCertificate := ctx.Payload["ssl_certificate"].(string)
// Detect and set certificate type
if sslCertificate != "" {
keyType, err := cert.GetKeyType(sslCertificate)
if err == nil && keyType != "" {
// Set KeyType based on certificate type
switch keyType {
case "2048":
ctx.Model.KeyType = certcrypto.RSA2048
case "3072":
ctx.Model.KeyType = certcrypto.RSA3072
case "4096":
ctx.Model.KeyType = certcrypto.RSA4096
case "P256":
ctx.Model.KeyType = certcrypto.EC256
case "P384":
ctx.Model.KeyType = certcrypto.EC384
}
}
}
// Update certificate model
err = certModel.Updates(&model.Cert{KeyType: certModel.KeyType})
}).
ExecutedHook(func(ctx *cosy.Ctx[model.Cert]) {
content := &cert.Content{
SSLCertificatePath: ctx.Model.SSLCertificatePath,
SSLCertificateKeyPath: ctx.Model.SSLCertificateKeyPath,
SSLCertificate: ctx.Payload["ssl_certificate"].(string),
SSLCertificateKey: ctx.Payload["ssl_certificate_key"].(string),
}
err := content.WriteFile()
if err != nil {
notification.Error("Update Certificate Type Error", err.Error(), nil)
ctx.AbortWithError(err)
return
}
}
}
err = cert.SyncToRemoteServer(certModel)
if err != nil {
notification.Error("Sync Certificate Error", err.Error(), nil)
return
}
c.JSON(http.StatusOK, Transformer(certModel))
err = cert.SyncToRemoteServer(&ctx.Model)
if err != nil {
notification.Error("Sync Certificate Error", err.Error(), nil)
return
}
ctx.Context.Set("id", ctx.Model.ID)
}).
SetNextHandler(GetCert).
Create()
}
func ModifyCert(c *gin.Context) {
id := cast.ToUint64(c.Param("id"))
var json certJson
if !cosy.BindAndValid(c, &json) {
return
}
q := query.Cert
certModel, err := q.FirstByID(id)
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Create update data object
updateData := &model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
ChallengeMethod: json.ChallengeMethod,
KeyType: json.KeyType,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
SyncNodeIds: json.SyncNodeIds,
RevokeOld: json.RevokeOld,
}
content := &cert.Content{
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
SSLCertificate: json.SSLCertificate,
SSLCertificateKey: json.SSLCertificateKey,
}
err = content.WriteFile()
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Detect and set certificate type
if len(json.SSLCertificate) > 0 {
keyType, err := cert.GetKeyType(json.SSLCertificate)
if err == nil && keyType != "" {
// Set KeyType based on certificate type
switch keyType {
case "2048":
updateData.KeyType = certcrypto.RSA2048
case "3072":
updateData.KeyType = certcrypto.RSA3072
case "4096":
updateData.KeyType = certcrypto.RSA4096
case "P256":
updateData.KeyType = certcrypto.EC256
case "P384":
updateData.KeyType = certcrypto.EC384
cosy.Core[model.Cert](c).
SetValidRules(gin.H{
"name": "omitempty",
"ssl_certificate_path": "required,certificate_path",
"ssl_certificate_key_path": "required,privatekey_path",
"ssl_certificate": "omitempty,certificate",
"ssl_certificate_key": "omitempty,privatekey",
"key_type": "omitempty,auto_cert_key_type",
"challenge_method": "omitempty,oneof=http01 dns01",
"dns_credential_id": "omitempty",
"acme_user_id": "omitempty",
"sync_node_ids": "omitempty",
"must_staple": "omitempty",
"lego_disable_cname_support": "omitempty",
"revoke_old": "omitempty",
}).
BeforeExecuteHook(func(ctx *cosy.Ctx[model.Cert]) {
sslCertificate := ctx.Payload["ssl_certificate"].(string)
// Detect and set certificate type
if sslCertificate != "" {
keyType, err := cert.GetKeyType(sslCertificate)
if err == nil && keyType != "" {
// Set KeyType based on certificate type
switch keyType {
case "2048":
ctx.Model.KeyType = certcrypto.RSA2048
case "3072":
ctx.Model.KeyType = certcrypto.RSA3072
case "4096":
ctx.Model.KeyType = certcrypto.RSA4096
case "P256":
ctx.Model.KeyType = certcrypto.EC256
case "P384":
ctx.Model.KeyType = certcrypto.EC384
}
}
}
}).
ExecutedHook(func(ctx *cosy.Ctx[model.Cert]) {
content := &cert.Content{
SSLCertificatePath: ctx.Model.SSLCertificatePath,
SSLCertificateKeyPath: ctx.Model.SSLCertificateKeyPath,
SSLCertificate: ctx.Payload["ssl_certificate"].(string),
SSLCertificateKey: ctx.Payload["ssl_certificate_key"].(string),
}
err := content.WriteFile()
if err != nil {
ctx.AbortWithError(err)
return
}
err = cert.SyncToRemoteServer(&ctx.Model)
if err != nil {
notification.Error("Sync Certificate Error", err.Error(), nil)
return
}
}
}
err = certModel.Updates(updateData)
if err != nil {
cosy.ErrHandler(c, err)
return
}
err = cert.SyncToRemoteServer(certModel)
if err != nil {
notification.Error("Sync Certificate Error", err.Error(), nil)
return
}
GetCert(c)
}).
SetNextHandler(GetCert).
Modify()
}
func RemoveCert(c *gin.Context) {

View file

@ -110,6 +110,12 @@ func Rename(c *gin.Context) {
return
}
b := query.ConfigBackup
_, _ = b.Where(b.FilePath.Eq(origFullPath)).Updates(map[string]interface{}{
"filepath": newFullPath,
"name": json.NewName,
})
if len(json.SyncNodeIds) > 0 {
err = config.SyncRenameOnRemoteServer(origFullPath, newFullPath, json.SyncNodeIds)
if err != nil {