mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
feat: change challenge method in certificate management page
This commit is contained in:
parent
002d9c22ab
commit
5b3cd981e9
33 changed files with 1875 additions and 1496 deletions
|
@ -2,166 +2,27 @@ package certificate
|
|||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/api/cosy"
|
||||
"github.com/0xJacky/Nginx-UI/api/sites"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/spf13/cast"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Success = "success"
|
||||
Info = "info"
|
||||
Error = "error"
|
||||
)
|
||||
|
||||
type IssueCertResponse struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
SSLCertificate string `json:"ssl_certificate,omitempty"`
|
||||
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
|
||||
}
|
||||
|
||||
func handleIssueCertLogChan(conn *websocket.Conn, logChan chan string) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
for logString := range logChan {
|
||||
|
||||
err := conn.WriteJSON(IssueCertResponse{
|
||||
Status: Info,
|
||||
Message: logString,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func IssueCert(c *gin.Context) {
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// upgrade http to websocket
|
||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func(ws *websocket.Conn) {
|
||||
_ = ws.Close()
|
||||
}(ws)
|
||||
|
||||
// read
|
||||
buffer := &cert.ConfigPayload{}
|
||||
|
||||
err = ws.ReadJSON(buffer)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
certModel, err := model.FirstOrCreateCert(c.Param("name"))
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logChan := make(chan string, 1)
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go cert.IssueCert(buffer, logChan, errChan)
|
||||
|
||||
go handleIssueCertLogChan(ws, logChan)
|
||||
|
||||
// block, until errChan closes
|
||||
for err = range errChan {
|
||||
errLog := &cert.AutoCertErrorLog{}
|
||||
errLog.SetCertModel(&certModel)
|
||||
errLog.Exit("issue cert", err)
|
||||
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
certDirName := strings.Join(buffer.ServerName, "_")
|
||||
sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
|
||||
sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")
|
||||
|
||||
err = certModel.Updates(&model.Cert{
|
||||
Domains: buffer.ServerName,
|
||||
SSLCertificatePath: sslCertificatePath,
|
||||
SSLCertificateKeyPath: sslCertificateKeyPath,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
certModel.ClearLog()
|
||||
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Success,
|
||||
Message: "Issued certificate successfully",
|
||||
SSLCertificate: sslCertificatePath,
|
||||
SSLCertificateKey: sslCertificateKeyPath,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetCertList(c *gin.Context) {
|
||||
certList := model.GetCertList(c.Query("name"), c.Query("domain"))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": certList,
|
||||
})
|
||||
cosy.Core[model.Cert](c).SetFussy("name", "domain").PagingList()
|
||||
}
|
||||
|
||||
func getCert(c *gin.Context, certModel *model.Cert) {
|
||||
type resp struct {
|
||||
*model.Cert
|
||||
SSLCertification string `json:"ssl_certification"`
|
||||
SSLCertificationKey string `json:"ssl_certification_key"`
|
||||
CertificateInfo *sites.CertificateInfo `json:"certificate_info,omitempty"`
|
||||
SSLCertificate string `json:"ssl_certificate"`
|
||||
SSLCertificateKey string `json:"ssl_certificate_key"`
|
||||
CertificateInfo *sites.CertificateInfo `json:"certificate_info,omitempty"`
|
||||
}
|
||||
|
||||
var sslCertificationBytes, sslCertificationKeyBytes []byte
|
||||
|
@ -273,8 +134,8 @@ func ModifyCert(c *gin.Context) {
|
|||
Name string `json:"name"`
|
||||
SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
|
||||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
|
||||
SSLCertification string `json:"ssl_certification"`
|
||||
SSLCertificationKey string `json:"ssl_certification_key"`
|
||||
SSLCertificate string `json:"ssl_certificate"`
|
||||
SSLCertificateKey string `json:"ssl_certificate_key"`
|
||||
}
|
||||
|
||||
if !api.BindAndValid(c, &json) {
|
||||
|
@ -310,16 +171,16 @@ func ModifyCert(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if json.SSLCertification != "" {
|
||||
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
|
||||
if json.SSLCertificate != "" {
|
||||
err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertificate), 0644)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if json.SSLCertificationKey != "" {
|
||||
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
|
||||
if json.SSLCertificateKeyPath != "" {
|
||||
err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificateKey), 0644)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
|
@ -330,39 +191,5 @@ func ModifyCert(c *gin.Context) {
|
|||
}
|
||||
|
||||
func RemoveCert(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
certModel, err := model.FirstCertByID(id)
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = certModel.Remove()
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
||||
|
||||
func GetDNSProvidersList(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, dns.GetProvidersList())
|
||||
}
|
||||
|
||||
func GetDNSProvider(c *gin.Context) {
|
||||
code := c.Param("code")
|
||||
|
||||
provider, ok := dns.GetProvider(code)
|
||||
|
||||
if !ok {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"message": "provider not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, provider)
|
||||
cosy.Core[model.Cert](c).Destroy()
|
||||
}
|
|
@ -2,6 +2,7 @@ package certificate
|
|||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/api/cosy"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
|
@ -33,23 +34,7 @@ func GetDnsCredential(c *gin.Context) {
|
|||
}
|
||||
|
||||
func GetDnsCredentialList(c *gin.Context) {
|
||||
d := query.DnsCredential
|
||||
provider := c.Query("provider")
|
||||
var data []*model.DnsCredential
|
||||
var err error
|
||||
if provider != "" {
|
||||
data, err = d.Where(d.Provider.Eq(provider)).Find()
|
||||
} else {
|
||||
data, err = d.Find()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": data,
|
||||
})
|
||||
cosy.Core[model.DnsCredential](c).SetFussy("provider").PagingList()
|
||||
}
|
||||
|
||||
type DnsCredentialManageJson struct {
|
||||
|
@ -114,18 +99,5 @@ func EditDnsCredential(c *gin.Context) {
|
|||
}
|
||||
|
||||
func DeleteDnsCredential(c *gin.Context) {
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
d := query.DnsCredential
|
||||
|
||||
dnsCredential, err := d.FirstByID(id)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
err = d.DeleteByID(dnsCredential.ID)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
cosy.Core[model.DnsCredential](c).Destroy()
|
||||
}
|
||||
|
|
27
api/certificate/dns_provider.go
Normal file
27
api/certificate/dns_provider.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package certificate
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetDNSProvidersList(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, dns.GetProvidersList())
|
||||
}
|
||||
|
||||
func GetDNSProvider(c *gin.Context) {
|
||||
code := c.Param("code")
|
||||
|
||||
provider, ok := dns.GetProvider(code)
|
||||
|
||||
if !ok {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"message": "provider not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, provider)
|
||||
}
|
||||
|
142
api/certificate/issue.go
Normal file
142
api/certificate/issue.go
Normal file
|
@ -0,0 +1,142 @@
|
|||
package certificate
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Success = "success"
|
||||
Info = "info"
|
||||
Error = "error"
|
||||
)
|
||||
|
||||
type IssueCertResponse struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
SSLCertificate string `json:"ssl_certificate,omitempty"`
|
||||
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
|
||||
}
|
||||
|
||||
func handleIssueCertLogChan(conn *websocket.Conn, logChan chan string) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
for logString := range logChan {
|
||||
|
||||
err := conn.WriteJSON(IssueCertResponse{
|
||||
Status: Info,
|
||||
Message: logString,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func IssueCert(c *gin.Context) {
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// upgrade http to websocket
|
||||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func(ws *websocket.Conn) {
|
||||
_ = ws.Close()
|
||||
}(ws)
|
||||
|
||||
// read
|
||||
buffer := &cert.ConfigPayload{}
|
||||
|
||||
err = ws.ReadJSON(buffer)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
certModel, err := model.FirstOrCreateCert(c.Param("name"))
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logChan := make(chan string, 1)
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go cert.IssueCert(buffer, logChan, errChan)
|
||||
|
||||
go handleIssueCertLogChan(ws, logChan)
|
||||
|
||||
// block, until errChan closes
|
||||
for err = range errChan {
|
||||
errLog := &cert.AutoCertErrorLog{}
|
||||
errLog.SetCertModel(&certModel)
|
||||
errLog.Exit("issue cert", err)
|
||||
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
certDirName := strings.Join(buffer.ServerName, "_")
|
||||
sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
|
||||
sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")
|
||||
|
||||
err = certModel.Updates(&model.Cert{
|
||||
Domains: buffer.ServerName,
|
||||
SSLCertificatePath: sslCertificatePath,
|
||||
SSLCertificateKeyPath: sslCertificateKeyPath,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
certModel.ClearLog()
|
||||
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Success,
|
||||
Message: "Issued certificate successfully",
|
||||
SSLCertificate: sslCertificatePath,
|
||||
SSLCertificateKey: sslCertificateKeyPath,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue