Skip to content

Commit cfa7629

Browse files
authored
Merge pull request #361 from 0xJacky/refresh-24.04
refactor: refresh webui and certificates module
2 parents 59d59dd + a0013b5 commit cfa7629

File tree

158 files changed

+8471
-6102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+8471
-6102
lines changed

api/certificate/acme_user.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package certificate
2+
3+
import (
4+
"github.com/0xJacky/Nginx-UI/api"
5+
"github.com/0xJacky/Nginx-UI/internal/cosy"
6+
"github.com/0xJacky/Nginx-UI/model"
7+
"github.com/0xJacky/Nginx-UI/query"
8+
"github.com/0xJacky/Nginx-UI/settings"
9+
"github.com/gin-gonic/gin"
10+
"github.com/spf13/cast"
11+
"net/http"
12+
)
13+
14+
func GetAcmeUser(c *gin.Context) {
15+
u := query.AcmeUser
16+
id := cast.ToInt(c.Param("id"))
17+
user, err := u.FirstByID(id)
18+
if err != nil {
19+
api.ErrHandler(c, err)
20+
return
21+
}
22+
c.JSON(http.StatusOK, user)
23+
}
24+
25+
func CreateAcmeUser(c *gin.Context) {
26+
cosy.Core[model.AcmeUser](c).SetValidRules(gin.H{
27+
"name": "required",
28+
"email": "required,email",
29+
"ca_dir": "omitempty",
30+
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
31+
if ctx.Model.CADir == "" {
32+
ctx.Model.CADir = settings.ServerSettings.CADir
33+
}
34+
err := ctx.Model.Register()
35+
if err != nil {
36+
ctx.AbortWithError(err)
37+
return
38+
}
39+
}).Create()
40+
}
41+
42+
func ModifyAcmeUser(c *gin.Context) {
43+
cosy.Core[model.AcmeUser](c).SetValidRules(gin.H{
44+
"name": "omitempty",
45+
"email": "omitempty,email",
46+
"ca_dir": "omitempty",
47+
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
48+
if ctx.Model.CADir == "" {
49+
ctx.Model.CADir = settings.ServerSettings.CADir
50+
}
51+
52+
if ctx.OriginModel.Email != ctx.Model.Email ||
53+
ctx.OriginModel.CADir != ctx.Model.CADir {
54+
err := ctx.Model.Register()
55+
if err != nil {
56+
ctx.AbortWithError(err)
57+
return
58+
}
59+
}
60+
}).Modify()
61+
}
62+
63+
func GetAcmeUserList(c *gin.Context) {
64+
cosy.Core[model.AcmeUser](c).
65+
SetFussy("name", "email").
66+
PagingList()
67+
}
68+
69+
func DestroyAcmeUser(c *gin.Context) {
70+
cosy.Core[model.AcmeUser](c).Destroy()
71+
}
72+
73+
func RecoverAcmeUser(c *gin.Context) {
74+
cosy.Core[model.AcmeUser](c).Recover()
75+
}
76+
77+
func RegisterAcmeUser(c *gin.Context) {
78+
id := cast.ToInt(c.Param("id"))
79+
u := query.AcmeUser
80+
user, err := u.FirstByID(id)
81+
if err != nil {
82+
api.ErrHandler(c, err)
83+
return
84+
}
85+
err = user.Register()
86+
if err != nil {
87+
api.ErrHandler(c, err)
88+
return
89+
}
90+
_, err = u.Where(u.ID.Eq(id)).Updates(user)
91+
if err != nil {
92+
api.ErrHandler(c, err)
93+
return
94+
}
95+
c.JSON(http.StatusOK, user)
96+
}

api/certificate/certificate.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package certificate
22

