Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance auto cert #420

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions api/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -86,6 +88,7 @@ type certJson struct {
ChallengeMethod string `json:"challenge_method"`
DnsCredentialID int `json:"dns_credential_id"`
ACMEUserID int `json:"acme_user_id"`
SyncNodeIds []int `json:"sync_node_ids"`
}

func AddCert(c *gin.Context) {
Expand All @@ -103,6 +106,7 @@ func AddCert(c *gin.Context) {
ChallengeMethod: json.ChallengeMethod,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
SyncNodeIds: json.SyncNodeIds,
}

err := certModel.Insert()
Expand All @@ -126,6 +130,12 @@ func AddCert(c *gin.Context) {
return
}

err = cert.SyncToRemoteServer(certModel)
if err != nil {
notification.Error("Sync Certificate Error", err.Error())
return
}

c.JSON(http.StatusOK, Transformer(certModel))
}

Expand Down Expand Up @@ -154,6 +164,7 @@ func ModifyCert(c *gin.Context) {
KeyType: json.KeyType,
DnsCredentialID: json.DnsCredentialID,
ACMEUserID: json.ACMEUserID,
SyncNodeIds: json.SyncNodeIds,
})

if err != nil {
Expand All @@ -175,9 +186,58 @@ func ModifyCert(c *gin.Context) {
return
}

err = cert.SyncToRemoteServer(certModel)
if err != nil {
notification.Error("Sync Certificate Error", err.Error())
return
}

GetCert(c)
}

func RemoveCert(c *gin.Context) {
cosy.Core[model.Cert](c).Destroy()
}

func SyncCertificate(c *gin.Context) {
var json cert.SyncCertificatePayload

if !api.BindAndValid(c, &json) {
return
}

certModel := &model.Cert{
Name: json.Name,
SSLCertificatePath: json.SSLCertificatePath,
SSLCertificateKeyPath: json.SSLCertificateKeyPath,
KeyType: json.KeyType,
AutoCert: model.AutoCertSync,
}

db := model.UseDB()

err := db.Where(certModel).FirstOrCreate(certModel).Error
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
}

nginx.Reload()

c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}
15 changes: 4 additions & 11 deletions api/certificate/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ 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/go-acme/lego/v4/certcrypto"
"github.com/gorilla/websocket"
"net/http"
"strings"
)

