-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanchor.go
95 lines (77 loc) · 1.84 KB
/
anchor.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
package anchor
import (
"crypto/tls"
"encoding/base64"
"errors"
"log"
"net"
"net/url"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)
type Config struct {
URL *url.URL
EAB *EAB
ServerNames []string
mgr *autocert.Manager
}
type EAB struct {
KID string
Key string
}
func Listen(network, laddr string, config *Config) (net.Listener, error) {
if config.mgr == nil {
if err := config.setup(); err != nil {
return nil, err
}
}
if len(config.ServerNames) != 1 {
return nil, errors.New("anchor: missing required ServerNames field for Config")
}
helloECDSA := &tls.ClientHelloInfo{
ServerName: config.ServerNames[0],
CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
}
crtECDSA, err := config.mgr.GetCertificate(helloECDSA)
if err != nil {
log.Fatal(err)
}
helloRSA := &tls.ClientHelloInfo{
ServerName: config.ServerNames[0],
CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
}
crtRSA, err := config.mgr.GetCertificate(helloRSA)
if err != nil {
log.Fatal(err)
}
cfgTLS := &tls.Config{
Certificates: []tls.Certificate{*crtECDSA, *crtRSA},
GetCertificate: config.mgr.GetCertificate,
ServerName: config.ServerNames[0],
}
return tls.Listen(network, laddr, cfgTLS)
}
func (c *Config) setup() error {
if c.URL == nil {
return errors.New("anchor: missing required URL field for Config")
}
var eabKey []byte
if c.EAB != nil && len(c.EAB.Key) > 0 {
var err error
if eabKey, err = base64.RawURLEncoding.DecodeString(c.EAB.Key); err != nil {
return err
}
}
c.mgr = &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(c.ServerNames...),
Client: &acme.Client{
DirectoryURL: c.URL.String(),
},
ExternalAccountBinding: &acme.ExternalAccountBinding{
KID: c.EAB.KID,
Key: eabKey,
},
}
return nil
}