2022-02-07 09:42:07 +00:00
|
|
|
package supervisor
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-10 20:55:44 +00:00
|
|
|
"context"
|
2019-12-04 17:22:08 +00:00
|
|
|
"errors"
|
2021-01-20 19:41:09 +00:00
|
|
|
"fmt"
|
2022-09-19 11:36:25 +00:00
|
|
|
"net"
|
2022-06-18 00:24:37 +00:00
|
|
|
"strings"
|
2018-05-01 23:45:06 +00:00
|
|
|
"time"
|
2019-02-01 20:11:12 +00:00
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/google/uuid"
|
2022-06-18 00:24:37 +00:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2019-03-18 23:14:47 +00:00
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
2020-02-06 00:55:26 +00:00
|
|
|
"github.com/cloudflare/cloudflared/edgediscovery"
|
2021-08-06 13:31:22 +00:00
|
|
|
"github.com/cloudflare/cloudflared/edgediscovery/allregions"
|
2019-12-04 17:22:08 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
2022-08-18 15:03:47 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ingress"
|
2022-02-11 10:49:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/orchestration"
|
2021-03-26 04:04:56 +00:00
|
|
|
"github.com/cloudflare/cloudflared/retry"
|
2019-03-04 19:48:56 +00:00
|
|
|
"github.com/cloudflare/cloudflared/signal"
|
2019-12-04 17:22:08 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2022-08-11 20:31:36 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-14 13:42:00 +00:00
|
|
|
// SRV and TXT record resolution TTL
|
|
|
|
ResolveTTL = time.Hour
|
2018-05-01 23:45:06 +00:00
|
|
|
// Waiting time before retrying a failed tunnel connection
|
|
|
|
tunnelRetryDuration = time.Second * 10
|
|
|
|
// Interval between registering new tunnels
|
|
|
|
registrationInterval = time.Second
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
subsystemRefreshAuth = "refresh_auth"
|
|
|
|
// Maximum exponent for 'Authenticate' exponential backoff
|
|
|
|
refreshAuthMaxBackoff = 10
|
|
|
|
// Waiting time before retrying a failed 'Authenticate' connection
|
|
|
|
refreshAuthRetryDuration = time.Second * 10
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
// Supervisor manages non-declarative tunnels. Establishes TCP connections with the edge, and
|
|
|
|
// reconnects them if they disconnect.
|
2018-05-01 23:45:06 +00:00
|
|
|
type Supervisor struct {
|
2022-06-18 00:24:37 +00:00
|
|
|
cloudflaredUUID uuid.UUID
|
|
|
|
config *TunnelConfig
|
|
|
|
orchestrator *orchestration.Orchestrator
|
|
|
|
edgeIPs *edgediscovery.Edge
|
2022-08-18 15:03:47 +00:00
|
|
|
edgeTunnelServer *EdgeTunnelServer
|
2022-06-18 00:24:37 +00:00
|
|
|
tunnelErrors chan tunnelError
|
|
|
|
tunnelsConnecting map[int]chan struct{}
|
|
|
|
tunnelsProtocolFallback map[int]*protocolFallback
|
2018-05-01 23:45:06 +00:00
|
|
|
// nextConnectedIndex and nextConnectedSignal are used to wait for all
|
|
|
|
// currently-connecting tunnels to finish connecting so we can reset backoff timer
|
|
|
|
nextConnectedIndex int
|
|
|
|
nextConnectedSignal chan struct{}
|
2019-06-17 21:18:47 +00:00
|
|
|
|
2021-11-08 15:43:36 +00:00
|
|
|
log *ConnAwareLogger
|
2021-02-03 18:32:54 +00:00
|
|
|
logTransport *zerolog.Logger
|
2019-12-04 17:22:08 +00:00
|
|
|
|
2020-08-18 10:14:14 +00:00
|
|
|
reconnectCredentialManager *reconnectCredentialManager
|
2020-10-08 10:12:26 +00:00
|
|
|
useReconnectToken bool
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
reconnectCh chan ReconnectSignal
|
2021-02-05 00:07:49 +00:00
|
|
|
gracefulShutdownC <-chan struct{}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 00:07:49 +00:00
|
|
|
var errEarlyShutdown = errors.New("shutdown started")
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
type tunnelError struct {
|
|
|
|
index int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func NewSupervisor(config *TunnelConfig, orchestrator *orchestration.Orchestrator, reconnectCh chan ReconnectSignal, gracefulShutdownC <-chan struct{}) (*Supervisor, error) {
|
2021-01-20 19:41:09 +00:00
|
|
|
cloudflaredUUID, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to generate cloudflared instance ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-18 00:24:37 +00:00
|
|
|
isStaticEdge := len(config.EdgeAddrs) > 0
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
var edgeIPs *edgediscovery.Edge
|
2022-06-18 00:24:37 +00:00
|
|
|
if isStaticEdge { // static edge addresses
|
2020-11-25 06:55:13 +00:00
|
|
|
edgeIPs, err = edgediscovery.StaticEdge(config.Log, config.EdgeAddrs)
|
2019-12-24 05:11:00 +00:00
|
|
|
} else {
|
2022-05-20 21:51:36 +00:00
|
|
|
edgeIPs, err = edgediscovery.ResolveEdge(config.Log, config.Region, config.EdgeIPVersion)
|
2019-12-24 05:11:00 +00:00
|
|
|
}
|
2019-12-13 23:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-25 18:25:39 +00:00
|
|
|
|
2022-05-20 21:51:36 +00:00
|
|
|
reconnectCredentialManager := newReconnectCredentialManager(connection.MetricsNamespace, connection.TunnelSubsystem, config.HAConnections)
|
2022-08-11 20:31:36 +00:00
|
|
|
|
|
|
|
tracker := tunnelstate.NewConnTracker(config.Log)
|
|
|
|
log := NewConnAwareLogger(config.Log, tracker, config.Observer)
|
2022-05-20 21:51:36 +00:00
|
|
|
|
|
|
|
var edgeAddrHandler EdgeAddrHandler
|
2022-06-18 00:24:37 +00:00
|
|
|
if isStaticEdge { // static edge addresses
|
|
|
|
edgeAddrHandler = &IPAddrFallback{}
|
|
|
|
} else if config.EdgeIPVersion == allregions.IPv6Only || config.EdgeIPVersion == allregions.Auto {
|
2022-05-20 21:51:36 +00:00
|
|
|
edgeAddrHandler = &IPAddrFallback{}
|
|
|
|
} else { // IPv4Only
|
|
|
|
edgeAddrHandler = &DefaultAddrFallback{}
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeTunnelServer := EdgeTunnelServer{
|
|
|
|
config: config,
|
|
|
|
cloudflaredUUID: cloudflaredUUID,
|
|
|
|
orchestrator: orchestrator,
|
|
|
|
credentialManager: reconnectCredentialManager,
|
|
|
|
edgeAddrs: edgeIPs,
|
|
|
|
edgeAddrHandler: edgeAddrHandler,
|
2022-08-11 20:31:36 +00:00
|
|
|
tracker: tracker,
|
2022-05-20 21:51:36 +00:00
|
|
|
reconnectCh: reconnectCh,
|
|
|
|
gracefulShutdownC: gracefulShutdownC,
|
|
|
|
connAwareLogger: log,
|
|
|
|
}
|
2022-09-20 08:01:35 +00:00
|
|
|
|
|
|
|
icmpRouter, err := ingress.NewICMPRouter(config.Log)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger().Warn().Err(err).Msg("Failed to create icmp router, ICMP proxy feature is disabled")
|
|
|
|
} else {
|
|
|
|
edgeTunnelServer.icmpRouter = icmpRouter
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
useReconnectToken := false
|
|
|
|
if config.ClassicTunnel != nil {
|
|
|
|
useReconnectToken = config.ClassicTunnel.UseReconnectToken
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
return &Supervisor{
|
2020-08-18 10:14:14 +00:00
|
|
|
cloudflaredUUID: cloudflaredUUID,
|
|
|
|
config: config,
|
2022-02-11 10:49:06 +00:00
|
|
|
orchestrator: orchestrator,
|
2020-08-18 10:14:14 +00:00
|
|
|
edgeIPs: edgeIPs,
|
2022-08-18 15:03:47 +00:00
|
|
|
edgeTunnelServer: &edgeTunnelServer,
|
2020-08-18 10:14:14 +00:00
|
|
|
tunnelErrors: make(chan tunnelError),
|
|
|
|
tunnelsConnecting: map[int]chan struct{}{},
|
2022-06-18 00:24:37 +00:00
|
|
|
tunnelsProtocolFallback: map[int]*protocolFallback{},
|
2022-05-20 21:51:36 +00:00
|
|
|
log: log,
|
2021-02-03 18:32:54 +00:00
|
|
|
logTransport: config.LogTransport,
|
2022-05-20 21:51:36 +00:00
|
|
|
reconnectCredentialManager: reconnectCredentialManager,
|
2020-10-08 10:12:26 +00:00
|
|
|
useReconnectToken: useReconnectToken,
|
2021-01-20 19:41:09 +00:00
|
|
|
reconnectCh: reconnectCh,
|
|
|
|
gracefulShutdownC: gracefulShutdownC,
|
2019-12-13 23:05:21 +00:00
|
|
|
}, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
func (s *Supervisor) Run(
|
|
|
|
ctx context.Context,
|
|
|
|
connectedSignal *signal.Signal,
|
|
|
|
) error {
|
2022-09-13 13:00:54 +00:00
|
|
|
if s.edgeTunnelServer.icmpRouter != nil {
|
2022-08-18 15:03:47 +00:00
|
|
|
go func() {
|
2022-09-13 13:00:54 +00:00
|
|
|
if err := s.edgeTunnelServer.icmpRouter.Serve(ctx); err != nil {
|
2022-09-19 11:36:25 +00:00
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
s.log.Logger().Info().Err(err).Msg("icmp router terminated")
|
|
|
|
} else {
|
|
|
|
s.log.Logger().Err(err).Msg("icmp router terminated")
|
|
|
|
}
|
2022-08-18 15:03:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
if err := s.initialize(ctx, connectedSignal); err != nil {
|
2021-02-05 00:07:49 +00:00
|
|
|
if err == errEarlyShutdown {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tunnelsWaiting []int
|
2019-12-06 21:32:15 +00:00
|
|
|
tunnelsActive := s.config.HAConnections
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff := retry.BackoffHandler{MaxRetries: s.config.Retries, BaseTime: tunnelRetryDuration, RetryForever: true}
|
2018-05-01 23:45:06 +00:00
|
|
|
var backoffTimer <-chan time.Time
|
|
|
|
|
2021-03-26 04:04:56 +00:00
|
|
|
refreshAuthBackoff := &retry.BackoffHandler{MaxRetries: refreshAuthMaxBackoff, BaseTime: refreshAuthRetryDuration, RetryForever: true}
|
2019-12-04 17:22:08 +00:00
|
|
|
var refreshAuthBackoffTimer <-chan time.Time
|
2019-12-06 21:32:15 +00:00
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
if s.useReconnectToken {
|
2020-08-18 10:14:14 +00:00
|
|
|
if timer, err := s.reconnectCredentialManager.RefreshAuth(ctx, refreshAuthBackoff, s.authenticate); err == nil {
|
2019-12-06 21:32:15 +00:00
|
|
|
refreshAuthBackoffTimer = timer
|
|
|
|
} else {
|
2021-11-08 15:43:36 +00:00
|
|
|
s.log.Logger().Err(err).
|
2020-12-28 18:10:01 +00:00
|
|
|
Dur("refreshAuthRetryDuration", refreshAuthRetryDuration).
|
|
|
|
Msgf("supervisor: initial refreshAuth failed, retrying in %v", refreshAuthRetryDuration)
|
2019-12-06 21:32:15 +00:00
|
|
|
refreshAuthBackoffTimer = time.After(refreshAuthRetryDuration)
|
|
|
|
}
|
2019-12-04 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 00:07:49 +00:00
|
|
|
shuttingDown := false
|
2018-05-01 23:45:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Context cancelled
|
|
|
|
case <-ctx.Done():
|
|
|
|
for tunnelsActive > 0 {
|
|
|
|
<-s.tunnelErrors
|
|
|
|
tunnelsActive--
|
|
|
|
}
|
|
|
|
return nil
|
2022-05-20 21:51:36 +00:00
|
|
|
// startTunnel completed with a response
|
2018-05-01 23:45:06 +00:00
|
|
|
// (note that this may also be caused by context cancellation)
|
|
|
|
case tunnelError := <-s.tunnelErrors:
|
|
|
|
tunnelsActive--
|
2021-02-05 00:07:49 +00:00
|
|
|
if tunnelError.err != nil && !shuttingDown {
|
2022-05-20 21:51:36 +00:00
|
|
|
switch tunnelError.err.(type) {
|
|
|
|
case ReconnectSignal:
|
|
|
|
// For tunnels that closed with reconnect signal, we reconnect immediately
|
|
|
|
go s.startTunnel(ctx, tunnelError.index, s.newConnectedTunnelSignal(tunnelError.index))
|
|
|
|
tunnelsActive++
|
|
|
|
continue
|
|
|
|
}
|
2022-06-18 00:24:37 +00:00
|
|
|
// Make sure we don't continue if there is no more fallback allowed
|
|
|
|
if _, retry := s.tunnelsProtocolFallback[tunnelError.index].GetMaxBackoffDuration(ctx); !retry {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-08 15:43:36 +00:00
|
|
|
s.log.ConnAwareLogger().Err(tunnelError.err).Int(connection.LogFieldConnIndex, tunnelError.index).Msg("Connection terminated")
|
2018-05-01 23:45:06 +00:00
|
|
|
tunnelsWaiting = append(tunnelsWaiting, tunnelError.index)
|
|
|
|
s.waitForNextTunnel(tunnelError.index)
|
|
|
|
|
|
|
|
if backoffTimer == nil {
|
|
|
|
backoffTimer = backoff.BackoffTimer()
|
|
|
|
}
|
2021-01-20 19:41:09 +00:00
|
|
|
} else if tunnelsActive == 0 {
|
2022-05-20 21:51:36 +00:00
|
|
|
s.log.ConnAwareLogger().Msg("no more connections active and exiting")
|
|
|
|
// All connected tunnels exited gracefully, no more work to do
|
2021-01-20 19:41:09 +00:00
|
|
|
return nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
// Backoff was set and its timer expired
|
|
|
|
case <-backoffTimer:
|
|
|
|
backoffTimer = nil
|
|
|
|
for _, index := range tunnelsWaiting {
|
2021-01-20 19:41:09 +00:00
|
|
|
go s.startTunnel(ctx, index, s.newConnectedTunnelSignal(index))
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
tunnelsActive += len(tunnelsWaiting)
|
|
|
|
tunnelsWaiting = nil
|
2019-12-04 17:22:08 +00:00
|
|
|
// Time to call Authenticate
|
|
|
|
case <-refreshAuthBackoffTimer:
|
2020-08-18 10:14:14 +00:00
|
|
|
newTimer, err := s.reconnectCredentialManager.RefreshAuth(ctx, refreshAuthBackoff, s.authenticate)
|
2019-12-04 17:22:08 +00:00
|
|
|
if err != nil {
|
2021-11-08 15:43:36 +00:00
|
|
|
s.log.Logger().Err(err).Msg("supervisor: Authentication failed")
|
2019-12-04 17:22:08 +00:00
|
|
|
// Permanent failure. Leave the `select` without setting the
|
|
|
|
// channel to be non-null, so we'll never hit this case of the `select` again.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
refreshAuthBackoffTimer = newTimer
|
2018-05-01 23:45:06 +00:00
|
|
|
// Tunnel successfully connected
|
|
|
|
case <-s.nextConnectedSignal:
|
|
|
|
if !s.waitForNextTunnel(s.nextConnectedIndex) && len(tunnelsWaiting) == 0 {
|
|
|
|
// No more tunnels outstanding, clear backoff timer
|
|
|
|
backoff.SetGracePeriod()
|
|
|
|
}
|
2021-02-05 00:07:49 +00:00
|
|
|
case <-s.gracefulShutdownC:
|
|
|
|
shuttingDown = true
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:32:15 +00:00
|
|
|
// Returns nil if initialization succeeded, else the initialization error.
|
2022-05-20 21:51:36 +00:00
|
|
|
// Attempts here will be made to connect one tunnel, if successful, it will
|
|
|
|
// connect the available tunnels up to config.HAConnections.
|
2021-01-20 19:41:09 +00:00
|
|
|
func (s *Supervisor) initialize(
|
|
|
|
ctx context.Context,
|
|
|
|
connectedSignal *signal.Signal,
|
|
|
|
) error {
|
|
|
|
availableAddrs := s.edgeIPs.AvailableAddrs()
|
2019-12-13 23:05:21 +00:00
|
|
|
if s.config.HAConnections > availableAddrs {
|
2021-11-08 15:43:36 +00:00
|
|
|
s.log.Logger().Info().Msgf("You requested %d HA connections but I can give you at most %d.", s.config.HAConnections, availableAddrs)
|
2019-12-13 23:05:21 +00:00
|
|
|
s.config.HAConnections = availableAddrs
|
|
|
|
}
|
2022-06-18 00:24:37 +00:00
|
|
|
s.tunnelsProtocolFallback[0] = &protocolFallback{
|
2022-08-25 17:52:18 +00:00
|
|
|
retry.BackoffHandler{MaxRetries: s.config.Retries, RetryForever: true},
|
2022-06-18 00:24:37 +00:00
|
|
|
s.config.ProtocolSelector.Current(),
|
|
|
|
false,
|
|
|
|
}
|
2019-12-13 23:05:21 +00:00
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
go s.startFirstTunnel(ctx, connectedSignal)
|
2022-05-20 21:51:36 +00:00
|
|
|
|
|
|
|
// Wait for response from first tunnel before proceeding to attempt other HA edge tunnels
|
2018-05-01 23:45:06 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
<-s.tunnelErrors
|
2019-12-04 17:22:08 +00:00
|
|
|
return ctx.Err()
|
2018-05-01 23:45:06 +00:00
|
|
|
case tunnelError := <-s.tunnelErrors:
|
|
|
|
return tunnelError.err
|
2021-02-05 00:07:49 +00:00
|
|
|
case <-s.gracefulShutdownC:
|
|
|
|
return errEarlyShutdown
|
2019-03-04 19:48:56 +00:00
|
|
|
case <-connectedSignal.Wait():
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// At least one successful connection, so start the rest
|
|
|
|
for i := 1; i < s.config.HAConnections; i++ {
|
2022-06-18 00:24:37 +00:00
|
|
|
s.tunnelsProtocolFallback[i] = &protocolFallback{
|
2022-08-25 17:52:18 +00:00
|
|
|
retry.BackoffHandler{MaxRetries: s.config.Retries, RetryForever: true},
|
2022-06-18 00:24:37 +00:00
|
|
|
s.config.ProtocolSelector.Current(),
|
|
|
|
false,
|
|
|
|
}
|
2022-08-31 19:52:44 +00:00
|
|
|
go s.startTunnel(ctx, i, s.newConnectedTunnelSignal(i))
|
2018-05-01 23:45:06 +00:00
|
|
|
time.Sleep(registrationInterval)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// startTunnel starts the first tunnel connection. The resulting error will be sent on
|
|
|
|
// s.tunnelErrors. It will send a signal via connectedSignal if registration succeed
|
2021-01-20 19:41:09 +00:00
|
|
|
func (s *Supervisor) startFirstTunnel(
|
|
|
|
ctx context.Context,
|
|
|
|
connectedSignal *signal.Signal,
|
|
|
|
) {
|
2019-12-13 23:05:21 +00:00
|
|
|
var (
|
2022-05-20 21:51:36 +00:00
|
|
|
err error
|
2019-12-13 23:05:21 +00:00
|
|
|
)
|
2020-06-25 18:25:39 +00:00
|
|
|
const firstConnIndex = 0
|
2022-06-18 00:24:37 +00:00
|
|
|
isStaticEdge := len(s.config.EdgeAddrs) > 0
|
2018-05-01 23:45:06 +00:00
|
|
|
defer func() {
|
2022-06-02 17:57:37 +00:00
|
|
|
s.tunnelErrors <- tunnelError{index: firstConnIndex, err: err}
|
2018-05-01 23:45:06 +00:00
|
|
|
}()
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
// If the first tunnel disconnects, keep restarting it.
|
2022-06-18 00:24:37 +00:00
|
|
|
for {
|
|
|
|
err = s.edgeTunnelServer.Serve(ctx, firstConnIndex, s.tunnelsProtocolFallback[firstConnIndex], connectedSignal)
|
2019-12-13 23:05:21 +00:00
|
|
|
if ctx.Err() != nil {
|
2018-05-01 23:45:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-20 21:51:36 +00:00
|
|
|
if err == nil {
|
2018-05-01 23:45:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-18 00:24:37 +00:00
|
|
|
// Make sure we don't continue if there is no more fallback allowed
|
|
|
|
if _, retry := s.tunnelsProtocolFallback[firstConnIndex].GetMaxBackoffDuration(ctx); !retry {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Try again for Unauthorized errors because we hope them to be
|
|
|
|
// transient due to edge propagation lag on new Tunnels.
|
|
|
|
if strings.Contains(err.Error(), "Unauthorized") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case edgediscovery.ErrNoAddressesLeft:
|
|
|
|
// If your provided addresses are not available, we will keep trying regardless.
|
|
|
|
if !isStaticEdge {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case connection.DupConnRegisterTunnelError,
|
|
|
|
*quic.IdleTimeoutError,
|
|
|
|
edgediscovery.DialError,
|
|
|
|
*connection.EdgeQuicDialError:
|
|
|
|
// Try again for these types of errors
|
|
|
|
default:
|
|
|
|
// Uncaught errors should bail startup
|
|
|
|
return
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startTunnel starts a new tunnel connection. The resulting error will be sent on
|
2022-05-20 21:51:36 +00:00
|
|
|
// s.tunnelError as this is expected to run in a goroutine.
|
2021-01-20 19:41:09 +00:00
|
|
|
func (s *Supervisor) startTunnel(
|
|
|
|
ctx context.Context,
|
|
|
|
index int,
|
|
|
|
connectedSignal *signal.Signal,
|
|
|
|
) {
|
2019-12-13 23:05:21 +00:00
|
|
|
var (
|
2022-05-20 21:51:36 +00:00
|
|
|
err error
|
2019-12-13 23:05:21 +00:00
|
|
|
)
|
|
|
|
defer func() {
|
2022-06-02 17:57:37 +00:00
|
|
|
s.tunnelErrors <- tunnelError{index: index, err: err}
|
2019-12-13 23:05:21 +00:00
|
|
|
}()
|
|
|
|
|
2022-06-18 00:24:37 +00:00
|
|
|
err = s.edgeTunnelServer.Serve(ctx, uint8(index), s.tunnelsProtocolFallback[index], connectedSignal)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:48:56 +00:00
|
|
|
func (s *Supervisor) newConnectedTunnelSignal(index int) *signal.Signal {
|
|
|
|
sig := make(chan struct{})
|
|
|
|
s.tunnelsConnecting[index] = sig
|
|
|
|
s.nextConnectedSignal = sig
|
2018-05-01 23:45:06 +00:00
|
|
|
s.nextConnectedIndex = index
|
2019-03-04 19:48:56 +00:00
|
|
|
return signal.New(sig)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) waitForNextTunnel(index int) bool {
|
|
|
|
delete(s.tunnelsConnecting, index)
|
|
|
|
s.nextConnectedSignal = nil
|
|
|
|
for k, v := range s.tunnelsConnecting {
|
|
|
|
s.nextConnectedIndex = k
|
|
|
|
s.nextConnectedSignal = v
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-13 23:05:21 +00:00
|
|
|
func (s *Supervisor) unusedIPs() bool {
|
|
|
|
return s.edgeIPs.AvailableAddrs() > s.config.HAConnections
|
2019-06-17 21:18:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
func (s *Supervisor) authenticate(ctx context.Context, numPreviousAttempts int) (tunnelpogs.AuthOutcome, error) {
|
2020-02-06 00:55:26 +00:00
|
|
|
arbitraryEdgeIP, err := s.edgeIPs.GetAddrForRPC()
|
2019-12-13 23:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:31:22 +00:00
|
|
|
edgeConn, err := edgediscovery.DialEdge(ctx, dialTimeout, s.config.EdgeTLSConfigs[connection.H2mux], arbitraryEdgeIP.TCP)
|
2019-12-04 17:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer edgeConn.Close()
|
|
|
|
|
|
|
|
handler := h2mux.MuxedStreamFunc(func(*h2mux.MuxedStream) error {
|
|
|
|
// This callback is invoked by h2mux when the edge initiates a stream.
|
|
|
|
return nil // noop
|
|
|
|
})
|
2021-02-03 18:32:54 +00:00
|
|
|
muxerConfig := s.config.MuxerConfig.H2MuxerConfig(handler, s.logTransport)
|
2020-10-08 10:12:26 +00:00
|
|
|
muxer, err := h2mux.Handshake(edgeConn, edgeConn, *muxerConfig, h2mux.ActiveStreams)
|
2019-12-04 17:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go muxer.Serve(ctx)
|
|
|
|
defer func() {
|
|
|
|
// If we don't wait for the muxer shutdown here, edgeConn.Close() runs before the muxer connections are done,
|
|
|
|
// and the user sees log noise: "error writing data", "connection closed unexpectedly"
|
|
|
|
<-muxer.Shutdown()
|
|
|
|
}()
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
stream, err := muxer.OpenRPCStream(ctx)
|
2019-12-04 17:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-08 15:43:36 +00:00
|
|
|
rpcClient := connection.NewTunnelServerClient(ctx, stream, s.log.Logger())
|
2020-09-28 09:10:30 +00:00
|
|
|
defer rpcClient.Close()
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
const arbitraryConnectionID = uint8(0)
|
2022-02-07 09:42:07 +00:00
|
|
|
registrationOptions := s.config.registrationOptions(arbitraryConnectionID, edgeConn.LocalAddr().String(), s.cloudflaredUUID)
|
2019-12-04 17:22:08 +00:00
|
|
|
registrationOptions.NumPreviousAttempts = uint8(numPreviousAttempts)
|
2020-10-08 10:12:26 +00:00
|
|
|
return rpcClient.Authenticate(ctx, s.config.ClassicTunnel, registrationOptions)
|
2019-12-04 17:22:08 +00:00
|
|
|
}
|