2020-02-06 00:55:26 +00:00
|
|
|
package edgediscovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/edgediscovery/allregions"
|
2020-02-06 00:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-28 18:10:01 +00:00
|
|
|
LogFieldConnIndex = "connIndex"
|
2022-05-20 21:51:36 +00:00
|
|
|
LogFieldIPAddress = "ip"
|
2020-02-06 00:55:26 +00:00
|
|
|
)
|
|
|
|
|
2022-05-20 21:51:36 +00:00
|
|
|
var ErrNoAddressesLeft = fmt.Errorf("there are no free edge addresses left")
|
2020-02-06 00:55:26 +00:00
|
|
|
|
|
|
|
// Edge finds addresses on the Cloudflare edge and hands them out to connections.
|
|
|
|
type Edge struct {
|
|
|
|
regions *allregions.Regions
|
|
|
|
sync.Mutex
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Constructors
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
// ResolveEdge runs the initial discovery of the Cloudflare edge, finding Addrs that can be allocated
|
|
|
|
// to connections.
|
2022-05-20 21:51:36 +00:00
|
|
|
func ResolveEdge(log *zerolog.Logger, region string, edgeIpVersion allregions.ConfigIPVersion) (*Edge, error) {
|
|
|
|
regions, err := allregions.ResolveEdge(log, region, edgeIpVersion)
|
2020-02-06 00:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return new(Edge), err
|
|
|
|
}
|
|
|
|
return &Edge{
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-02-06 00:55:26 +00:00
|
|
|
regions: regions,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticEdge creates a list of edge addresses from the list of hostnames. Mainly used for testing connectivity.
|
2020-11-25 06:55:13 +00:00
|
|
|
func StaticEdge(log *zerolog.Logger, hostnames []string) (*Edge, error) {
|
|
|
|
regions, err := allregions.StaticEdge(hostnames, log)
|
2020-02-06 00:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return new(Edge), err
|
|
|
|
}
|
|
|
|
return &Edge{
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-02-06 00:55:26 +00:00
|
|
|
regions: regions,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Methods
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
// GetAddrForRPC gives this connection an edge Addr.
|
2021-08-06 13:31:22 +00:00
|
|
|
func (ed *Edge) GetAddrForRPC() (*allregions.EdgeAddr, error) {
|
2020-02-06 00:55:26 +00:00
|
|
|
ed.Lock()
|
|
|
|
defer ed.Unlock()
|
|
|
|
addr := ed.regions.GetAnyAddress()
|
|
|
|
if addr == nil {
|
2022-05-20 21:51:36 +00:00
|
|
|
return nil, ErrNoAddressesLeft
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddr gives this proxy connection an edge Addr. Prefer Addrs this connection has already used.
|
2021-08-06 13:31:22 +00:00
|
|
|
func (ed *Edge) GetAddr(connIndex int) (*allregions.EdgeAddr, error) {
|
2020-12-28 18:10:01 +00:00
|
|
|
log := ed.log.With().Int(LogFieldConnIndex, connIndex).Logger()
|
2020-02-06 00:55:26 +00:00
|
|
|
ed.Lock()
|
|
|
|
defer ed.Unlock()
|
|
|
|
|
|
|
|
// If this connection has already used an edge addr, return it.
|
2020-12-28 18:10:01 +00:00
|
|
|
if addr := ed.regions.AddrUsedBy(connIndex); addr != nil {
|
|
|
|
log.Debug().Msg("edgediscovery - GetAddr: Returning same address back to proxy connection")
|
2020-02-06 00:55:26 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, give it an unused one
|
2020-12-28 18:10:01 +00:00
|
|
|
addr := ed.regions.GetUnusedAddr(nil, connIndex)
|
2020-02-06 00:55:26 +00:00
|
|
|
if addr == nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Debug().Msg("edgediscovery - GetAddr: No addresses left to give proxy connection")
|
2022-05-20 21:51:36 +00:00
|
|
|
return nil, ErrNoAddressesLeft
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
log = ed.log.With().
|
|
|
|
Int(LogFieldConnIndex, connIndex).
|
|
|
|
IPAddr(LogFieldIPAddress, addr.UDP.IP).Logger()
|
|
|
|
log.Debug().Msgf("edgediscovery - GetAddr: Giving connection its new address")
|
2020-02-06 00:55:26 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifferentAddr gives back the proxy connection's edge Addr and uses a new one.
|
2022-05-20 21:51:36 +00:00
|
|
|
func (ed *Edge) GetDifferentAddr(connIndex int, hasConnectivityError bool) (*allregions.EdgeAddr, error) {
|
2021-08-26 08:34:56 +00:00
|
|
|
log := ed.log.With().Int(LogFieldConnIndex, connIndex).Logger()
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
ed.Lock()
|
|
|
|
defer ed.Unlock()
|
|
|
|
|
2020-12-28 18:10:01 +00:00
|
|
|
oldAddr := ed.regions.AddrUsedBy(connIndex)
|
2020-02-06 00:55:26 +00:00
|
|
|
if oldAddr != nil {
|
2022-05-20 21:51:36 +00:00
|
|
|
ed.regions.GiveBack(oldAddr, hasConnectivityError)
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
2020-12-28 18:10:01 +00:00
|
|
|
addr := ed.regions.GetUnusedAddr(oldAddr, connIndex)
|
2020-02-06 00:55:26 +00:00
|
|
|
if addr == nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Debug().Msg("edgediscovery - GetDifferentAddr: No addresses left to give proxy connection")
|
2020-04-27 19:25:37 +00:00
|
|
|
// note: if oldAddr were not nil, it will become available on the next iteration
|
2022-05-20 21:51:36 +00:00
|
|
|
return nil, ErrNoAddressesLeft
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
log = ed.log.With().
|
|
|
|
Int(LogFieldConnIndex, connIndex).
|
|
|
|
IPAddr(LogFieldIPAddress, addr.UDP.IP).Logger()
|
|
|
|
log.Debug().Msgf("edgediscovery - GetDifferentAddr: Giving connection its new address from the address list: %v", ed.regions.AvailableAddrs())
|
2020-02-06 00:55:26 +00:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvailableAddrs returns how many unused addresses there are left.
|
|
|
|
func (ed *Edge) AvailableAddrs() int {
|
|
|
|
ed.Lock()
|
|
|
|
defer ed.Unlock()
|
|
|
|
return ed.regions.AvailableAddrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GiveBack the address so that other connections can use it.
|
|
|
|
// Returns true if the address is in this edge.
|
2022-05-20 21:51:36 +00:00
|
|
|
func (ed *Edge) GiveBack(addr *allregions.EdgeAddr, hasConnectivityError bool) bool {
|
2020-02-06 00:55:26 +00:00
|
|
|
ed.Lock()
|
|
|
|
defer ed.Unlock()
|
2022-05-20 21:51:36 +00:00
|
|
|
log := ed.log.With().
|
|
|
|
IPAddr(LogFieldIPAddress, addr.UDP.IP).Logger()
|
|
|
|
log.Debug().Msgf("edgediscovery - GiveBack: Address now unused")
|
|
|
|
return ed.regions.GiveBack(addr, hasConnectivityError)
|
2020-02-06 00:55:26 +00:00
|
|
|
}
|