|
1 | 1 | package certificate |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/0xJacky/Nginx-UI/api" |
5 | | - "github.com/0xJacky/Nginx-UI/api/cosy" |
6 | | - "github.com/0xJacky/Nginx-UI/internal/cert" |
7 | | - "github.com/0xJacky/Nginx-UI/model" |
8 | | - "github.com/0xJacky/Nginx-UI/query" |
9 | | - "github.com/gin-gonic/gin" |
10 | | - "github.com/spf13/cast" |
11 | | - "net/http" |
12 | | - "os" |
| 4 | + "github.com/0xJacky/Nginx-UI/api" |
| 5 | + "github.com/0xJacky/Nginx-UI/api/cosy" |
| 6 | + "github.com/0xJacky/Nginx-UI/internal/cert" |
| 7 | + "github.com/0xJacky/Nginx-UI/model" |
| 8 | + "github.com/0xJacky/Nginx-UI/query" |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/spf13/cast" |
| 11 | + "net/http" |
| 12 | + "os" |
13 | 13 | ) |
14 | 14 |
|
15 | 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"` |
| 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"` |
20 | 20 | } |
21 | 21 |
|
22 | 22 | func Transformer(certModel *model.Cert) (certificate *APICertificate) { |
23 | | - var sslCertificationBytes, sslCertificationKeyBytes []byte |
24 | | - var certificateInfo *cert.Info |
25 | | - if certModel.SSLCertificatePath != "" { |
26 | | - if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { |
27 | | - sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) |
28 | | - if !cert.IsPublicKey(string(sslCertificationBytes)) { |
29 | | - sslCertificationBytes = []byte{} |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) |
34 | | - } |
35 | | - |
36 | | - if certModel.SSLCertificateKeyPath != "" { |
37 | | - if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { |
38 | | - sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) |
39 | | - if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { |
40 | | - sslCertificationKeyBytes = []byte{} |
41 | | - } |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - return &APICertificate{ |
46 | | - Cert: certModel, |
47 | | - SSLCertificate: string(sslCertificationBytes), |
48 | | - SSLCertificateKey: string(sslCertificationKeyBytes), |
49 | | - CertificateInfo: certificateInfo, |
50 | | - } |
| 23 | + var sslCertificationBytes, sslCertificationKeyBytes []byte |
| 24 | + var certificateInfo *cert.Info |
| 25 | + if certModel.SSLCertificatePath != "" { |
| 26 | + if _, err := os.Stat(certModel.SSLCertificatePath); err == nil { |
| 27 | + sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath) |
| 28 | + if !cert.IsPublicKey(string(sslCertificationBytes)) { |
| 29 | + sslCertificationBytes = []byte{} |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath) |
| 34 | + } |
| 35 | + |
| 36 | + if certModel.SSLCertificateKeyPath != "" { |
| 37 | + if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil { |
| 38 | + sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath) |
| 39 | + if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) { |
| 40 | + sslCertificationKeyBytes = []byte{} |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return &APICertificate{ |
| 46 | + Cert: certModel, |
| 47 | + SSLCertificate: string(sslCertificationBytes), |
| 48 | + SSLCertificateKey: string(sslCertificationKeyBytes), |
| 49 | + CertificateInfo: certificateInfo, |
| 50 | + } |
51 | 51 | } |
52 | 52 |
|
53 | 53 | func GetCertList(c *gin.Context) { |
54 | | - cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { |
| 54 | + cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any { |
55 | 55 |
|
56 | | - info, _ := cert.GetCertInfo(m.SSLCertificatePath) |
| 56 | + info, _ := cert.GetCertInfo(m.SSLCertificatePath) |
57 | 57 |
|
58 | | - return APICertificate{ |
59 | | - Cert: m, |
60 | | - CertificateInfo: info, |
61 | | - } |
62 | | - }).PagingList() |
| 58 | + return APICertificate{ |
| 59 | + Cert: m, |
| 60 | + CertificateInfo: info, |
| 61 | + } |
| 62 | + }).PagingList() |
63 | 63 | } |
64 | 64 |
|
65 | 65 | func GetCert(c *gin.Context) { |
66 | | - q := query.Cert |
| 66 | + q := query.Cert |
67 | 67 |
|
68 | | - certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) |
| 68 | + certModel, err := q.FirstByID(cast.ToInt(c.Param("id"))) |
69 | 69 |
|
70 | | - if err != nil { |
71 | | - api.ErrHandler(c, err) |
72 | | - return |
73 | | - } |
| 70 | + if err != nil { |
| 71 | + api.ErrHandler(c, err) |
| 72 | + return |
| 73 | + } |
74 | 74 |
|
75 | | - c.JSON(http.StatusOK, Transformer(certModel)) |
| 75 | + c.JSON(http.StatusOK, Transformer(certModel)) |
76 | 76 | } |
77 | 77 |
|
78 | 78 | type certJson struct { |
79 | | - Name string `json:"name"` |
80 | | - SSLCertificatePath string `json:"ssl_certificate_path" binding:"publickey_path"` |
81 | | - SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"privatekey_path"` |
82 | | - SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` |
83 | | - SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` |
84 | | - ChallengeMethod string `json:"challenge_method"` |
85 | | - DnsCredentialID int `json:"dns_credential_id"` |
| 79 | + Name string `json:"name" binding:"required"` |
| 80 | + SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"` |
| 81 | + SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"` |
| 82 | + SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"` |
| 83 | + SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"` |
| 84 | + ChallengeMethod string `json:"challenge_method"` |
| 85 | + DnsCredentialID int `json:"dns_credential_id"` |
86 | 86 | } |
87 | 87 |
|
88 | 88 | func AddCert(c *gin.Context) { |
89 | | - var json certJson |
90 | | - if !api.BindAndValid(c, &json) { |
91 | | - return |
92 | | - } |
93 | | - certModel := &model.Cert{ |
94 | | - Name: json.Name, |
95 | | - SSLCertificatePath: json.SSLCertificatePath, |
96 | | - SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
97 | | - ChallengeMethod: json.ChallengeMethod, |
98 | | - DnsCredentialID: json.DnsCredentialID, |
99 | | - } |
100 | | - |
101 | | - err := certModel.Insert() |
102 | | - |
103 | | - if err != nil { |
104 | | - api.ErrHandler(c, err) |
105 | | - return |
106 | | - } |
107 | | - |
108 | | - content := &cert.Content{ |
109 | | - SSLCertificatePath: json.SSLCertificatePath, |
110 | | - SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
111 | | - SSLCertificate: json.SSLCertificate, |
112 | | - SSLCertificateKey: json.SSLCertificateKey, |
113 | | - } |
114 | | - |
115 | | - err = content.WriteFile() |
116 | | - |
117 | | - if err != nil { |
118 | | - api.ErrHandler(c, err) |
119 | | - return |
120 | | - } |
121 | | - |
122 | | - c.JSON(http.StatusOK, Transformer(certModel)) |
| 89 | + var json certJson |
| 90 | + if !api.BindAndValid(c, &json) { |
| 91 | + return |
| 92 | + } |
| 93 | + certModel := &model.Cert{ |
| 94 | + Name: json.Name, |
| 95 | + SSLCertificatePath: json.SSLCertificatePath, |
| 96 | + SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
| 97 | + ChallengeMethod: json.ChallengeMethod, |
| 98 | + DnsCredentialID: json.DnsCredentialID, |
| 99 | + } |
| 100 | + |
| 101 | + err := certModel.Insert() |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + api.ErrHandler(c, err) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + content := &cert.Content{ |
| 109 | + SSLCertificatePath: json.SSLCertificatePath, |
| 110 | + SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
| 111 | + SSLCertificate: json.SSLCertificate, |
| 112 | + SSLCertificateKey: json.SSLCertificateKey, |
| 113 | + } |
| 114 | + |
| 115 | + err = content.WriteFile() |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + api.ErrHandler(c, err) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + c.JSON(http.StatusOK, Transformer(certModel)) |
123 | 123 | } |
124 | 124 |
|
125 | 125 | func ModifyCert(c *gin.Context) { |
126 | | - id := cast.ToInt(c.Param("id")) |
127 | | - |
128 | | - var json certJson |
129 | | - |
130 | | - if !api.BindAndValid(c, &json) { |
131 | | - return |
132 | | - } |
133 | | - |
134 | | - q := query.Cert |
135 | | - |
136 | | - certModel, err := q.FirstByID(id) |
137 | | - if err != nil { |
138 | | - api.ErrHandler(c, err) |
139 | | - return |
140 | | - } |
141 | | - |
142 | | - err = certModel.Updates(&model.Cert{ |
143 | | - Name: json.Name, |
144 | | - SSLCertificatePath: json.SSLCertificatePath, |
145 | | - SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
146 | | - ChallengeMethod: json.ChallengeMethod, |
147 | | - DnsCredentialID: json.DnsCredentialID, |
148 | | - }) |
149 | | - |
150 | | - if err != nil { |
151 | | - api.ErrHandler(c, err) |
152 | | - return |
153 | | - } |
154 | | - |
155 | | - content := &cert.Content{ |
156 | | - SSLCertificatePath: json.SSLCertificatePath, |
157 | | - SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
158 | | - SSLCertificate: json.SSLCertificate, |
159 | | - SSLCertificateKey: json.SSLCertificateKey, |
160 | | - } |
161 | | - |
162 | | - err = content.WriteFile() |
163 | | - |
164 | | - if err != nil { |
165 | | - api.ErrHandler(c, err) |
166 | | - return |
167 | | - } |
168 | | - |
169 | | - GetCert(c) |
| 126 | + id := cast.ToInt(c.Param("id")) |
| 127 | + |
| 128 | + var json certJson |
| 129 | + |
| 130 | + if !api.BindAndValid(c, &json) { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + q := query.Cert |
| 135 | + |
| 136 | + certModel, err := q.FirstByID(id) |
| 137 | + if err != nil { |
| 138 | + api.ErrHandler(c, err) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + err = certModel.Updates(&model.Cert{ |
| 143 | + Name: json.Name, |
| 144 | + SSLCertificatePath: json.SSLCertificatePath, |
| 145 | + SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
| 146 | + ChallengeMethod: json.ChallengeMethod, |
| 147 | + DnsCredentialID: json.DnsCredentialID, |
| 148 | + }) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + api.ErrHandler(c, err) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + content := &cert.Content{ |
| 156 | + SSLCertificatePath: json.SSLCertificatePath, |
| 157 | + SSLCertificateKeyPath: json.SSLCertificateKeyPath, |
| 158 | + SSLCertificate: json.SSLCertificate, |
| 159 | + SSLCertificateKey: json.SSLCertificateKey, |
| 160 | + } |
| 161 | + |
| 162 | + err = content.WriteFile() |
| 163 | + |
| 164 | + if err != nil { |
| 165 | + api.ErrHandler(c, err) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + GetCert(c) |
170 | 170 | } |
171 | 171 |
|
172 | 172 | func RemoveCert(c *gin.Context) { |
173 | | - cosy.Core[model.Cert](c).Destroy() |
| 173 | + cosy.Core[model.Cert](c).Destroy() |
174 | 174 | } |
0 commit comments