-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
92 lines (83 loc) · 2.21 KB
/
spec.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
package ipc
import (
"crypto/tls"
"fmt"
"io/ioutil"
)
// Spec represents the specification for an IPC connection
type Spec struct {
// The connection type. This can be any of the following: "tcp",
// "tcp4", "tcp6", or "unix"
Type string
// When using "tcp", "tcp4", or "tcp6" this should be the
// "host:port" formatted address. When using "unix", this should
// be the path to the file you wish to use for the unix socket.
Address string
// The secret that is communicated upon first connection to verify
// that the peer belongs to this IPC connection.
Secret string
useTls bool
tlsCert tls.Certificate
tlsCertBytes []byte
}
func (spec *Spec) toString() string {
return fmt.Sprintf(
"Type: %v, Address: %v, Secret: %v, TLS: %v",
spec.Type,
spec.Address,
spec.Secret,
spec.useTls,
)
}
// NewSpec creates a new insecure connection specification using the
// specified type, address and secret.
func NewSpec(hType string, address string, secret string) *Spec {
return &Spec{
Type: hType,
Address: address,
Secret: secret,
useTls: false,
}
}
// NewTLSSpec creates a new secure connection specification using the
// specified type, address and secret. The file paths to your TLS
// certificcate and key should be passed in the crt and key parameters.
func NewTLSSpec(hType string, address string,
crt string, key string, secret string) (*Spec, error) {
cerbytes, err := ioutil.ReadFile(crt)
if err != nil {
return nil, err
}
cer, err := tls.LoadX509KeyPair(crt, key)
if err != nil {
return nil, err
}
return &Spec{
Type: hType,
Address: address,
Secret: secret,
useTls: true,
tlsCert: cer,
tlsCertBytes: cerbytes,
}, nil
}
// NewTCPSpec creates a new insecure TCP connection specification
// using the provided port and secret.
func NewTCPSpec(port int, secret string) *Spec {
return &Spec{
Type: "tcp",
Address: fmt.Sprintf(":%d", port),
Secret: secret,
useTls: false,
}
}
// NewUnixSpec creates a new insecure Unix connection specification
// using the provided socket and secret.
func NewUnixSpec(socket string, secret string) *Spec {
return &Spec{
Type: "unix",
Address: socket,
Secret: secret,
useTls: false,
}
}