-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtls.go
96 lines (72 loc) · 1.88 KB
/
tls.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
package main
import (
"crypto/tls"
"net"
"os"
)
type Connector interface {
Listen(addr string) (net.Listener, error)
Dial(addr string) (net.Conn, error)
}
//---------------------------------------
// TLS
//---------------------------------------
type tlsConnector struct{}
func (self *tlsConnector) Listen(addr string) (net.Listener, error) {
cert, _ := tls.X509KeyPair([]byte(CertPEM), []byte(KeyPEM))
config := &tls.Config{
Certificates: make([]tls.Certificate, 1),
}
config.Certificates[0] = cert
return tls.Listen("unix", addr, config)
}
func (self *tlsConnector) Dial(addr string) (net.Conn, error) {
return tls.Dial("unix", addr, &tls.Config{InsecureSkipVerify: true})
}
//---------------------------------------
// UDS
//---------------------------------------
type udsConnector struct{}
func (self *udsConnector) Listen(addr string) (net.Listener, error) {
return net.Listen("unix", addr)
}
func (self *udsConnector) Dial(addr string) (net.Conn, error) {
return net.Dial("unix", addr)
}
//---------------------------------------
// Common Initializer
//---------------------------------------
func newConnector(connector Connector, addr string, payloadLen int) func() {
// Start our listener in common-context so we don't race with the registration
listener, err := connector.Listen(addr)
if err != nil {
panic(err)
}
go func() {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
buf := make([]byte, payloadLen)
for {
conn.Read(buf)
conn.Write(buf)
}
}()
conn, err := connector.Dial(addr)
if err != nil {
panic(err)
}
os.Remove(addr)
buf := make([]byte, payloadLen)
return func() {
conn.Write(buf)
conn.Read(buf)
}
}
func NewTLS(payloadLen int) func() {
return newConnector(&tlsConnector{}, "./rtt-go.tls", payloadLen)
}
func NewUDS(payloadLen int) func() {
return newConnector(&udsConnector{}, "./rtt-go.uds", payloadLen)
}