2024-05-14 04:22:06 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
2024-05-15 20:06:58 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc"
|
2024-05-28 21:14:25 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/metrics"
|
2024-05-14 04:22:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CloudflaredClient calls capnp rpc methods of SessionManager and ConfigurationManager.
|
|
|
|
type CloudflaredClient struct {
|
|
|
|
client pogs.CloudflaredServer_PogsClient
|
|
|
|
transport rpc.Transport
|
|
|
|
requestTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCloudflaredClient(ctx context.Context, stream io.ReadWriteCloser, requestTimeout time.Duration) (*CloudflaredClient, error) {
|
|
|
|
n, err := stream.Write(rpcStreamProtocolSignature[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != len(rpcStreamProtocolSignature) {
|
|
|
|
return nil, fmt.Errorf("expect to write %d bytes for RPC stream protocol signature, wrote %d", len(rpcStreamProtocolSignature), n)
|
|
|
|
}
|
2024-05-15 20:06:58 +00:00
|
|
|
transport := tunnelrpc.SafeTransport(stream)
|
2024-06-26 20:34:49 +00:00
|
|
|
conn := tunnelrpc.NewClientConn(transport)
|
2024-05-14 04:22:06 +00:00
|
|
|
client := pogs.NewCloudflaredServer_PogsClient(conn.Bootstrap(ctx), conn)
|
|
|
|
return &CloudflaredClient{
|
|
|
|
client: client,
|
|
|
|
transport: transport,
|
|
|
|
requestTimeout: requestTimeout,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudflaredClient) RegisterUdpSession(ctx context.Context, sessionID uuid.UUID, dstIP net.IP, dstPort uint16, closeIdleAfterHint time.Duration, traceContext string) (*pogs.RegisterUdpSessionResponse, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, c.requestTimeout)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Cloudflared, metrics.OperationRegisterUdpSession).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Cloudflared, metrics.OperationRegisterUdpSession)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
resp, err := c.client.RegisterUdpSession(ctx, sessionID, dstIP, dstPort, closeIdleAfterHint, traceContext)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Cloudflared, metrics.OperationRegisterUdpSession).Inc()
|
|
|
|
}
|
|
|
|
return resp, err
|
2024-05-14 04:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudflaredClient) UnregisterUdpSession(ctx context.Context, sessionID uuid.UUID, message string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, c.requestTimeout)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Cloudflared, metrics.OperationUnregisterUdpSession).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Cloudflared, metrics.OperationUnregisterUdpSession)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := c.client.UnregisterUdpSession(ctx, sessionID, message)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Cloudflared, metrics.OperationUnregisterUdpSession).Inc()
|
|
|
|
}
|
|
|
|
return err
|
2024-05-14 04:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudflaredClient) UpdateConfiguration(ctx context.Context, version int32, config []byte) (*pogs.UpdateConfigurationResponse, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, c.requestTimeout)
|
|
|
|
defer cancel()
|
2024-05-28 21:14:25 +00:00
|
|
|
defer metrics.CapnpMetrics.ClientOperations.WithLabelValues(metrics.Cloudflared, metrics.OperationUpdateConfiguration).Inc()
|
|
|
|
timer := metrics.NewClientOperationLatencyObserver(metrics.Cloudflared, metrics.OperationUpdateConfiguration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
resp, err := c.client.UpdateConfiguration(ctx, version, config)
|
|
|
|
if err != nil {
|
|
|
|
metrics.CapnpMetrics.ClientFailures.WithLabelValues(metrics.Cloudflared, metrics.OperationUpdateConfiguration).Inc()
|
|
|
|
}
|
|
|
|
return resp, err
|
2024-05-14 04:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CloudflaredClient) Close() {
|
|
|
|
_ = c.client.Close()
|
|
|
|
_ = c.transport.Close()
|
|
|
|
}
|