@@ -2,166 +2,27 @@ package certificate
22
33import (
44 "github.com/0xJacky/Nginx-UI/api"
5+ "github.com/0xJacky/Nginx-UI/api/cosy"
56 "github.com/0xJacky/Nginx-UI/api/sites"
67 "github.com/0xJacky/Nginx-UI/internal/cert"
7- "github.com/0xJacky/Nginx-UI/internal/cert/dns"
8- "github.com/0xJacky/Nginx-UI/internal/logger"
9- "github.com/0xJacky/Nginx-UI/internal/nginx"
108 "github.com/0xJacky/Nginx-UI/model"
119 "github.com/gin-gonic/gin"
12- "github.com/gorilla/websocket"
1310 "github.com/spf13/cast"
1411 "net/http"
1512 "os"
1613 "path/filepath"
17- "strings"
1814)
1915
20- const (
21- Success = "success"
22- Info = "info"
23- Error = "error"
24- )
25-
26- type IssueCertResponse struct {
27- Status string `json:"status"`
28- Message string `json:"message"`
29- SSLCertificate string `json:"ssl_certificate,omitempty"`
30- SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
31- }
32-
33- func handleIssueCertLogChan (conn * websocket.Conn , logChan chan string ) {
34- defer func () {
35- if err := recover (); err != nil {
36- logger .Error (err )
37- }
38- }()
39-
40- for logString := range logChan {
41-
42- err := conn .WriteJSON (IssueCertResponse {
43- Status : Info ,
44- Message : logString ,
45- })
46-
47- if err != nil {
48- logger .Error (err )
49- return
50- }
51-
52- }
53- }
54-
55- func IssueCert (c * gin.Context ) {
56- var upGrader = websocket.Upgrader {
57- CheckOrigin : func (r * http.Request ) bool {
58- return true
59- },
60- }
61-
62- // upgrade http to websocket
63- ws , err := upGrader .Upgrade (c .Writer , c .Request , nil )
64- if err != nil {
65- logger .Error (err )
66- return
67- }
68-
69- defer func (ws * websocket.Conn ) {
70- _ = ws .Close ()
71- }(ws )
72-
73- // read
74- buffer := & cert.ConfigPayload {}
75-
76- err = ws .ReadJSON (buffer )
77-
78- if err != nil {
79- logger .Error (err )
80- return
81- }
82-
83- certModel , err := model .FirstOrCreateCert (c .Param ("name" ))
84-
85- if err != nil {
86- logger .Error (err )
87- return
88- }
89-
90- logChan := make (chan string , 1 )
91- errChan := make (chan error , 1 )
92-
93- go cert .IssueCert (buffer , logChan , errChan )
94-
95- go handleIssueCertLogChan (ws , logChan )
96-
97- // block, until errChan closes
98- for err = range errChan {
99- errLog := & cert.AutoCertErrorLog {}
100- errLog .SetCertModel (& certModel )
101- errLog .Exit ("issue cert" , err )
102-
103- err = ws .WriteJSON (IssueCertResponse {
104- Status : Error ,
105- Message : err .Error (),
106- })
107-
108- if err != nil {
109- logger .Error (err )
110- return
111- }
112-
113- return
114- }
115-
116- certDirName := strings .Join (buffer .ServerName , "_" )
117- sslCertificatePath := nginx .GetConfPath ("ssl" , certDirName , "fullchain.cer" )
118- sslCertificateKeyPath := nginx .GetConfPath ("ssl" , certDirName , "private.key" )
119-
120- err = certModel .Updates (& model.Cert {
121- Domains : buffer .ServerName ,
122- SSLCertificatePath : sslCertificatePath ,
123- SSLCertificateKeyPath : sslCertificateKeyPath ,
124- })
125-
126- if err != nil {
127- logger .Error (err )
128- err = ws .WriteJSON (IssueCertResponse {
129- Status : Error ,
130- Message : err .Error (),
131- })
132- return
133- }
134-
135- certModel .ClearLog ()
136-
137- err = ws .WriteJSON (IssueCertResponse {
138- Status : Success ,
139- Message : "Issued certificate successfully" ,
140- SSLCertificate : sslCertificatePath ,
141- SSLCertificateKey : sslCertificateKeyPath ,
142- })
143-
144- if err != nil {
145- logger .Error (err )
146- return
147- }
148-
149- }
150-
15116func GetCertList (c * gin.Context ) {
152- certList := model .GetCertList (c .Query ("name" ), c .Query ("domain" ))
153-
154- c .JSON (http .StatusOK , gin.H {
155- "data" : certList ,
156- })
17+ cosy.Core [model.Cert ](c ).SetFussy ("name" , "domain" ).PagingList ()
15718}
15819
15920func getCert (c * gin.Context , certModel * model.Cert ) {
16021 type resp struct {
16122 * model.Cert
162- SSLCertification string `json:"ssl_certification "`
163- SSLCertificationKey string `json:"ssl_certification_key "`
164- CertificateInfo * sites.CertificateInfo `json:"certificate_info,omitempty"`
23+ SSLCertificate string `json:"ssl_certificate "`
24+ SSLCertificateKey string `json:"ssl_certificate_key "`
25+ CertificateInfo * sites.CertificateInfo `json:"certificate_info,omitempty"`
16526 }
16627
16728 var sslCertificationBytes , sslCertificationKeyBytes []byte
@@ -273,8 +134,8 @@ func ModifyCert(c *gin.Context) {
273134 Name string `json:"name"`
274135 SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
275136 SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
276- SSLCertification string `json:"ssl_certification "`
277- SSLCertificationKey string `json:"ssl_certification_key "`
137+ SSLCertificate string `json:"ssl_certificate "`
138+ SSLCertificateKey string `json:"ssl_certificate_key "`
278139 }
279140
280141 if ! api .BindAndValid (c , & json ) {
@@ -310,16 +171,16 @@ func ModifyCert(c *gin.Context) {
310171 return
311172 }
312173
313- if json .SSLCertification != "" {
314- err = os .WriteFile (json .SSLCertificatePath , []byte (json .SSLCertification ), 0644 )
174+ if json .SSLCertificate != "" {
175+ err = os .WriteFile (json .SSLCertificatePath , []byte (json .SSLCertificate ), 0644 )
315176 if err != nil {
316177 api .ErrHandler (c , err )
317178 return
318179 }
319180 }
320181
321- if json .SSLCertificationKey != "" {
322- err = os .WriteFile (json .SSLCertificateKeyPath , []byte (json .SSLCertificationKey ), 0644 )
182+ if json .SSLCertificateKeyPath != "" {
183+ err = os .WriteFile (json .SSLCertificateKeyPath , []byte (json .SSLCertificateKey ), 0644 )
323184 if err != nil {
324185 api .ErrHandler (c , err )
325186 return
@@ -330,39 +191,5 @@ func ModifyCert(c *gin.Context) {
330191}
331192
332193func RemoveCert (c * gin.Context ) {
333- id := cast .ToInt (c .Param ("id" ))
334- certModel , err := model .FirstCertByID (id )
335-
336- if err != nil {
337- api .ErrHandler (c , err )
338- return
339- }
340-
341- err = certModel .Remove ()
342-
343- if err != nil {
344- api .ErrHandler (c , err )
345- return
346- }
347-
348- c .JSON (http .StatusNoContent , nil )
349- }
350-
351- func GetDNSProvidersList (c * gin.Context ) {
352- c .JSON (http .StatusOK , dns .GetProvidersList ())
353- }
354-
355- func GetDNSProvider (c * gin.Context ) {
356- code := c .Param ("code" )
357-
358- provider , ok := dns .GetProvider (code )
359-
360- if ! ok {
361- c .JSON (http .StatusNotFound , gin.H {
362- "message" : "provider not found" ,
363- })
364- return
365- }
366-
367- c .JSON (http .StatusOK , provider )
194+ cosy.Core [model.Cert ](c ).Destroy ()
368195}
0 commit comments