-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_outgoing.go
75 lines (66 loc) · 2.02 KB
/
manager_outgoing.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
package cluster
import (
"crypto/tls"
"net"
"time"
)
func (m *Manager) handleOutgoingConnections(tlsConfig *tls.Config) {
for {
select {
case <-m.quit:
// if manager exists, stop making outgoing connections
m.log("EXIT of manager for outgoing connections")
return
default:
}
// Attempt to connect to non-connected nodes
for _, node := range m.getConfiguredNodes() {
if !m.connectedNodes.nodeExists(node.name) {
// Connect to the remote cluster node
m.log("%s Connecting to non-connected cluster node: %s", m.name, node.name)
m.dial(node.name, node.addr, tlsConfig)
}
}
//w ait before we try again
time.Sleep(m.getDuration("connectinterval"))
}
}
func (m *Manager) dial(name, addr string, tlsConfig *tls.Config) {
var conn net.Conn
var err error
if len(tlsConfig.Certificates) == 0 {
m.log("%s Connecting to %s (%s) non-tls", m.name, name, addr)
conn, err = net.DialTimeout("tcp", addr, m.getDuration("connecttimeout"))
} else {
m.log("%s Connecting to %s (%s) with-tls", m.name, name, addr)
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: m.getDuration("connecttimeout")}, "tcp", addr, tlsConfig)
}
if err == nil {
// on dialing out, we need to send an auth
authRequest, _ := m.newPacket(packetAuthRequest{AuthKey: m.authKey})
m.connectedNodes.writeSocket(conn, authRequest)
packet, err := m.connectedNodes.readSocket(conn)
if err != nil {
// close connection if someone is talking gibrish
m.log("%s auth request failed on dial: %s", m.name, err)
conn.Close()
return
}
authResponse := &packetAuthResponse{}
err = packet.Message(authResponse)
if err != nil {
// auth response unknown
m.log("%s auth response failed on dial: %s", m.name, err)
conn.Close()
return
}
if authResponse.Status != true {
m.log("%s auth failed on dial: %s", m.name, authResponse.Error)
conn.Close()
return
}
m.log("%s Connection to %s (%s) authorized", m.name, name, addr)
node := newNode(packet.Name, conn, false)
go m.handleAuthorizedConnection(node)
}
}