-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
91 lines (77 loc) · 1.69 KB
/
example_test.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
package dane_test
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/hawell/dane"
"log"
"net"
"net/http"
"net/smtp"
"time"
)
func ExampleHTTP() {
var (
certPool *x509.CertPool
err error
)
certPool, err = dane.AcmeCertPool()
if err != nil {
log.Printf("failed to load mozilla cert pool: %+v, using default", err)
}
t := &http.Transport{
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
conn, err := tls.DialWithDialer(dialer, network, addr, &tls.Config{
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return dane.VerifyPeerCertificate(network, addr, rawCerts, certPool)
},
})
if err != nil {
return conn, err
}
return conn, nil
},
}
client := http.Client{Transport: t}
_, err = client.Get("https://zone-42.com")
if err != nil {
log.Fatal(err)
}
fmt.Println("success")
// Output:
// success
}
func ExampleSMTP() {
// Connect to the SMTP Server
servername := "open.nlnet.nl:25"
host, _, _ := net.SplitHostPort(servername)
// TLS config
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return dane.VerifyPeerCertificate("tcp", servername, rawCerts, nil)
},
}
c, err := smtp.Dial(servername)
if err != nil {
panic(err)
}
err = c.StartTLS(tlsConfig)
if err != nil {
panic(err)
}
err = c.Quit()
if err != nil {
panic(err)
}
fmt.Println("success")
// Output:
// success
}