mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
enhance: show the form error details of CertificateEditor
This commit is contained in:
parent
8581bdd3c6
commit
b0a3989ef4
4 changed files with 166 additions and 128 deletions
|
@ -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{}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
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{}
|
||||
}
|
||||
}
|
||||
}
|
||||
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,
|
||||
}
|
||||
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,
|
||||
}
|
||||
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()
|
||||
err := certModel.Insert()
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
content := &cert.Content{
|
||||
SSLCertificatePath: json.SSLCertificatePath,
|
||||
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
|
||||
SSLCertificate: json.SSLCertificate,
|
||||
SSLCertificateKey: json.SSLCertificateKey,
|
||||
}
|
||||
content := &cert.Content{
|
||||
SSLCertificatePath: json.SSLCertificatePath,
|
||||
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
|
||||
SSLCertificate: json.SSLCertificate,
|
||||
SSLCertificateKey: json.SSLCertificateKey,
|
||||
}
|
||||
|
||||
err = content.WriteFile()
|
||||
err = content.WriteFile()
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
func ModifyCert(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
|
||||
var json certJson
|
||||
var json certJson
|
||||
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
|
||||
q := query.Cert
|
||||
q := query.Cert
|
||||
|
||||
certModel, err := q.FirstByID(id)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
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,
|
||||
})
|
||||
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
|
||||
}
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
content := &cert.Content{
|
||||
SSLCertificatePath: json.SSLCertificatePath,
|
||||
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
|
||||
SSLCertificate: json.SSLCertificate,
|
||||
SSLCertificateKey: json.SSLCertificateKey,
|
||||
}
|
||||
content := &cert.Content{
|
||||
SSLCertificatePath: json.SSLCertificatePath,
|
||||
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
|
||||
SSLCertificate: json.SSLCertificate,
|
||||
SSLCertificateKey: json.SSLCertificateKey,
|
||||
}
|
||||
|
||||
err = content.WriteFile()
|
||||
err = content.WriteFile()
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
GetCert(c)
|
||||
GetCert(c)
|
||||
}
|
||||
|
||||
func RemoveCert(c *gin.Context) {
|
||||
cosy.Core[model.Cert](c).Destroy()
|
||||
cosy.Core[model.Cert](c).Destroy()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue