-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: show the form error details of CertificateEditor
- Loading branch information
Showing
4 changed files
with
185 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,174 +1,174 @@ | ||
package certificate | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/api" | ||
"github.com/0xJacky/Nginx-UI/api/cosy" | ||
"github.com/0xJacky/Nginx-UI/internal/cert" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/cast" | ||
"net/http" | ||
"os" | ||
"github.com/0xJacky/Nginx-UI/api" | ||
"github.com/0xJacky/Nginx-UI/api/cosy" | ||
"github.com/0xJacky/Nginx-UI/internal/cert" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/cast" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type APICertificate struct { | ||
*model.Cert | ||
SSLCertificate string `json:"ssl_certificate,omitempty"` | ||
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"` | ||
CertificateInfo *cert.Info `json:"certificate_info,omitempty"` | ||
*model.Cert | ||
SSLCertificate string `json:"ssl_certificate,omitempty"` | ||
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"` | ||
CertificateInfo *cert.Info `json:"certificate_info,omitempty"` | ||
} | ||
|
||
func Transformer(certModel *model.Cert) (certificate *APICertificate) { | ||
var sslCertificationBytes, sslCertificationKeyBytes []byte | ||
var certificateInfo *cert.Info | ||
if certModel.SSLCertificatePath != "" { | ||
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { | ||
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) | ||
if !cert.IsPublicKey(string(sslCertificationBytes)) { | ||
sslCertificationBytes = []byte{} | ||
} | ||
} | ||
|
||
certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) | ||
} | ||
|
||
if certModel.SSLCertificateKeyPath != "" { | ||
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { | ||
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) | ||
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { | ||
sslCertificationKeyBytes = []byte{} | ||
} | ||
} | ||
} | ||
|
||
return &APICertificate{ | ||
Cert: certModel, | ||
SSLCertificate: string(sslCertificationBytes), | ||
SSLCertificateKey: string(sslCertificationKeyBytes), | ||
CertificateInfo: certificateInfo, | ||
} | ||
var sslCertificationBytes, sslCertificationKeyBytes []byte | ||
var certificateInfo *cert.Info | ||
if certModel.SSLCertificatePath != "" { | ||
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { | ||
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) | ||
if !cert.IsPublicKey(string(sslCertificationBytes)) { | ||
sslCertificationBytes = []byte{} | ||
} | ||
} | ||
|
||
certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) | ||
} | ||
|
||
if certModel.SSLCertificateKeyPath != "" { | ||
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { | ||
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) | ||
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { | ||
sslCertificationKeyBytes = []byte{} | ||
} | ||
} | ||
} | ||
|
||
return &APICertificate{ | ||
Cert: certModel, | ||
SSLCertificate: string(sslCertificationBytes), | ||
SSLCertificateKey: string(sslCertificationKeyBytes), | ||
CertificateInfo: certificateInfo, | ||
} | ||
} | ||
|
||
func GetCertList(c *gin.Context) { | ||
cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { | ||
cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { | ||
|
||
info, _ := cert.GetCertInfo(m.SSLCertificatePath) | ||
info, _ := cert.GetCertInfo(m.SSLCertificatePath) | ||
|
||
return APICertificate{ | ||
Cert: m, | ||
CertificateInfo: info, | ||
} | ||
}).PagingList() | ||
return APICertificate{ | ||
Cert: m, | ||
CertificateInfo: info, | ||
} | ||
}).PagingList() | ||
} | ||
|
||
func GetCert(c *gin.Context) { | ||
q := query.Cert | ||
q := query.Cert | ||
|
||
certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) | ||
certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) | ||
|
||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, Transformer(certModel)) | ||
c.JSON(http.StatusOK, Transformer(certModel)) | ||
} | ||
|
||
type certJson struct { | ||
Name string `json:"name"` | ||
SSLCertificatePath string `json:"ssl_certificate_path" binding:"publickey_path"` | ||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"privatekey_path"` | ||
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` | ||
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` | ||
ChallengeMethod string `json:"challenge_method"` | ||
DnsCredentialID int `json:"dns_credential_id"` | ||
Name string `json:"name" binding:"required"` | ||
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"` | ||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"` | ||
SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` | ||
SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` | ||
ChallengeMethod string `json:"challenge_method"` | ||
DnsCredentialID int `json:"dns_credential_id"` | ||
} | ||
|
||
func AddCert(c *gin.Context) { | ||
var json certJson | ||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
certModel := &model.Cert{ | ||
Name: json.Name, | ||
SSLCertificatePath: json.SSLCertificatePath, | ||
SSLCertificateKeyPath: json.SSLCertificateKeyPath, | ||
ChallengeMethod: json.ChallengeMethod, | ||
DnsCredentialID: json.DnsCredentialID, | ||
} | ||
|
||
err := certModel.Insert() | ||
|
||
if err != nil { | ||
api.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 { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, Transformer(certModel)) | ||
var json certJson | ||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
certModel := &model.Cert{ | ||
Name: json.Name, | ||
SSLCertificatePath: json.SSLCertificatePath, | ||
SSLCertificateKeyPath: json.SSLCertificateKeyPath, | ||
ChallengeMethod: json.ChallengeMethod, | ||
DnsCredentialID: json.DnsCredentialID, | ||
} | ||
|
||
err := certModel.Insert() | ||
|
||
if err != nil { | ||
api.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 { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, Transformer(certModel)) | ||
} | ||
|
||
func ModifyCert(c *gin.Context) { | ||
id := cast.ToInt(c.Param("id")) | ||
|
||
var json certJson | ||
|
||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
|
||
q := query.Cert | ||
|
||
certModel, err := q.FirstByID(id) | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
err = certModel.Updates(&model.Cert{ | ||
Name: json.Name, | ||
SSLCertificatePath: json.SSLCertificatePath, | ||
SSLCertificateKeyPath: json.SSLCertificateKeyPath, | ||
ChallengeMethod: json.ChallengeMethod, | ||
DnsCredentialID: json.DnsCredentialID, | ||
}) | ||
|
||
if err != nil { | ||
api.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 { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
GetCert(c) | ||
id := cast.ToInt(c.Param("id")) | ||
|
||
var json certJson | ||
|
||
if !api.BindAndValid(c, &json) { | ||
return | ||
} | ||
|
||
q := query.Cert | ||
|
||
certModel, err := q.FirstByID(id) | ||
if err != nil { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
err = certModel.Updates(&model.Cert{ | ||
Name: json.Name, | ||
SSLCertificatePath: json.SSLCertificatePath, | ||
SSLCertificateKeyPath: json.SSLCertificateKeyPath, | ||
ChallengeMethod: json.ChallengeMethod, | ||
DnsCredentialID: json.DnsCredentialID, | ||
}) | ||
|
||
if err != nil { | ||
api.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 { | ||
api.ErrHandler(c, err) | ||
return | ||
} | ||
|
||
GetCert(c) | ||
} | ||
|
||
func RemoveCert(c *gin.Context) { | ||
cosy.Core[model.Cert](c).Destroy() | ||
cosy.Core[model.Cert](c).Destroy() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.