@@ -3,48 +3,31 @@ package certificate
33import (
44 "github.com/0xJacky/Nginx-UI/api"
55 "github.com/0xJacky/Nginx-UI/api/cosy"
6- "github.com/0xJacky/Nginx-UI/api/sites"
76 "github.com/0xJacky/Nginx-UI/internal/cert"
87 "github.com/0xJacky/Nginx-UI/model"
8+ "github.com/0xJacky/Nginx-UI/query"
99 "github.com/gin-gonic/gin"
1010 "github.com/spf13/cast"
1111 "net/http"
1212 "os"
13- "path/filepath"
1413)
1514
16- func GetCertList (c * gin.Context ) {
17- cosy.Core [model.Cert ](c ).SetFussy ("name" , "domain" ).PagingList ()
15+ type APICertificate struct {
16+ * model.Cert
17+ SSLCertificate string `json:"ssl_certificate,omitempty"`
18+ SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
19+ CertificateInfo * cert.Info `json:"certificate_info,omitempty"`
1820}
1921
20- func getCert (c * gin.Context , certModel * model.Cert ) {
21- type resp struct {
22- * model.Cert
23- SSLCertificate string `json:"ssl_certificate"`
24- SSLCertificateKey string `json:"ssl_certificate_key"`
25- CertificateInfo * sites.CertificateInfo `json:"certificate_info,omitempty"`
26- }
27-
22+ func Transformer (certModel * model.Cert ) (certificate * APICertificate ) {
2823 var sslCertificationBytes , sslCertificationKeyBytes []byte
29- var certificateInfo * sites. CertificateInfo
24+ var certificateInfo * cert. Info
3025 if certModel .SSLCertificatePath != "" {
3126 if _ , err := os .Stat (certModel .SSLCertificatePath ); err == nil {
3227 sslCertificationBytes , _ = os .ReadFile (certModel .SSLCertificatePath )
3328 }
3429
35- pubKey , err := cert .GetCertInfo (certModel .SSLCertificatePath )
36-
37- if err != nil {
38- api .ErrHandler (c , err )
39- return
40- }
41-
42- certificateInfo = & sites.CertificateInfo {
43- SubjectName : pubKey .Subject .CommonName ,
44- IssuerName : pubKey .Issuer .CommonName ,
45- NotAfter : pubKey .NotAfter ,
46- NotBefore : pubKey .NotBefore ,
47- }
30+ certificateInfo , _ = cert .GetCertInfo (certModel .SSLCertificatePath )
4831 }
4932
5033 if certModel .SSLCertificateKeyPath != "" {
@@ -53,32 +36,48 @@ func getCert(c *gin.Context, certModel *model.Cert) {
5336 }
5437 }
5538
56- c .JSON (http .StatusOK , resp {
57- certModel ,
58- string (sslCertificationBytes ),
59- string (sslCertificationKeyBytes ),
60- certificateInfo ,
61- })
39+ return & APICertificate {
40+ Cert : certModel ,
41+ SSLCertificate : string (sslCertificationBytes ),
42+ SSLCertificateKey : string (sslCertificationKeyBytes ),
43+ CertificateInfo : certificateInfo ,
44+ }
45+ }
46+
47+ func GetCertList (c * gin.Context ) {
48+ cosy.Core [model.Cert ](c ).SetFussy ("name" , "domain" ).SetTransformer (func (m * model.Cert ) any {
49+
50+ info , _ := cert .GetCertInfo (m .SSLCertificatePath )
51+
52+ return APICertificate {
53+ Cert : m ,
54+ CertificateInfo : info ,
55+ }
56+ }).PagingList ()
6257}
6358
6459func GetCert (c * gin.Context ) {
65- certModel , err := model .FirstCertByID (cast .ToInt (c .Param ("id" )))
60+ q := query .Cert
61+
62+ certModel , err := q .FirstByID (cast .ToInt (c .Param ("id" )))
6663
6764 if err != nil {
6865 api .ErrHandler (c , err )
6966 return
7067 }
7168
72- getCert ( c , & certModel )
69+ c . JSON ( http . StatusOK , Transformer ( certModel ) )
7370}
7471
7572func AddCert (c * gin.Context ) {
7673 var json struct {
7774 Name string `json:"name"`
7875 SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
7976 SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
80- SSLCertification string `json:"ssl_certification"`
81- SSLCertificationKey string `json:"ssl_certification_key"`
77+ SSLCertificate string `json:"ssl_certificate"`
78+ SSLCertificateKey string `json:"ssl_certificate_key"`
79+ ChallengeMethod string `json:"challenge_method"`
80+ DnsCredentialID int `json:"dns_credential_id"`
8281 }
8382 if ! api .BindAndValid (c , & json ) {
8483 return
@@ -87,6 +86,8 @@ func AddCert(c *gin.Context) {
8786 Name : json .Name ,
8887 SSLCertificatePath : json .SSLCertificatePath ,
8988 SSLCertificateKeyPath : json .SSLCertificateKeyPath ,
89+ ChallengeMethod : json .ChallengeMethod ,
90+ DnsCredentialID : json .DnsCredentialID ,
9091 }
9192
9293 err := certModel .Insert ()
@@ -96,35 +97,21 @@ func AddCert(c *gin.Context) {
9697 return
9798 }
9899
99- err = os .MkdirAll (filepath .Dir (json .SSLCertificatePath ), 0644 )
100- if err != nil {
101- api .ErrHandler (c , err )
102- return
100+ content := & cert.Content {
101+ SSLCertificatePath : json .SSLCertificatePath ,
102+ SSLCertificateKeyPath : json .SSLCertificateKeyPath ,
103+ SSLCertificate : json .SSLCertificate ,
104+ SSLCertificateKey : json .SSLCertificateKey ,
103105 }
104106
105- err = os .MkdirAll (filepath .Dir (json .SSLCertificateKeyPath ), 0644 )
107+ err = content .WriteFile ()
108+
106109 if err != nil {
107110 api .ErrHandler (c , err )
108111 return
109112 }
110113
111- if json .SSLCertification != "" {
112- err = os .WriteFile (json .SSLCertificatePath , []byte (json .SSLCertification ), 0644 )
113- if err != nil {
114- api .ErrHandler (c , err )
115- return
116- }
117- }
118-
119- if json .SSLCertificationKey != "" {
120- err = os .WriteFile (json .SSLCertificateKeyPath , []byte (json .SSLCertificationKey ), 0644 )
121- if err != nil {
122- api .ErrHandler (c , err )
123- return
124- }
125- }
126-
127- getCert (c , certModel )
114+ c .JSON (http .StatusOK , Transformer (certModel ))
128115}
129116
130117func ModifyCert (c * gin.Context ) {
@@ -136,13 +123,17 @@ func ModifyCert(c *gin.Context) {
136123 SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
137124 SSLCertificate string `json:"ssl_certificate"`
138125 SSLCertificateKey string `json:"ssl_certificate_key"`
126+ ChallengeMethod string `json:"challenge_method"`
127+ DnsCredentialID int `json:"dns_credential_id"`
139128 }
140129
141130 if ! api .BindAndValid (c , & json ) {
142131 return
143132 }
144133
145- certModel , err := model .FirstCertByID (id )
134+ q := query .Cert
135+
136+ certModel , err := q .FirstByID (id )
146137 if err != nil {
147138 api .ErrHandler (c , err )
148139 return
@@ -152,41 +143,29 @@ func ModifyCert(c *gin.Context) {
152143 Name : json .Name ,
153144 SSLCertificatePath : json .SSLCertificatePath ,
154145 SSLCertificateKeyPath : json .SSLCertificateKeyPath ,
146+ ChallengeMethod : json .ChallengeMethod ,
147+ DnsCredentialID : json .DnsCredentialID ,
155148 })
156149
157150 if err != nil {
158151 api .ErrHandler (c , err )
159152 return
160153 }
161154
162- err = os .MkdirAll (filepath .Dir (json .SSLCertificatePath ), 0644 )
163- if err != nil {
164- api .ErrHandler (c , err )
165- return
155+ content := & cert.Content {
156+ SSLCertificatePath : json .SSLCertificatePath ,
157+ SSLCertificateKeyPath : json .SSLCertificateKeyPath ,
158+ SSLCertificate : json .SSLCertificate ,
159+ SSLCertificateKey : json .SSLCertificateKey ,
166160 }
167161
168- err = os .MkdirAll (filepath .Dir (json .SSLCertificateKeyPath ), 0644 )
162+ err = content .WriteFile ()
163+
169164 if err != nil {
170165 api .ErrHandler (c , err )
171166 return
172167 }
173168
174- if json .SSLCertificate != "" {
175- err = os .WriteFile (json .SSLCertificatePath , []byte (json .SSLCertificate ), 0644 )
176- if err != nil {
177- api .ErrHandler (c , err )
178- return
179- }
180- }
181-
182- if json .SSLCertificateKeyPath != "" {
183- err = os .WriteFile (json .SSLCertificateKeyPath , []byte (json .SSLCertificateKey ), 0644 )
184- if err != nil {
185- api .ErrHandler (c , err )
186- return
187- }
188- }
189-
190169 GetCert (c )
191170}
192171
0 commit comments