forked from canonical/go-dqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dial.go
41 lines (34 loc) · 936 Bytes
/
dial.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
package app
import (
"context"
"crypto/tls"
"net"
"github.com/canonical/go-dqlite/client"
"github.com/pkg/errors"
)
// Like client.DialFuncWithTLS but also starts the proxy, since the raft
// connect function only supports Unix and TCP connections.
func makeNodeDialFunc(config *tls.Config) client.DialFunc {
dial := func(ctx context.Context, addr string) (net.Conn, error) {
clonedConfig := config.Clone()
if len(clonedConfig.ServerName) == 0 {
remoteIP, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
clonedConfig.ServerName = remoteIP
}
dialer := &net.Dialer{}
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
goUnix, cUnix, err := socketpair()
if err != nil {
return nil, errors.Wrap(err, "create pair of Unix sockets")
}
go proxy(context.Background(), conn, goUnix, clonedConfig)
return cUnix, nil
}
return dial
}