33
import (
44
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/api/cosy"
65
"github.com/0xJacky/Nginx-UI/internal/cert"
6+
"github.com/0xJacky/Nginx-UI/internal/cosy"
77
"github.com/0xJacky/Nginx-UI/model"
88
"github.com/0xJacky/Nginx-UI/query"
99
"github.com/gin-gonic/gin"
@@ -85,6 +85,7 @@ type certJson struct {
8585
KeyType certcrypto.KeyType `json:"key_type" binding:"omitempty,auto_cert_key_type"`
8686
ChallengeMethod string `json:"challenge_method"`
8787
DnsCredentialID int `json:"dns_credential_id"`
88+
ACMEUserID int `json:"acme_user_id"`
8889
}
8990

9091
func AddCert(c *gin.Context) {
@@ -101,6 +102,7 @@ func AddCert(c *gin.Context) {
101102
KeyType: json.KeyType,
102103
ChallengeMethod: json.ChallengeMethod,
103104
DnsCredentialID: json.DnsCredentialID,
105+
ACMEUserID: json.ACMEUserID,
104106
}
105107

106108
err := certModel.Insert()
@@ -151,6 +153,7 @@ func ModifyCert(c *gin.Context) {
151153
ChallengeMethod: json.ChallengeMethod,
152154
KeyType: json.KeyType,
153155
DnsCredentialID: json.DnsCredentialID,
156+
ACMEUserID: json.ACMEUserID,
154157
})
155158

156159
if err != nil {

api/certificate/dns_credential.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package certificate
22

33
import (
44
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/api/cosy"
65
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
6+
"github.com/0xJacky/Nginx-UI/internal/cosy"
77
"github.com/0xJacky/Nginx-UI/model"
88
"github.com/0xJacky/Nginx-UI/query"
99
"github.com/gin-gonic/gin"

api/certificate/issue.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/0xJacky/Nginx-UI/internal/nginx"
77
"github.com/0xJacky/Nginx-UI/model"
88
"github.com/gin-gonic/gin"
9+
"github.com/go-acme/lego/v4/certcrypto"
910
"github.com/gorilla/websocket"
1011
"net/http"
1112
"strings"
@@ -18,10 +19,11 @@ const (
1819
)
1920

2021
type IssueCertResponse struct {
21-
Status string `json:"status"`
22-
Message string `json:"message"`
23-
SSLCertificate string `json:"ssl_certificate,omitempty"`
24-
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
22+
Status string `json:"status"`
23+
Message string `json:"message"`
24+
SSLCertificate string `json:"ssl_certificate,omitempty"`
25+
SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
26+
KeyType certcrypto.KeyType `json:"key_type"`
2527
}
2628

2729
func handleIssueCertLogChan(conn *websocket.Conn, log *cert.Logger, logChan chan string) {
@@ -75,13 +77,20 @@ func IssueCert(c *gin.Context) {
7577
return
7678
}
7779

78-
certModel, err := model.FirstOrCreateCert(c.Param("name"))
79-
80+
certModel, err := model.FirstOrCreateCert(c.Param("name"), payload.GetKeyType())
8081
if err != nil {
8182
logger.Error(err)
8283
return
8384
}
8485

86+
certInfo, err := cert.GetCertInfo(certModel.SSLCertificatePath)
87+
if err != nil {
88+
logger.Error("get certificate info error", err)
89+
return
90+
}
91+
payload.Resource = certModel.Resource
92+
payload.NotBefore = certInfo.NotBefore
93+
8594
logChan := make(chan string, 1)
8695
errChan := make(chan error, 1)
8796

@@ -113,7 +122,7 @@ func IssueCert(c *gin.Context) {
113122
return
114123
}
115124

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

@@ -125,6 +134,7 @@ func IssueCert(c *gin.Context) {
125134
KeyType: payload.KeyType,
126135
ChallengeMethod: payload.ChallengeMethod,
127136
DnsCredentialID: payload.DNSCredentialID,
137+
Resource: payload.Resource,
128138
})
129139

130140
if err != nil {
@@ -144,6 +154,7 @@ func IssueCert(c *gin.Context) {
144154
Message: "Issued certificate successfully",
145155
SSLCertificate: sslCertificatePath,
146156
SSLCertificateKey: sslCertificateKeyPath,
157+
KeyType: payload.GetKeyType(),
147158
})
148159

149160
if err != nil {

api/certificate/router.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,13 @@ func InitCertificateRouter(r *gin.RouterGroup) {
2323
func InitCertificateWebSocketRouter(r *gin.RouterGroup) {
2424
r.GET("domain/:name/cert", IssueCert)
2525
}
26+
27+
func InitAcmeUserRouter(r *gin.RouterGroup) {
28+
r.GET("acme_users", GetAcmeUserList)
29+
r.GET("acme_user/:id", GetAcmeUser)
30+
r.POST("acme_user", CreateAcmeUser)
31+
r.POST("acme_user/:id", ModifyAcmeUser)
32+
r.POST("acme_user/:id/register", RegisterAcmeUser)
33+
r.DELETE("acme_user/:id", DestroyAcmeUser)
34+
r.PATCH("acme_user/:id", RecoverAcmeUser)
35+
}

api/notification/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package notification
22

33
import (
44
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/api/cosy"
5+
"github.com/0xJacky/Nginx-UI/internal/cosy"
66
"github.com/0xJacky/Nginx-UI/model"
77
"github.com/0xJacky/Nginx-UI/query"
88
"github.com/gin-gonic/gin"

api/sites/auto_cert.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@ package sites
22

33
import (
44
"github.com/0xJacky/Nginx-UI/api"
5+
"github.com/0xJacky/Nginx-UI/internal/helper"
56
"github.com/0xJacky/Nginx-UI/model"
67
"github.com/gin-gonic/gin"
8+
"github.com/go-acme/lego/v4/certcrypto"
79
"net/http"
810
)
911

1012
func AddDomainToAutoCert(c *gin.Context) {
1113
name := c.Param("name")
1214

1315
var json struct {
14-
DnsCredentialID int `json:"dns_credential_id"`
15-
ChallengeMethod string `json:"challenge_method"`
16-
Domains []string `json:"domains"`
16+
DnsCredentialID int `json:"dns_credential_id"`
17+
ChallengeMethod string `json:"challenge_method"`
18+
Domains []string `json:"domains"`
19+
KeyType certcrypto.KeyType `json:"key_type"`
1720
}
1821

1922
if !api.BindAndValid(c, &json) {
2023
return
2124
}
2225

23-
certModel, err := model.FirstOrCreateCert(name)
26+
certModel, err := model.FirstOrCreateCert(name, helper.GetKeyType(json.KeyType))
2427

2528
if err != nil {
2629
api.ErrHandler(c, err)

api/user/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package user
22

33
import (
44
"github.com/0xJacky/Nginx-UI/api"
5-
"github.com/0xJacky/Nginx-UI/api/cosy"
5+
"github.com/0xJacky/Nginx-UI/internal/cosy"
66
"github.com/0xJacky/Nginx-UI/model"
77
"github.com/0xJacky/Nginx-UI/query"
88
"github.com/0xJacky/Nginx-UI/settings"

app/.eslintrc.cjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = {
4040
before: false,
4141
after: true,
4242
}],
43-
'key-spacing': ['error', { afterColon: true }],
43+
'key-spacing': ['error', {afterColon: true}],
4444

4545
'vue/first-attribute-linebreak': ['error', {
4646
singleline: 'beside',
@@ -78,7 +78,7 @@ module.exports = {
7878
],
7979

8080
// Ignore _ as unused variable
81-
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_+$' }],
81+
'@typescript-eslint/no-unused-vars': ['error', {argsIgnorePattern: '^_+$'}],
8282

8383
'array-element-newline': ['error', 'consistent'],
8484
'array-bracket-newline': ['error', 'consistent'],
@@ -111,7 +111,7 @@ module.exports = {
111111

112112
// Plugin: eslint-plugin-import
113113
'import/prefer-default-export': 'off',
114-
'import/newline-after-import': ['error', { count: 1 }],
114+
'import/newline-after-import': ['error', {count: 1}],
115115
'no-restricted-imports': ['error', 'vuetify/components'],
116116

117117
// For omitting extension for ts files
@@ -150,7 +150,7 @@ module.exports = {
150150

151151
// ESLint plugin vue
152152
'vue/component-api-style': 'error',
153-
'vue/component-name-in-template-casing': ['error', 'PascalCase', { registeredComponentsOnly: false }],
153+
'vue/component-name-in-template-casing': ['error', 'PascalCase', {registeredComponentsOnly: false}],
154154
'vue/custom-event-name-casing': ['error', 'camelCase', {
155155
ignores: [
156156
'/^(click):[a-z]+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?/',

app/auto-imports.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
// Generated by unplugin-auto-import
66
export {}
77
declare global {
8+
const $gettext: typeof import('@/gettext')['$gettext']
9+
const $ngettext: typeof import('@/gettext')['$ngettext']
10+
const $npgettext: typeof import('@/gettext')['$npgettext']
11+
const $pgettext: typeof import('@/gettext')['$pgettext']
812
const EffectScope: typeof import('vue')['EffectScope']
913
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
1014
const computed: typeof import('vue')['computed']
@@ -86,6 +90,10 @@ import { UnwrapRef } from 'vue'
8690
declare module 'vue' {
8791
interface GlobalComponents {}
8892
interface ComponentCustomProperties {
93+
readonly $gettext: UnwrapRef<typeof import('@/gettext')['$gettext']>
94+
readonly $ngettext: UnwrapRef<typeof import('@/gettext')['$ngettext']>
95+
readonly $npgettext: UnwrapRef<typeof import('@/gettext')['$npgettext']>
96+
readonly $pgettext: UnwrapRef<typeof import('@/gettext')['$pgettext']>
8997
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
9098
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
9199
readonly computed: UnwrapRef<typeof import('vue')['computed']>
@@ -160,6 +168,10 @@ declare module 'vue' {
160168
declare module '@vue/runtime-core' {
161169
interface GlobalComponents {}
162170
interface ComponentCustomProperties {
171+
readonly $gettext: UnwrapRef<typeof import('@/gettext')['$gettext']>
172+
readonly $ngettext: UnwrapRef<typeof import('@/gettext')['$ngettext']>
173+
readonly $npgettext: UnwrapRef<typeof import('@/gettext')['$npgettext']>
174+
readonly $pgettext: UnwrapRef<typeof import('@/gettext')['$pgettext']>
163175
readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
164176
readonly acceptHMRUpdate: UnwrapRef<typeof import('pinia')['acceptHMRUpdate']>
165177
readonly computed: UnwrapRef<typeof import('vue')['computed']>

0 commit comments

Comments
 (0)