2019-11-21 17:03:13 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-08 10:12:26 +00:00
|
|
|
"fmt"
|
2020-09-28 09:10:30 +00:00
|
|
|
"io"
|
2020-10-27 22:27:15 +00:00
|
|
|
"time"
|
2019-11-21 17:03:13 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-08 10:12:26 +00:00
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc"
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2019-11-21 17:03:13 +00:00
|
|
|
)
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
type tunnelServerClient struct {
|
|
|
|
client tunnelpogs.TunnelServer_PogsClient
|
|
|
|
transport rpc.Transport
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTunnelRPCClient creates and returns a new RPC client, which will communicate using a stream on the given muxer.
|
|
|
|
// This method is exported for supervisor to call Authenticate RPC
|
|
|
|
func NewTunnelServerClient(
|
2019-11-21 17:03:13 +00:00
|
|
|
ctx context.Context,
|
2020-09-28 09:10:30 +00:00
|
|
|
stream io.ReadWriteCloser,
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger,
|
2020-10-08 10:12:26 +00:00
|
|
|
) *tunnelServerClient {
|
2020-11-25 06:55:13 +00:00
|
|
|
transport := tunnelrpc.NewTransportLogger(log, rpc.StreamTransport(stream))
|
2019-11-21 17:03:13 +00:00
|
|
|
conn := rpc.NewConn(
|
2020-10-08 10:12:26 +00:00
|
|
|
transport,
|
2020-11-25 06:55:13 +00:00
|
|
|
tunnelrpc.ConnLog(log),
|
2019-11-21 17:03:13 +00:00
|
|
|
)
|
2020-09-28 09:10:30 +00:00
|
|
|
registrationClient := tunnelpogs.RegistrationServer_PogsClient{Client: conn.Bootstrap(ctx), Conn: conn}
|
2020-10-08 10:12:26 +00:00
|
|
|
return &tunnelServerClient{
|
|
|
|
client: tunnelpogs.TunnelServer_PogsClient{RegistrationServer_PogsClient: registrationClient, Client: conn.Bootstrap(ctx), Conn: conn},
|
|
|
|
transport: transport,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
func (tsc *tunnelServerClient) Authenticate(ctx context.Context, classicTunnel *ClassicTunnelProperties, registrationOptions *tunnelpogs.RegistrationOptions) (tunnelpogs.AuthOutcome, error) {
|
2020-10-08 10:12:26 +00:00
|
|
|
authResp, err := tsc.client.Authenticate(ctx, classicTunnel.OriginCert, classicTunnel.Hostname, registrationOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return authResp.Outcome(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsc *tunnelServerClient) Close() {
|
|
|
|
// Closing the client will also close the connection
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = tsc.client.Close()
|
|
|
|
_ = tsc.transport.Close()
|
2019-11-21 17:03:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
type NamedTunnelRPCClient interface {
|
|
|
|
RegisterConnection(
|
|
|
|
c context.Context,
|
2022-02-07 09:42:07 +00:00
|
|
|
config *NamedTunnelProperties,
|
2020-10-27 22:27:15 +00:00
|
|
|
options *tunnelpogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
|
|
|
observer *Observer,
|
|
|
|
) error
|
|
|
|
GracefulShutdown(ctx context.Context, gracePeriod time.Duration)
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
type registrationServerClient struct {
|
|
|
|
client tunnelpogs.RegistrationServer_PogsClient
|
|
|
|
transport rpc.Transport
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRegistrationRPCClient(
|
2020-09-28 09:10:30 +00:00
|
|
|
ctx context.Context,
|
|
|
|
stream io.ReadWriteCloser,
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger,
|
2020-10-27 22:27:15 +00:00
|
|
|
) NamedTunnelRPCClient {
|
2020-11-25 06:55:13 +00:00
|
|
|
transport := tunnelrpc.NewTransportLogger(log, rpc.StreamTransport(stream))
|
2020-09-28 09:10:30 +00:00
|
|
|
conn := rpc.NewConn(
|
2020-10-08 10:12:26 +00:00
|
|
|
transport,
|
2020-11-25 06:55:13 +00:00
|
|
|
tunnelrpc.ConnLog(log),
|
2020-09-28 09:10:30 +00:00
|
|
|
)
|
2020-10-08 10:12:26 +00:00
|
|
|
return ®istrationServerClient{
|
|
|
|
client: tunnelpogs.RegistrationServer_PogsClient{Client: conn.Bootstrap(ctx), Conn: conn},
|
|
|
|
transport: transport,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (rsc *registrationServerClient) RegisterConnection(
|
2020-10-08 10:12:26 +00:00
|
|
|
ctx context.Context,
|
2022-02-07 09:42:07 +00:00
|
|
|
properties *NamedTunnelProperties,
|
2020-10-08 10:12:26 +00:00
|
|
|
options *tunnelpogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
|
|
|
observer *Observer,
|
|
|
|
) error {
|
2020-10-27 22:27:15 +00:00
|
|
|
conn, err := rsc.client.RegisterConnection(
|
2020-10-08 10:12:26 +00:00
|
|
|
ctx,
|
2022-02-07 09:42:07 +00:00
|
|
|
properties.Credentials.Auth(),
|
|
|
|
properties.Credentials.TunnelID,
|
2020-10-08 10:12:26 +00:00
|
|
|
connIndex,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == DuplicateConnectionError {
|
|
|
|
observer.metrics.regFail.WithLabelValues("dup_edge_conn", "registerConnection").Inc()
|
|
|
|
return errDuplicationConnection
|
|
|
|
}
|
|
|
|
observer.metrics.regFail.WithLabelValues("server_error", "registerConnection").Inc()
|
|
|
|
return serverRegistrationErrorFromRPC(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.metrics.regSuccess.WithLabelValues("registerConnection").Inc()
|
|
|
|
|
2020-12-28 18:10:01 +00:00
|
|
|
observer.logServerInfo(connIndex, conn.Location, fmt.Sprintf("Connection %s registered", conn.UUID))
|
2020-10-08 10:12:26 +00:00
|
|
|
observer.sendConnectedEvent(connIndex, conn.Location)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (rsc *registrationServerClient) GracefulShutdown(ctx context.Context, gracePeriod time.Duration) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, gracePeriod)
|
|
|
|
defer cancel()
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = rsc.client.UnregisterConnection(ctx)
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rsc *registrationServerClient) Close() {
|
|
|
|
// Closing the client will also close the connection
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = rsc.client.Close()
|
2020-10-27 22:27:15 +00:00
|
|
|
// Closing the transport also closes the stream
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = rsc.transport.Close()
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rpcName string
|
|
|
|
|
|
|
|
const (
|
|
|
|
register rpcName = "register"
|
|
|
|
reconnect rpcName = "reconnect"
|
|
|
|
unregister rpcName = "unregister"
|
|
|
|
authenticate rpcName = " authenticate"
|
|
|
|
)
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
func (h *h2muxConnection) registerTunnel(ctx context.Context, credentialSetter CredentialManager, classicTunnel *ClassicTunnelProperties, registrationOptions *tunnelpogs.RegistrationOptions) error {
|
2021-01-19 12:20:11 +00:00
|
|
|
h.observer.sendRegisteringEvent(registrationOptions.ConnectionID)
|
2020-10-08 10:12:26 +00:00
|
|
|
|
|
|
|
stream, err := h.newRPCStream(ctx, register)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
rpcClient := NewTunnelServerClient(ctx, stream, h.observer.log)
|
2020-10-08 10:12:26 +00:00
|
|
|
defer rpcClient.Close()
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = h.logServerInfo(ctx, rpcClient)
|
2020-10-08 10:12:26 +00:00
|
|
|
registration := rpcClient.client.RegisterTunnel(
|
|
|
|
ctx,
|
|
|
|
classicTunnel.OriginCert,
|
|
|
|
classicTunnel.Hostname,
|
|
|
|
registrationOptions,
|
|
|
|
)
|
|
|
|
if registrationErr := registration.DeserializeError(); registrationErr != nil {
|
|
|
|
// RegisterTunnel RPC failure
|
|
|
|
return h.processRegisterTunnelError(registrationErr, register)
|
|
|
|
}
|
|
|
|
|
|
|
|
credentialSetter.SetEventDigest(h.connIndex, registration.EventDigest)
|
|
|
|
return h.processRegistrationSuccess(registration, register, credentialSetter, classicTunnel)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CredentialManager interface {
|
|
|
|
ReconnectToken() ([]byte, error)
|
|
|
|
EventDigest(connID uint8) ([]byte, error)
|
|
|
|
SetEventDigest(connID uint8, digest []byte)
|
|
|
|
ConnDigest(connID uint8) ([]byte, error)
|
|
|
|
SetConnDigest(connID uint8, digest []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *h2muxConnection) processRegistrationSuccess(
|
|
|
|
registration *tunnelpogs.TunnelRegistration,
|
|
|
|
name rpcName,
|
2022-02-07 09:42:07 +00:00
|
|
|
credentialManager CredentialManager, classicTunnel *ClassicTunnelProperties,
|
2020-10-08 10:12:26 +00:00
|
|
|
) error {
|
|
|
|
for _, logLine := range registration.LogLines {
|
2020-11-25 06:55:13 +00:00
|
|
|
h.observer.log.Info().Msg(logLine)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if registration.TunnelID != "" {
|
|
|
|
h.observer.metrics.tunnelsHA.AddTunnelID(h.connIndex, registration.TunnelID)
|
2020-11-25 06:55:13 +00:00
|
|
|
h.observer.log.Info().Msgf("Each HA connection's tunnel IDs: %v", h.observer.metrics.tunnelsHA.String())
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
credentialManager.SetConnDigest(h.connIndex, registration.ConnDigest)
|
|
|
|
h.observer.metrics.userHostnamesCounts.WithLabelValues(registration.Url).Inc()
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
h.observer.log.Info().Msgf("Route propagating, it may take up to 1 minute for your new route to become functional")
|
2020-10-08 10:12:26 +00:00
|
|
|
h.observer.metrics.regSuccess.WithLabelValues(string(name)).Inc()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *h2muxConnection) processRegisterTunnelError(err tunnelpogs.TunnelRegistrationError, name rpcName) error {
|
|
|
|
if err.Error() == DuplicateConnectionError {
|
|
|
|
h.observer.metrics.regFail.WithLabelValues("dup_edge_conn", string(name)).Inc()
|
|
|
|
return errDuplicationConnection
|
|
|
|
}
|
|
|
|
h.observer.metrics.regFail.WithLabelValues("server_error", string(name)).Inc()
|
2021-01-20 17:52:35 +00:00
|
|
|
return ServerRegisterTunnelError{
|
|
|
|
Cause: err,
|
|
|
|
Permanent: err.IsPermanent(),
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
func (h *h2muxConnection) reconnectTunnel(ctx context.Context, credentialManager CredentialManager, classicTunnel *ClassicTunnelProperties, registrationOptions *tunnelpogs.RegistrationOptions) error {
|
2020-10-08 10:12:26 +00:00
|
|
|
token, err := credentialManager.ReconnectToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
eventDigest, err := credentialManager.EventDigest(h.connIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
connDigest, err := credentialManager.ConnDigest(h.connIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
h.observer.log.Debug().Msg("initiating RPC stream to reconnect")
|
2020-10-08 10:12:26 +00:00
|
|
|
stream, err := h.newRPCStream(ctx, register)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
rpcClient := NewTunnelServerClient(ctx, stream, h.observer.log)
|
2020-10-08 10:12:26 +00:00
|
|
|
defer rpcClient.Close()
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = h.logServerInfo(ctx, rpcClient)
|
2020-10-08 10:12:26 +00:00
|
|
|
registration := rpcClient.client.ReconnectTunnel(
|
|
|
|
ctx,
|
|
|
|
token,
|
|
|
|
eventDigest,
|
|
|
|
connDigest,
|
|
|
|
classicTunnel.Hostname,
|
|
|
|
registrationOptions,
|
|
|
|
)
|
|
|
|
if registrationErr := registration.DeserializeError(); registrationErr != nil {
|
|
|
|
// ReconnectTunnel RPC failure
|
|
|
|
return h.processRegisterTunnelError(registrationErr, reconnect)
|
|
|
|
}
|
|
|
|
return h.processRegistrationSuccess(registration, reconnect, credentialManager, classicTunnel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *h2muxConnection) logServerInfo(ctx context.Context, rpcClient *tunnelServerClient) error {
|
|
|
|
// Request server info without blocking tunnel registration; must use capnp library directly.
|
|
|
|
serverInfoPromise := tunnelrpc.TunnelServer{Client: rpcClient.client.Client}.GetServerInfo(ctx, func(tunnelrpc.TunnelServer_getServerInfo_Params) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
serverInfoMessage, err := serverInfoPromise.Result().Struct()
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
h.observer.log.Err(err).Msg("Failed to retrieve server information")
|
2020-10-08 10:12:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
serverInfo, err := tunnelpogs.UnmarshalServerInfo(serverInfoMessage)
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
h.observer.log.Err(err).Msg("Failed to retrieve server information")
|
2020-10-08 10:12:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-12-28 18:10:01 +00:00
|
|
|
h.observer.logServerInfo(h.connIndex, serverInfo.LocationName, "Connection established")
|
2020-10-08 10:12:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
func (h *h2muxConnection) registerNamedTunnel(
|
|
|
|
ctx context.Context,
|
2022-02-07 09:42:07 +00:00
|
|
|
namedTunnel *NamedTunnelProperties,
|
2021-01-20 19:41:09 +00:00
|
|
|
connOptions *tunnelpogs.ConnectionOptions,
|
|
|
|
) error {
|
|
|
|
stream, err := h.newRPCStream(ctx, register)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rpcClient := h.newRPCClientFunc(ctx, stream, h.observer.log)
|
|
|
|
defer rpcClient.Close()
|
|
|
|
|
|
|
|
if err = rpcClient.RegisterConnection(ctx, namedTunnel, connOptions, h.connIndex, h.observer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func (h *h2muxConnection) unregister(isNamedTunnel bool) {
|
2021-02-04 21:09:17 +00:00
|
|
|
h.observer.sendUnregisteringEvent(h.connIndex)
|
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
unregisterCtx, cancel := context.WithTimeout(context.Background(), h.gracePeriod)
|
2020-10-08 10:12:26 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
stream, err := h.newRPCStream(unregisterCtx, unregister)
|
2020-10-08 10:12:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-20 19:41:09 +00:00
|
|
|
defer stream.Close()
|
2020-10-08 10:12:26 +00:00
|
|
|
|
|
|
|
if isNamedTunnel {
|
2021-01-20 19:41:09 +00:00
|
|
|
rpcClient := h.newRPCClientFunc(unregisterCtx, stream, h.observer.log)
|
2020-10-27 22:27:15 +00:00
|
|
|
defer rpcClient.Close()
|
2020-10-08 10:12:26 +00:00
|
|
|
|
2022-02-07 09:42:07 +00:00
|
|
|
rpcClient.GracefulShutdown(unregisterCtx, h.gracePeriod)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
rpcClient := NewTunnelServerClient(unregisterCtx, stream, h.observer.log)
|
2020-10-08 10:12:26 +00:00
|
|
|
defer rpcClient.Close()
|
|
|
|
|
|
|
|
// gracePeriod is encoded in int64 using capnproto
|
2022-02-07 09:42:07 +00:00
|
|
|
_ = rpcClient.client.UnregisterTunnel(unregisterCtx, h.gracePeriod.Nanoseconds())
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
h.observer.log.Info().Uint8(LogFieldConnIndex, h.connIndex).Msg("Unregistered tunnel connection")
|
2019-11-21 17:03:13 +00:00
|
|
|
}
|