2024-05-24 18:40:10 +00:00
|
|
|
package tunnelrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
|
|
|
|
2024-05-28 21:14:25 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/metrics"
|
2024-05-24 18:40:10 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RegistrationClient interface {
|
|
|
|
RegisterConnection(
|
|
|
|
ctx context.Context,
|
|
|
|
auth pogs.TunnelAuth,
|
|
|
|
tunnelID uuid.UUID,
|
|
|
|
options *pogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
|
|
|
edgeAddress net.IP,
|
|
|
|
) (*pogs.ConnectionDetails, error)
|
|
|
|
SendLocalConfiguration(ctx context.Context, config []byte) error
|
2024-09-10 15:47:36 +00:00
|
|
|
GracefulShutdown(ctx context.Context, gracePeriod time.Duration)
|
2024-05-24 18:40:10 +00:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type registrationClient struct {
|
|
|
|
client pogs.RegistrationServer_PogsClient
|
|
|
|
transport rpc.Transport
|
|
|
|
requestTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRegistrationClient(ctx context.Context, stream io.ReadWriteCloser, requestTimeout time.Duration) RegistrationClient {
|
|
|
|
transport := SafeTransport(stream)
|
2024-06-26 20:34:49 +00:00
|
|
|
conn := NewClientConn(transport)
|
2024-05-24 18:40:10 +00:00
|
|
|
client := pogs.NewRegistrationServer_PogsClient(conn.Bootstrap(ctx), conn)
|
|
|
|
return ®istrationClient{
|
|
|
|
client: client,
|
|
|
|
transport: transport,
|
|
|
|
requestTimeout: requestTimeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *registrationClient) RegisterConnection(
|
|
|
|
ctx context.Context,
|
|
|
|
auth pogs.TunnelAuth,
|
|
|
|
tunnelID uuid.UUID,
|
|
|
|
options *pogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
|
|
|
edgeAddress net.IP,
|
|
|
|
) (*pogs.ConnectionDetails, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, r.requestTimeout)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Registration, metrics.OperationRegisterConnection).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Registration, metrics.OperationRegisterConnection)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
conn, err := r.client.RegisterConnection(ctx, auth, tunnelID, connIndex, options)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Registration, metrics.OperationRegisterConnection).Inc()
|
|
|
|
}
|
|
|
|
return conn, err
|
2024-05-24 18:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *registrationClient) SendLocalConfiguration(ctx context.Context, config []byte) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, r.requestTimeout)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Registration, metrics.OperationUpdateLocalConfiguration).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Registration, metrics.OperationUpdateLocalConfiguration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := r.client.SendLocalConfiguration(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Registration, metrics.OperationUpdateLocalConfiguration).Inc()
|
|
|
|
}
|
|
|
|
return err
|
2024-05-24 18:40:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 15:47:36 +00:00
|
|
|
func (r *registrationClient) GracefulShutdown(ctx context.Context, gracePeriod time.Duration) {
|
2024-05-24 18:40:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, gracePeriod)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Registration, metrics.OperationUnregisterConnection).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Registration, metrics.OperationUnregisterConnection)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
err := r.client.UnregisterConnection(ctx)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Registration, metrics.OperationUnregisterConnection).Inc()
|
|
|
|
}
|
2024-05-24 18:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *registrationClient) Close() {
|
|
|
|
// Closing the client will also close the connection
|
|
|
|
_ = r.client.Close()
|
|
|
|
// Closing the transport also closes the stream
|
|
|
|
_ = r.transport.Close()
|
|
|
|
}
|