-
Notifications
You must be signed in to change notification settings - Fork 184
/
utils.go
113 lines (102 loc) · 2.51 KB
/
utils.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
104
105
106
107
108
109
110
111
112
113
package certutils
import (
"github.com/TeaWeb/build/internal/teaconfigs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"strings"
)
// 查找使用服务的证书
func FindAllServersUsingCert(certId string) []*teaconfigs.ServerConfig {
serverList, err := teaconfigs.SharedServerList()
if err != nil || serverList == nil {
return nil
}
result := []*teaconfigs.ServerConfig{}
if serverList != nil {
for _, server := range serverList.FindAllServers() {
if server.SSL == nil {
continue
}
found := false
for _, c := range server.SSL.Certs {
if c.Id == certId {
found = true
result = append(result, server)
break
}
}
if !found && lists.ContainsString(server.SSL.ClientCACertIds, certId) {
result = append(result, server)
break
}
}
}
return result
}
// 以Map的形式列出所有加密证书
func ListPairCertsMap() []maps.Map {
result := []maps.Map{}
for _, cert := range teaconfigs.SharedSSLCertList().Certs {
if cert.IsCA {
continue
}
err := cert.Validate()
errorString := ""
if err != nil {
errorString = err.Error()
}
summary := cert.Description
dnsNames := cert.DNSNames()
if len(dnsNames) > 0 {
if len(dnsNames) > 2 {
summary += " (" + strings.Join(dnsNames[:2], ",") + "等"
} else {
summary += " (" + strings.Join(dnsNames, ",")
}
summary += " - " + timeutil.Format("Y-m-d H:i:s", cert.TimeAfter())
summary += ")"
}
result = append(result, maps.Map{
"id": cert.Id,
"error": errorString,
"dnsNames": cert.DNSNames(),
"description": cert.Description,
"summary": summary,
})
}
return result
}
// 以Map的形式列出所有CA证书
func ListCACertsMap() []maps.Map {
result := []maps.Map{}
for _, cert := range teaconfigs.SharedSSLCertList().Certs {
if !cert.IsCA {
continue
}
err := cert.Validate()
errorString := ""
if err != nil {
errorString = err.Error()
}
summary := cert.Description
dnsNames := cert.DNSNames()
if len(dnsNames) > 0 {
if len(dnsNames) > 2 {
summary += " (" + strings.Join(dnsNames[:2], ",") + "等"
} else {
summary += " (" + strings.Join(dnsNames, ",")
}
summary += " - " + timeutil.Format("Y-m-d H:i:s", cert.TimeAfter())
summary += ")"
}
result = append(result, maps.Map{
"id": cert.Id,
"error": errorString,
"dnsNames": cert.DNSNames(),
"description": cert.Description,
"summary": summary,
})
}
return result
}