|
1 | 1 | package service |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/rand" |
5 | 6 | "crypto/rsa" |
6 | 7 | "crypto/tls" |
7 | 8 | "crypto/x509" |
8 | 9 | "encoding/json" |
9 | 10 | "encoding/pem" |
10 | 11 | "fmt" |
| 12 | + "github.com/1Panel-dev/1Panel/core/app/model" |
| 13 | + "github.com/1Panel-dev/1Panel/core/utils/req_helper" |
11 | 14 | "net" |
| 15 | + "net/http" |
12 | 16 | "os" |
13 | 17 | "path" |
14 | 18 | "strconv" |
@@ -265,6 +269,48 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error { |
265 | 269 | return err |
266 | 270 | } |
267 | 271 | secret = string(certFile) |
| 272 | + case "select": |
| 273 | + ssl, err := agentRepo.GetWebsiteSSL(repo.WithByID(req.SSLID)) |
| 274 | + if err != nil { |
| 275 | + return err |
| 276 | + } |
| 277 | + secret = ssl.Pem |
| 278 | + key = ssl.PrivateKey |
| 279 | + if err := settingRepo.Update("SSLID", strconv.Itoa(int(req.SSLID))); err != nil { |
| 280 | + return err |
| 281 | + } |
| 282 | + case "self": |
| 283 | + ca, err := agentRepo.GetCA(repo.WithByName("1Panel")) |
| 284 | + if err != nil { |
| 285 | + return err |
| 286 | + } |
| 287 | + params := make(map[string]interface{}) |
| 288 | + params["domains"] = req.Domain |
| 289 | + params["time"] = 10 |
| 290 | + params["unit"] = "year" |
| 291 | + params["keyType"] = "P256" |
| 292 | + params["id"] = ca.ID |
| 293 | + jsonData, err := json.Marshal(params) |
| 294 | + if err != nil { |
| 295 | + return err |
| 296 | + } |
| 297 | + res, err := req_helper.NewLocalClient("/api/v2/websites/ca/obtain", http.MethodPost, bytes.NewReader(jsonData)) |
| 298 | + if err != nil { |
| 299 | + return err |
| 300 | + } |
| 301 | + jsonBytes, err := json.Marshal(res) |
| 302 | + if err != nil { |
| 303 | + return err |
| 304 | + } |
| 305 | + var ssl model.WebsiteSSL |
| 306 | + if err := json.Unmarshal(jsonBytes, &ssl); err != nil { |
| 307 | + return err |
| 308 | + } |
| 309 | + secret = ssl.Pem |
| 310 | + key = ssl.PrivateKey |
| 311 | + if err := settingRepo.Update("SSLID", strconv.Itoa(int(ssl.ID))); err != nil { |
| 312 | + return err |
| 313 | + } |
268 | 314 | } |
269 | 315 |
|
270 | 316 | if err := os.WriteFile(path.Join(secretDir, "server.crt.tmp"), []byte(secret), 0600); err != nil { |
@@ -325,7 +371,18 @@ func (u *SettingService) LoadFromCert() (*dto.SSLInfo, error) { |
325 | 371 | keyFile, _ := os.ReadFile(path.Join(global.CONF.Base.InstallDir, "1panel/secret/server.key")) |
326 | 372 | data.Key = string(keyFile) |
327 | 373 | case "select": |
328 | | - // TODO select ssl from website |
| 374 | + sslID, err := settingRepo.Get(repo.WithByKey("SSLID")) |
| 375 | + if err != nil { |
| 376 | + return nil, err |
| 377 | + } |
| 378 | + id, _ := strconv.Atoi(sslID.Value) |
| 379 | + ssl, err := agentRepo.GetWebsiteSSL(repo.WithByID(uint(id))) |
| 380 | + if err != nil { |
| 381 | + return nil, err |
| 382 | + } |
| 383 | + data.Domain = ssl.PrimaryDomain |
| 384 | + data.SSLID = uint(id) |
| 385 | + data.Timeout = ssl.ExpireDate.Format(constant.DateTimeLayout) |
329 | 386 | } |
330 | 387 | return &data, nil |
331 | 388 | } |
|
0 commit comments