This repository was archived by the owner on Dec 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcertificate.go
More file actions
94 lines (75 loc) · 1.8 KB
/
certificate.go
File metadata and controls
94 lines (75 loc) · 1.8 KB
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
package wenex
import (
"crypto/tls"
"errors"
"github.com/nexcode/joneva"
"golang.org/x/crypto/acme/autocert"
)
func stringCert(wnx *Wenex, path string) (*tls.Config, error) {
if wnx.Config.Get(path) == nil {
return nil, nil
}
certPem, err := wnx.Config.String(path + ".cert")
if err != nil {
return nil, err
}
keyPem, err := wnx.Config.String(path + ".key")
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair([]byte(certPem), []byte(keyPem))
if err != nil {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, nil
}
func loadCert(wnx *Wenex, path string) (*tls.Config, error) {
if wnx.Config.Get(path) == nil {
return nil, nil
}
certFile, err := wnx.Config.String(path + ".cert")
if err != nil {
return nil, err
}
keyFile, err := wnx.Config.String(path + ".key")
if err != nil {
return nil, err
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, nil
}
func autoCert(wnx *Wenex, path string) (*autocert.Manager, error) {
if wnx.Config.Get(path) == nil {
return nil, nil
}
hostsInterface, err := wnx.Config.Slice(path + ".hosts")
if err != nil {
return nil, err
}
hostsString := make([]string, len(hostsInterface))
for key, value := range hostsInterface {
hostString, ok := value.(string)
if !ok {
return nil, errors.New("sefsef")
}
hostsString[key] = hostString
}
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(hostsString...),
}
dirCache, err := wnx.Config.String(path + ".dirCache")
if err == nil {
certManager.Cache = autocert.DirCache(dirCache)
} else if !errors.Is(err, joneva.ErrNotFound) {
return nil, err
}
return certManager, nil
}