-
Notifications
You must be signed in to change notification settings - Fork 36
/
memconn_example_buffered_tls_test.go
147 lines (128 loc) · 6.09 KB
/
memconn_example_buffered_tls_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package memconn_test
import (
"crypto/tls"
"crypto/x509"
"io"
"os"
"github.com/akutz/memconn"
)
// ExampleBufferedTLS illustrates a server and client that
// communicate over a buffered, in-memory connection using TLS.
func Example_bufferedTLS() {
// Create the cert pool for the CA.
rootCAs := x509.NewCertPool()
rootCAs.AppendCertsFromPEM([]byte(tlsBufferedCACrt))
// Load the host certificate and key.
hostKeyPair, _ := tls.X509KeyPair(
[]byte(tlsBufferedCrt), []byte(tlsBufferedKey))
// Announce a new listener named "localhost" on MemConn's
// buffered network, "memb".
lis, _ := memconn.Listen("memb", "localhost")
// Ensure the listener is closed.
defer lis.Close()
// Start a goroutine that will wait for a client to dial the
// listener and then echo back any data sent to the remote
// connection.
go func() {
conn, _ := lis.Accept()
// Wrap the new connection inside of a TLS server.
conn = tls.Server(conn, &tls.Config{
Certificates: []tls.Certificate{hostKeyPair},
RootCAs: rootCAs,
})
// If no errors occur then make sure the connection is closed.
defer conn.Close()
// Echo the data back to the client.
io.CopyN(conn, conn, 13)
}()
// Dial the buffered, in-memory network named "localhost".
conn, _ := memconn.Dial("memb", "localhost")
// Wrap the connection in TLS. It's necessary to set the "ServerName"
// field in the TLS configuration in order to match one of the host
// certificate's Subject Alternate Name values.
conn = tls.Client(conn, &tls.Config{
RootCAs: rootCAs,
ServerName: "localhost",
})
// Ensure the connection is closed.
defer conn.Close()
// Write the data to the server.
conn.Write([]byte("Hello, world."))
// Read the data from the server.
io.CopyN(os.Stdout, conn, 13)
// Output: Hello, world.
}
const (
tlsBufferedCrt = `-----BEGIN CERTIFICATE-----
MIIDvjCCAqagAwIBAgIBATANBgkqhkiG9w0BAQUFADCBgjELMAkGA1UEBhMCVVMx
DjAMBgNVBAgMBVRleGFzMQ8wDQYDVQQHDAZBdXN0aW4xDzANBgNVBAoMBkdpdEh1
YjEOMAwGA1UECwwFYWt1dHoxEDAOBgNVBAMMB01lbUNvbm4xHzAdBgkqhkiG9w0B
CQEWEGFrdXR6QGdpdGh1Yi5jb20wIBcNMTgwNDI0MTUwMzQ2WhgPMjExODAzMzEx
NTAzNDZaMFExCzAJBgNVBAYTAlVTMQ4wDAYDVQQIDAVUZXhhczEPMA0GA1UECgwG
R2l0SHViMSEwHwYDVQQDDBhtZW1jb25uLmFrdXR6LmdpdGh1Yi5jb20wggEiMA0G
CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDJVRieeIQCb4RpXiDL8cPs6QYxRzZX
fHxTFK8GOhr08fBbW+HJbHCVlbQa/iLwJyjUc98oBiWmdNyZBLJDmEPxcjFGdD8C
N6EArKto9R1NC1vEMdm3nMYzveQ0/4O9RqLxk12papCMZEomv0Wv4ZsqhWTSl+gQ
MLt5uI51AAbpNmzO2eUHGHX7TDb73dcAiH77p5mrqDnFR+vd1a6wJNeLIRoUBzeR
peKO+gTTgWgLfqYEk/eOL3ymA3IxPadWGleeQZl0el+2fau5Z+L0Rlo4Tbdj2Gb2
VG3r/GKBIB7ALW10JJloyi5sTn4cpPUJp8FX6RZAZ9gGcwJtdJ38hqXLAgMBAAGj
bTBrMAkGA1UdEwQCMAAwPwYDVR0RBDgwNoIEaG9zdIIHbWVtY29uboIJbG9jYWxo
b3N0ghoqLm1lbWNvbm4uYWt1dHouZ2l0aHViLmNvbTAdBgNVHQ4EFgQU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