const (
Expand Down Expand Up @@ -71,7 +69,6 @@ func IssueCert(c *gin.Context) {
payload := &cert.ConfigPayload{}

err = ws.ReadJSON(payload)

if err != nil {
logger.Error(err)
return
Expand Down Expand Up @@ -122,14 +119,10 @@ func IssueCert(c *gin.Context) {
return
}

certDirName := strings.Join(payload.ServerName, "_") + "_" + string(payload.GetKeyType())
sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")

err = certModel.Updates(&model.Cert{
Domains: payload.ServerName,
SSLCertificatePath: sslCertificatePath,
SSLCertificateKeyPath: sslCertificateKeyPath,
SSLCertificatePath: payload.GetCertificatePath(),
SSLCertificateKeyPath: payload.GetCertificateKeyPath(),
AutoCert: model.AutoCertEnabled,
KeyType: payload.KeyType,
ChallengeMethod: payload.ChallengeMethod,
Expand All @@ -152,8 +145,8 @@ func IssueCert(c *gin.Context) {
err = ws.WriteJSON(IssueCertResponse{
Status: Success,
Message: "Issued certificate successfully",
SSLCertificate: sslCertificatePath,
SSLCertificateKey: sslCertificateKeyPath,
SSLCertificate: payload.GetCertificatePath(),
SSLCertificateKey: payload.GetCertificateKeyPath(),
KeyType: payload.GetKeyType(),
})

Expand Down
1 change: 1 addition & 0 deletions api/certificate/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func InitCertificateRouter(r *gin.RouterGroup) {
r.POST("cert", AddCert)
r.POST("cert/:id", ModifyCert)
r.DELETE("cert/:id", RemoveCert)
r.PUT("cert_sync", SyncCertificate)
r.GET("certificate/dns_providers", GetDNSProvidersList)
r.GET("certificate/dns_provider/:code", GetDNSProvider)
}
Expand Down
3 changes: 1 addition & 2 deletions api/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func GetList(c *gin.Context) {
}

func Destroy(c *gin.Context) {
cosy.Core[model.Notification](c).
PermanentlyDelete()
cosy.Core[model.Notification](c).Destroy()
}

func DestroyAll(c *gin.Context) {
Expand Down
2 changes: 0 additions & 2 deletions api/sites/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ func GetDomains(c *gin.Context) {
sort := c.DefaultQuery("sort", "desc")

configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))

if err != nil {
api.ErrHandler(c, err)
return
}

enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))

if err != nil {
api.ErrHandler(c, err)
return
Expand Down
38 changes: 19 additions & 19 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nginx-ui-app-next",
"version": "2.0.0-beta.24",
"version": "2.0.0-beta.25",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -13,15 +13,15 @@
"dependencies": {
"@ant-design/icons-vue": "^7.0.1",
"@formkit/auto-animate": "^0.8.2",
"@vue/reactivity": "^3.4.27",
"@vue/shared": "^3.4.27",
"@vueuse/core": "^10.9.0",
"@vue/reactivity": "^3.4.29",
"@vue/shared": "^3.4.29",
"@vueuse/core": "^10.11.0",
"@xterm/addon-attach": "^0.11.0",
"@xterm/addon-fit": "^0.10.0",
"@xterm/xterm": "^5.5.0",
"ant-design-vue": "^4.2.1",
"apexcharts": "^3.49.0",
"axios": "^1.6.8",
"ant-design-vue": "^4.2.3",
"apexcharts": "^3.49.1",
"axios": "^1.7.2",
"dayjs": "^1.11.11",
"highlight.js": "^11.9.0",
"lodash": "^4.17.21",
Expand All @@ -32,44 +32,44 @@
"reconnecting-websocket": "^4.4.0",
"sortablejs": "^1.15.2",
"vite-plugin-build-id": "^0.2.9",
"vue": "^3.4.27",
"vue": "^3.4.29",
"vue-github-button": "github:0xJacky/vue-github-button",
"vue-router": "^4.3.2",
"vue-router": "^4.3.3",
"vue3-ace-editor": "2.2.4",
"vue3-apexcharts": "1.4.4",
"vue3-gettext": "3.0.0-beta.4",
"vuedraggable": "^4.1.0"
},
"devDependencies": {
"@antfu/eslint-config-vue": "^0.43.1",
"@types/lodash": "^4.17.1",
"@types/lodash": "^4.17.5",
"@types/nprogress": "^0.2.3",
"@types/sortablejs": "^1.15.8",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vitejs/plugin-vue": "^5.0.4",
"@vitejs/plugin-vue": "^5.0.5",
"@vitejs/plugin-vue-jsx": "^3.1.0",
"@vue/compiler-sfc": "^3.4.27",
"@vue/compiler-sfc": "^3.4.29",
"@vue/tsconfig": "^0.5.1",
"ace-builds": "^1.33.1",
"ace-builds": "^1.35.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-regex": "^1.10.0",
"eslint-plugin-sonarjs": "^0.23.0",
"eslint-plugin-vue": "^9.25.0",
"eslint-plugin-vue": "^9.26.0",
"less": "^4.2.0",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"tailwindcss": "^3.4.4",
"typescript": "5.3.3",
"unplugin-auto-import": "^0.17.5",
"unplugin-auto-import": "^0.17.6",
"unplugin-vue-components": "^0.26.0",
"unplugin-vue-define-options": "^1.4.4",
"vite": "^5.2.11",
"unplugin-vue-define-options": "^1.4.5",
"vite": "^5.3.1",
"vite-svg-loader": "^5.1.0",
"vue-tsc": "^1.8.27"
},
"packageManager": "pnpm@9.0.6+sha256.0624e30eff866cdeb363b15061bdb7fd9425b17bc1bb42c22f5f4efdea21f6b3"
"packageManager": "pnpm@9.3.0+sha512.ee7b93e0c2bd11409c6424f92b866f31d3ea1bef5fbe47d3c7500cdc3c9668833d2e55681ad66df5b640c61fa9dc25d546efa54d76d7f8bf54b13614ac293631"
}
Loading