2018-05-01 23:45:06 +00:00
|
|
|
package origin
|
|
|
|
|
|
|
|
import (
|
2019-01-10 20:55:44 +00:00
|
|
|
"context"
|
2019-12-04 17:22:08 +00:00
|
|
|
"errors"
|
2018-05-01 23:45:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-12-04 17:22:08 +00:00
|
|
|
"sync"
|
2018-05-01 23:45:06 +00:00
|
|
|
"time"
|
2019-02-01 20:11:12 +00:00
|
|
|
|
2019-11-21 17:03:13 +00:00
|
|
|
"github.com/google/uuid"
|
2019-06-17 21:18:47 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-02-24 17:06:19 +00:00
|
|
|
"github.com/cloudflare/cloudflared/buffer"
|
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"
|
2019-12-04 17:22:08 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
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"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Waiting time before retrying a failed tunnel connection
|
|
|
|
tunnelRetryDuration = time.Second * 10
|
|
|
|
// SRV record resolution TTL
|
|
|
|
resolveTTL = time.Hour
|
|
|
|
// 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
|
2019-12-06 21:32:15 +00:00
|
|
|
// Maximum time to make an Authenticate RPC
|
|
|
|
authTokenTimeout = time.Second * 30
|
2019-12-04 17:22:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errJWTUnset = errors.New("JWT unset")
|
|
|
|
errEventDigestUnset = errors.New("event digest unset")
|
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 {
|
2019-12-13 23:05:21 +00:00
|
|
|
cloudflaredUUID uuid.UUID
|
|
|
|
config *TunnelConfig
|
2020-02-06 00:55:26 +00:00
|
|
|
edgeIPs *edgediscovery.Edge
|
2018-05-01 23:45:06 +00:00
|
|
|
lastResolve time.Time
|
|
|
|
resolverC chan resolveResult
|
|
|
|
tunnelErrors chan tunnelError
|
|
|
|
tunnelsConnecting map[int]chan struct{}
|
|
|
|
// 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
|
|
|
|
|
|
|
logger *logrus.Entry
|
2019-12-04 17:22:08 +00:00
|
|
|
|
|
|
|
jwtLock *sync.RWMutex
|
|
|
|
jwt []byte
|
|
|
|
|
|
|
|
eventDigestLock *sync.RWMutex
|
|
|
|
eventDigest []byte
|
2020-02-24 17:06:19 +00:00
|
|
|
|
|
|
|
bufferPool *buffer.Pool
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type resolveResult struct {
|
2019-12-13 23:05:21 +00:00
|
|
|
err error
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type tunnelError struct {
|
|
|
|
index int
|
2019-12-13 23:05:21 +00:00
|
|
|
addr *net.TCPAddr
|
2018-05-01 23:45:06 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2019-12-13 23:05:21 +00:00
|
|
|
func NewSupervisor(config *TunnelConfig, u uuid.UUID) (*Supervisor, error) {
|
2019-12-24 05:11:00 +00:00
|
|
|
var (
|
2020-02-06 00:55:26 +00:00
|
|
|
edgeIPs *edgediscovery.Edge
|
2019-12-24 05:11:00 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
if len(config.EdgeAddrs) > 0 {
|
2020-02-06 00:55:26 +00:00
|
|
|
edgeIPs, err = edgediscovery.StaticEdge(config.Logger, config.EdgeAddrs)
|
2019-12-24 05:11:00 +00:00
|
|
|
} else {
|
2020-02-06 00:55:26 +00:00
|
|
|
edgeIPs, err = edgediscovery.ResolveEdge(config.Logger)
|
2019-12-24 05:11:00 +00:00
|
|
|
}
|
2019-12-13 23:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
return &Supervisor{
|
2019-12-04 17:22:08 +00:00
|
|
|
cloudflaredUUID: u,
|
2018-05-01 23:45:06 +00:00
|
|
|
config: config,
|
2019-12-13 23:05:21 +00:00
|
|
|
edgeIPs: edgeIPs,
|
2018-05-01 23:45:06 +00:00
|
|
|
tunnelErrors: make(chan tunnelError),
|
|
|
|
tunnelsConnecting: map[int]chan struct{}{},
|
2019-06-17 21:18:47 +00:00
|
|
|
logger: config.Logger.WithField("subsystem", "supervisor"),
|
2019-12-04 17:22:08 +00:00
|
|
|
jwtLock: &sync.RWMutex{},
|
|
|
|
eventDigestLock: &sync.RWMutex{},
|
2020-02-24 17:06:19 +00:00
|
|
|
bufferPool: buffer.NewPool(512 * 1024),
|
2019-12-13 23:05:21 +00:00
|
|
|
}, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
func (s *Supervisor) Run(ctx context.Context, connectedSignal *signal.Signal) error {
|
2018-05-01 23:45:06 +00:00
|
|
|
logger := s.config.Logger
|
2019-12-04 17:22:08 +00:00
|
|
|
if err := s.initialize(ctx, connectedSignal); err != 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
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
backoff := BackoffHandler{MaxRetries: s.config.Retries, BaseTime: tunnelRetryDuration, RetryForever: true}
|
|
|
|
var backoffTimer <-chan time.Time
|
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
refreshAuthBackoff := &BackoffHandler{MaxRetries: refreshAuthMaxBackoff, BaseTime: refreshAuthRetryDuration, RetryForever: true}
|
|
|
|
var refreshAuthBackoffTimer <-chan time.Time
|
2019-12-06 21:32:15 +00:00
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
if s.config.UseReconnectToken {
|
2019-12-06 21:32:15 +00:00
|
|
|
if timer, err := s.refreshAuth(ctx, refreshAuthBackoff, s.authenticate); err == nil {
|
|
|
|
refreshAuthBackoffTimer = timer
|
|
|
|
} else {
|
|
|
|
logger.WithError(err).Errorf("initial refreshAuth failed, retrying in %v", refreshAuthRetryDuration)
|
|
|
|
refreshAuthBackoffTimer = time.After(refreshAuthRetryDuration)
|
|
|
|
}
|
2019-12-04 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Context cancelled
|
|
|
|
case <-ctx.Done():
|
|
|
|
for tunnelsActive > 0 {
|
|
|
|
<-s.tunnelErrors
|
|
|
|
tunnelsActive--
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
// startTunnel returned with error
|
|
|
|
// (note that this may also be caused by context cancellation)
|
|
|
|
case tunnelError := <-s.tunnelErrors:
|
|
|
|
tunnelsActive--
|
|
|
|
if tunnelError.err != nil {
|
|
|
|
logger.WithError(tunnelError.err).Warn("Tunnel disconnected due to error")
|
|
|
|
tunnelsWaiting = append(tunnelsWaiting, tunnelError.index)
|
|
|
|
s.waitForNextTunnel(tunnelError.index)
|
|
|
|
|
|
|
|
if backoffTimer == nil {
|
|
|
|
backoffTimer = backoff.BackoffTimer()
|
|
|
|
}
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
// Previously we'd mark the edge address as bad here, but now we'll just silently use
|
|
|
|
// another.
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
// Backoff was set and its timer expired
|
|
|
|
case <-backoffTimer:
|
|
|
|
backoffTimer = nil
|
|
|
|
for _, index := range tunnelsWaiting {
|
2019-12-04 17:22:08 +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:
|
|
|
|
newTimer, err := s.refreshAuth(ctx, refreshAuthBackoff, s.authenticate)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Authentication failed")
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
// DNS resolution returned
|
|
|
|
case result := <-s.resolverC:
|
|
|
|
s.lastResolve = time.Now()
|
|
|
|
s.resolverC = nil
|
|
|
|
if result.err == nil {
|
|
|
|
logger.Debug("Service discovery refresh complete")
|
|
|
|
} else {
|
|
|
|
logger.WithError(result.err).Error("Service discovery error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:32:15 +00:00
|
|
|
// Returns nil if initialization succeeded, else the initialization error.
|
2019-12-04 17:22:08 +00:00
|
|
|
func (s *Supervisor) initialize(ctx context.Context, connectedSignal *signal.Signal) error {
|
2019-06-17 21:18:47 +00:00
|
|
|
logger := s.logger
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
s.lastResolve = time.Now()
|
2019-12-13 23:05:21 +00:00
|
|
|
availableAddrs := int(s.edgeIPs.AvailableAddrs())
|
|
|
|
if s.config.HAConnections > availableAddrs {
|
|
|
|
logger.Warnf("You requested %d HA connections but I can give you at most %d.", s.config.HAConnections, availableAddrs)
|
|
|
|
s.config.HAConnections = availableAddrs
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
go s.startFirstTunnel(ctx, connectedSignal)
|
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
|
2019-03-04 19:48:56 +00:00
|
|
|
case <-connectedSignal.Wait():
|
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++ {
|
2019-03-04 19:48:56 +00:00
|
|
|
ch := signal.New(make(chan struct{}))
|
2019-12-04 17:22:08 +00:00
|
|
|
go s.startTunnel(ctx, i, ch)
|
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
|
2019-12-04 17:22:08 +00:00
|
|
|
func (s *Supervisor) startFirstTunnel(ctx context.Context, connectedSignal *signal.Signal) {
|
2019-12-13 23:05:21 +00:00
|
|
|
var (
|
|
|
|
addr *net.TCPAddr
|
|
|
|
err error
|
|
|
|
)
|
2020-02-06 00:55:26 +00:00
|
|
|
const thisConnID = 0
|
2018-05-01 23:45:06 +00:00
|
|
|
defer func() {
|
2020-02-06 00:55:26 +00:00
|
|
|
s.tunnelErrors <- tunnelError{index: thisConnID, addr: addr, err: err}
|
2018-05-01 23:45:06 +00:00
|
|
|
}()
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
addr, err = s.edgeIPs.GetAddr(thisConnID)
|
2019-12-13 23:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:06:19 +00:00
|
|
|
err = ServeTunnelLoop(ctx, s, s.config, addr, thisConnID, connectedSignal, s.cloudflaredUUID, s.bufferPool)
|
2020-02-06 00:55:26 +00:00
|
|
|
// If the first tunnel disconnects, keep restarting it.
|
|
|
|
edgeErrors := 0
|
2018-05-01 23:45:06 +00:00
|
|
|
for s.unusedIPs() {
|
2019-12-13 23:05:21 +00:00
|
|
|
if ctx.Err() != nil {
|
2018-05-01 23:45:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
return
|
|
|
|
// try the next address if it was a dialError(network problem) or
|
|
|
|
// dupConnRegisterTunnelError
|
2019-11-21 18:10:44 +00:00
|
|
|
case connection.DialError, dupConnRegisterTunnelError:
|
2020-02-06 00:55:26 +00:00
|
|
|
edgeErrors++
|
2018-05-01 23:45:06 +00:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2020-02-06 00:55:26 +00:00
|
|
|
if edgeErrors >= 2 {
|
|
|
|
addr, err = s.edgeIPs.GetDifferentAddr(thisConnID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-13 23:05:21 +00:00
|
|
|
}
|
2020-02-24 17:06:19 +00:00
|
|
|
err = ServeTunnelLoop(ctx, s, s.config, addr, thisConnID, connectedSignal, s.cloudflaredUUID, s.bufferPool)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startTunnel starts a new tunnel connection. The resulting error will be sent on
|
|
|
|
// s.tunnelErrors.
|
2019-12-04 17:22:08 +00:00
|
|
|
func (s *Supervisor) startTunnel(ctx context.Context, index int, connectedSignal *signal.Signal) {
|
2019-12-13 23:05:21 +00:00
|
|
|
var (
|
|
|
|
addr *net.TCPAddr
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
s.tunnelErrors <- tunnelError{index: index, addr: addr, err: err}
|
|
|
|
}()
|
|
|
|
|
2020-02-06 00:55:26 +00:00
|
|
|
addr, err = s.edgeIPs.GetAddr(index)
|
2019-12-13 23:05:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-24 17:06:19 +00:00
|
|
|
err = ServeTunnelLoop(ctx, s, s.config, addr, uint8(index), connectedSignal, s.cloudflaredUUID, s.bufferPool)
|
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) ReconnectToken() ([]byte, error) {
|
|
|
|
s.jwtLock.RLock()
|
|
|
|
defer s.jwtLock.RUnlock()
|
|
|
|
if s.jwt == nil {
|
|
|
|
return nil, errJWTUnset
|
|
|
|
}
|
|
|
|
return s.jwt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) SetReconnectToken(jwt []byte) {
|
|
|
|
s.jwtLock.Lock()
|
|
|
|
defer s.jwtLock.Unlock()
|
|
|
|
s.jwt = jwt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) EventDigest() ([]byte, error) {
|
|
|
|
s.eventDigestLock.RLock()
|
|
|
|
defer s.eventDigestLock.RUnlock()
|
|
|
|
if s.eventDigest == nil {
|
|
|
|
return nil, errEventDigestUnset
|
|
|
|
}
|
|
|
|
return s.eventDigest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) SetEventDigest(eventDigest []byte) {
|
|
|
|
s.eventDigestLock.Lock()
|
|
|
|
defer s.eventDigestLock.Unlock()
|
|
|
|
s.eventDigest = eventDigest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) refreshAuth(
|
|
|
|
ctx context.Context,
|
|
|
|
backoff *BackoffHandler,
|
|
|
|
authenticate func(ctx context.Context, numPreviousAttempts int) (tunnelpogs.AuthOutcome, error),
|
|
|
|
) (retryTimer <-chan time.Time, err error) {
|
|
|
|
logger := s.config.Logger.WithField("subsystem", subsystemRefreshAuth)
|
|
|
|
authOutcome, err := authenticate(ctx, backoff.Retries())
|
|
|
|
if err != nil {
|
2020-01-28 18:21:51 +00:00
|
|
|
s.config.Metrics.authFail.WithLabelValues(err.Error()).Inc()
|
2019-12-04 17:22:08 +00:00
|
|
|
if duration, ok := backoff.GetBackoffDuration(ctx); ok {
|
|
|
|
logger.WithError(err).Warnf("Retrying in %v", duration)
|
|
|
|
return backoff.BackoffTimer(), nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// clear backoff timer
|
|
|
|
backoff.SetGracePeriod()
|
|
|
|
|
|
|
|
switch outcome := authOutcome.(type) {
|
|
|
|
case tunnelpogs.AuthSuccess:
|
|
|
|
s.SetReconnectToken(outcome.JWT())
|
2020-01-28 18:21:51 +00:00
|
|
|
s.config.Metrics.authSuccess.Inc()
|
2019-12-04 17:22:08 +00:00
|
|
|
return timeAfter(outcome.RefreshAfter()), nil
|
|
|
|
case tunnelpogs.AuthUnknown:
|
2019-12-06 21:32:15 +00:00
|
|
|
duration := outcome.RefreshAfter()
|
2020-01-28 18:21:51 +00:00
|
|
|
s.config.Metrics.authFail.WithLabelValues(outcome.Error()).Inc()
|
2019-12-06 21:32:15 +00:00
|
|
|
logger.WithError(outcome).Warnf("Retrying in %v", duration)
|
|
|
|
return timeAfter(duration), nil
|
2019-12-04 17:22:08 +00:00
|
|
|
case tunnelpogs.AuthFail:
|
2020-01-28 18:21:51 +00:00
|
|
|
s.config.Metrics.authFail.WithLabelValues(outcome.Error()).Inc()
|
2019-12-04 17:22:08 +00:00
|
|
|
return nil, outcome
|
|
|
|
default:
|
2020-01-28 18:21:51 +00:00
|
|
|
err := fmt.Errorf("Unexpected outcome type %T", authOutcome)
|
|
|
|
s.config.Metrics.authFail.WithLabelValues(err.Error()).Inc()
|
|
|
|
return nil, err
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:22:08 +00:00
|
|
|
edgeConn, err := connection.DialEdge(ctx, dialTimeout, s.config.TlsConfig, arbitraryEdgeIP)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
muxerConfig := s.config.muxerConfig(handler)
|
|
|
|
muxerConfig.Logger = muxerConfig.Logger.WithField("subsystem", subsystemRefreshAuth)
|
|
|
|
muxer, err := h2mux.Handshake(edgeConn, edgeConn, muxerConfig, s.config.Metrics.activeStreams)
|
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
tunnelServer, err := connection.NewRPCClient(ctx, muxer, s.logger.WithField("subsystem", subsystemRefreshAuth), openStreamTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tunnelServer.Close()
|
|
|
|
|
|
|
|
const arbitraryConnectionID = uint8(0)
|
|
|
|
registrationOptions := s.config.RegistrationOptions(arbitraryConnectionID, edgeConn.LocalAddr().String(), s.cloudflaredUUID)
|
|
|
|
registrationOptions.NumPreviousAttempts = uint8(numPreviousAttempts)
|
|
|
|
authResponse, err := tunnelServer.Authenticate(
|
|
|
|
ctx,
|
|
|
|
s.config.OriginCert,
|
|
|
|
s.config.Hostname,
|
|
|
|
registrationOptions,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return authResponse.Outcome(), nil
|
|
|
|
}
|