2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-05-08 22:57:37 +00:00
|
|
|
|
2021-08-26 22:04:32 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2015-05-08 22:57:37 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2013-12-09 21:13:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RaftLayer implements the raft.StreamLayer interface,
|
|
|
|
// so that we can use a single RPC layer for Raft and Consul
|
|
|
|
type RaftLayer struct {
|
2017-05-03 10:57:11 +00:00
|
|
|
// src is the address for outgoing connections.
|
|
|
|
src net.Addr
|
|
|
|
|
|
|
|
// addr is the listener address to return.
|
2013-12-09 21:13:40 +00:00
|
|
|
addr net.Addr
|
|
|
|
|
2017-05-03 10:57:11 +00:00
|
|
|
// connCh is used to accept connections.
|
2013-12-09 21:13:40 +00:00
|
|
|
connCh chan net.Conn
|
|
|
|
|
2015-05-08 22:57:37 +00:00
|
|
|
// TLS wrapper
|
|
|
|
tlsWrap tlsutil.Wrapper
|
2014-04-04 23:36:47 +00:00
|
|
|
|
2013-12-09 21:13:40 +00:00
|
|
|
// Tracks if we are closed
|
|
|
|
closed bool
|
|
|
|
closeCh chan struct{}
|
|
|
|
closeLock sync.Mutex
|
2017-05-10 21:25:48 +00:00
|
|
|
|
|
|
|
// tlsFunc is a callback to determine whether to use TLS for connecting to
|
|
|
|
// a given Raft server
|
|
|
|
tlsFunc func(raft.ServerAddress) bool
|
2013-12-09 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRaftLayer is used to initialize a new RaftLayer which can
|
2014-04-04 23:36:47 +00:00
|
|
|
// be used as a StreamLayer for Raft. If a tlsConfig is provided,
|
|
|
|
// then the connection will use TLS.
|
2017-05-10 21:25:48 +00:00
|
|
|
func NewRaftLayer(src, addr net.Addr, tlsWrap tlsutil.Wrapper, tlsFunc func(raft.ServerAddress) bool) *RaftLayer {
|
2013-12-09 21:13:40 +00:00
|
|
|
layer := &RaftLayer{
|
2017-05-03 10:57:11 +00:00
|
|
|
src: src,
|
2015-05-08 22:57:37 +00:00
|
|
|
addr: addr,
|
|
|
|
connCh: make(chan net.Conn),
|
|
|
|
tlsWrap: tlsWrap,
|
|
|
|
closeCh: make(chan struct{}),
|
2017-05-10 21:25:48 +00:00
|
|
|
tlsFunc: tlsFunc,
|
2013-12-09 21:13:40 +00:00
|
|
|
}
|
|
|
|
return layer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handoff is used to hand off a connection to the
|
|
|
|
// RaftLayer. This allows it to be Accept()'ed
|
|
|
|
func (l *RaftLayer) Handoff(c net.Conn) error {
|
|
|
|
select {
|
|
|
|
case l.connCh <- c:
|
|
|
|
return nil
|
|
|
|
case <-l.closeCh:
|
|
|
|
return fmt.Errorf("Raft RPC layer closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept is used to return connection which are
|
|
|
|
// dialed to be used with the Raft layer
|
|
|
|
func (l *RaftLayer) Accept() (net.Conn, error) {
|
|
|
|
select {
|
|
|
|
case conn := <-l.connCh:
|
|
|
|
return conn, nil
|
|
|
|
case <-l.closeCh:
|
|
|
|
return nil, fmt.Errorf("Raft RPC layer closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to stop listening for Raft connections
|
|
|
|
func (l *RaftLayer) Close() error {
|
|
|
|
l.closeLock.Lock()
|
|
|
|
defer l.closeLock.Unlock()
|
|
|
|
|
|
|
|
if !l.closed {
|
|
|
|
l.closed = true
|
|
|
|
close(l.closeCh)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr is used to return the address of the listener
|
|
|
|
func (l *RaftLayer) Addr() net.Addr {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial is used to create a new outgoing connection
|
2016-07-28 19:11:28 +00:00
|
|
|
func (l *RaftLayer) Dial(address raft.ServerAddress, timeout time.Duration) (net.Conn, error) {
|
2017-05-03 10:57:11 +00:00
|
|
|
d := &net.Dialer{LocalAddr: l.src, Timeout: timeout}
|
|
|
|
conn, err := d.Dial("tcp", string(address))
|
2013-12-09 22:29:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-04 23:36:47 +00:00
|
|
|
// Check for tls mode
|
2017-05-10 21:25:48 +00:00
|
|
|
if l.tlsFunc(address) && l.tlsWrap != nil {
|
2014-04-04 23:36:47 +00:00
|
|
|
// Switch the connection into TLS mode
|
2017-06-15 13:16:16 +00:00
|
|
|
if _, err := conn.Write([]byte{byte(pool.RPCTLS)}); err != nil {
|
2014-04-04 23:36:47 +00:00
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the connection in a TLS client
|
2015-05-08 22:57:37 +00:00
|
|
|
conn, err = l.tlsWrap(conn)
|
2014-06-22 19:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-04 23:36:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:29:20 +00:00
|
|
|
// Write the Raft byte to set the mode
|
2017-06-15 13:16:16 +00:00
|
|
|
_, err = conn.Write([]byte{byte(pool.RPCRaft)})
|
2014-01-10 20:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-09 22:29:20 +00:00
|
|
|
return conn, err
|
2013-12-09 21:13:40 +00:00
|
|
|
}
|