-
Notifications
You must be signed in to change notification settings - Fork 282
/
ssl.go
103 lines (89 loc) · 2.35 KB
/
ssl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package web
import (
"fmt"
"log"
"net/http"
ssl "github.com/gdy666/lucky/module/sslcertficate"
sslconf "github.com/gdy666/lucky/module/sslcertficate/conf"
"github.com/gin-gonic/gin"
)
func addSSL(c *gin.Context) {
var requestObj sslconf.SSLCertficate
err := c.BindJSON(&requestObj)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": "请求解析出错"})
return
}
err = requestObj.Init()
if err != nil {
log.Printf("addSSL requestObj.Init() error:%s", err.Error())
c.JSON(http.StatusOK, gin.H{"ret": 2, "msg": "证书和Key有误!"})
return
}
//fmt.Printf("CertsInfo:%v\n", *requestObj.CertsInfo)
err = ssl.SSLCertficateListAdd(&requestObj)
if err != nil {
log.Printf("config.SSLCertficateListAdd error:%s", err.Error())
c.JSON(http.StatusOK, gin.H{"ret": 2, "msg": fmt.Sprintf("添加SSL证书出错!:%s", err.Error())})
return
}
c.JSON(http.StatusOK, gin.H{"ret": 0})
}
type sslResInfo struct {
Key string `json:"Key"`
Remark string `json:"Remark"`
Enable bool `json:"Enable"`
AddTime string `json:"AddTime"`
CertsInfo *[]sslconf.CertInfo `json:"CertsInfo"`
}
func getSSLCertficateList(c *gin.Context) {
rawList := ssl.GetSSLCertficateList()
var res []sslResInfo
for i := range rawList {
info := sslResInfo{
Key: rawList[i].Key,
Remark: rawList[i].Remark,
Enable: rawList[i].Enable,
AddTime: rawList[i].AddTime,
CertsInfo: rawList[i].CertsInfo,
}
res = append(res, info)
}
c.JSON(http.StatusOK, gin.H{"ret": 0, "list": res})
}
func alterSSLCertficate(c *gin.Context) {
key := c.Query("key")
field := c.Query("field")
value := c.Query("value")
var err error
switch field {
case "enable":
{
enable := false
if value == "true" {
enable = true
}
err = ssl.SSLCertficateEnable(key, enable)
}
case "remark":
{
err = ssl.SSLCertficateAlterRemark(key, value)
}
default:
err = fmt.Errorf("不支持修改的字段:%s", field)
}
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"ret": 0})
}
func deleteSSLCertficate(c *gin.Context) {
key := c.Query("key")
err := ssl.SSLCertficateListDelete(key)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"ret": 0})
}