2020-06-02 18:19:19 +00:00
|
|
|
package pogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-09-28 09:10:30 +00:00
|
|
|
capnp "zombiezen.com/go/capnproto2"
|
2020-06-02 18:19:19 +00:00
|
|
|
"zombiezen.com/go/capnproto2/pogs"
|
2020-09-28 09:10:30 +00:00
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
2020-06-02 18:19:19 +00:00
|
|
|
"zombiezen.com/go/capnproto2/server"
|
|
|
|
|
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/proto"
|
2020-06-02 18:19:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RegistrationServer interface {
|
2024-05-28 21:14:25 +00:00
|
|
|
// RegisterConnection is the call typically handled by the edge to initiate and authenticate a new connection
|
|
|
|
// for cloudflared.
|
2020-06-12 02:47:40 +00:00
|
|
|
RegisterConnection(ctx context.Context, auth TunnelAuth, tunnelID uuid.UUID, connIndex byte, options *ConnectionOptions) (*ConnectionDetails, error)
|
2024-05-28 21:14:25 +00:00
|
|
|
// UnregisterConnection is the call typically handled by the edge to close an existing connection for cloudflared.
|
2020-06-02 18:19:19 +00:00
|
|
|
UnregisterConnection(ctx context.Context)
|
2024-05-28 21:14:25 +00:00
|
|
|
// UpdateLocalConfiguration is the call typically handled by the edge for cloudflared to provide the current
|
|
|
|
// configuration it is operating with.
|
2022-04-20 16:52:55 +00:00
|
|
|
UpdateLocalConfiguration(ctx context.Context, config []byte) error
|
2020-06-02 18:19:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 09:10:30 +00:00
|
|
|
type RegistrationServer_PogsImpl struct {
|
|
|
|
impl RegistrationServer
|
2020-06-02 18:19:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func RegistrationServer_ServerToClient(s RegistrationServer) proto.RegistrationServer {
|
|
|
|
return proto.RegistrationServer_ServerToClient(RegistrationServer_PogsImpl{s})
|
2020-06-02 18:19:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (i RegistrationServer_PogsImpl) RegisterConnection(p proto.RegistrationServer_registerConnection) error {
|
2024-05-28 21:14:25 +00:00
|
|
|
return metrics.ObserveServerHandler(func() error { return i.registerConnection(p) }, metrics.Registration, metrics.OperationRegisterConnection)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i RegistrationServer_PogsImpl) registerConnection(p proto.RegistrationServer_registerConnection) error {
|
2020-06-02 18:19:19 +00:00
|
|
|
server.Ack(p.Options)
|
|
|
|
|
|
|
|
auth, err := p.Params.Auth()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-12 02:47:40 +00:00
|
|
|
var pogsAuth TunnelAuth
|
|
|
|
err = pogsAuth.UnmarshalCapnproto(auth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-02 18:19:19 +00:00
|
|
|
uuidBytes, err := p.Params.TunnelId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tunnelID, err := uuid.FromBytes(uuidBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
connIndex := p.Params.ConnIndex()
|
|
|
|
options, err := p.Params.Options()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pogsOptions ConnectionOptions
|
|
|
|
err = pogsOptions.UnmarshalCapnproto(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:47:40 +00:00
|
|
|
connDetails, callError := i.impl.RegisterConnection(p.Ctx, pogsAuth, tunnelID, connIndex, &pogsOptions)
|
2020-06-02 18:19:19 +00:00
|
|
|
|
|
|
|
resp, err := p.Results.NewResult()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if callError != nil {
|
|
|
|
if connError, err := resp.Result().NewError(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return MarshalError(connError, callError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if details, err := resp.Result().NewConnectionDetails(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return connDetails.MarshalCapnproto(details)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (i RegistrationServer_PogsImpl) UnregisterConnection(p proto.RegistrationServer_unregisterConnection) error {
|
2024-05-28 21:14:25 +00:00
|
|
|
return metrics.ObserveServerHandler(func() error {
|
|
|
|
server.Ack(p.Options)
|
|
|
|
i.impl.UnregisterConnection(p.Ctx)
|
|
|
|
return nil // No metrics will be reported for failure as this method has no return value
|
|
|
|
}, metrics.Registration, metrics.OperationUnregisterConnection)
|
|
|
|
}
|
2020-06-02 18:19:19 +00:00
|
|
|
|
2024-05-28 21:14:25 +00:00
|
|
|
func (i RegistrationServer_PogsImpl) UpdateLocalConfiguration(p proto.RegistrationServer_updateLocalConfiguration) error {
|
|
|
|
return metrics.ObserveServerHandler(func() error { return i.updateLocalConfiguration(p) }, metrics.Registration, metrics.OperationUpdateLocalConfiguration)
|
2020-06-02 18:19:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:14:25 +00:00
|
|
|
func (i RegistrationServer_PogsImpl) updateLocalConfiguration(c proto.RegistrationServer_updateLocalConfiguration) error {
|
2022-04-20 16:52:55 +00:00
|
|
|
server.Ack(c.Options)
|
|
|
|
|
|
|
|
configBytes, err := c.Params.Config()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.impl.UpdateLocalConfiguration(c.Ctx, configBytes)
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:10:30 +00:00
|
|
|
type RegistrationServer_PogsClient struct {
|
|
|
|
Client capnp.Client
|
|
|
|
Conn *rpc.Conn
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:40:10 +00:00
|
|
|
func NewRegistrationServer_PogsClient(client capnp.Client, conn *rpc.Conn) RegistrationServer_PogsClient {
|
|
|
|
return RegistrationServer_PogsClient{
|
|
|
|
Client: client,
|
|
|
|
Conn: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func (c RegistrationServer_PogsClient) Close() error {
|
|
|
|
c.Client.Close()
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:10:30 +00:00
|
|
|
func (c RegistrationServer_PogsClient) RegisterConnection(ctx context.Context, auth TunnelAuth, tunnelID uuid.UUID, connIndex byte, options *ConnectionOptions) (*ConnectionDetails, error) {
|
2024-05-14 04:22:06 +00:00
|
|
|
client := proto.TunnelServer{Client: c.Client}
|
|
|
|
promise := client.RegisterConnection(ctx, func(p proto.RegistrationServer_registerConnection_Params) error {
|
2020-06-12 02:47:40 +00:00
|
|
|
tunnelAuth, err := p.NewAuth()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = auth.MarshalCapnproto(tunnelAuth); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.SetAuth(tunnelAuth)
|
2020-06-02 18:19:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.SetTunnelId(tunnelID[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.SetConnIndex(connIndex)
|
|
|
|
connectionOptions, err := p.NewOptions()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = options.MarshalCapnproto(connectionOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
response, err := promise.Result().Struct()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
result := response.Result()
|
|
|
|
switch result.Which() {
|
2024-05-14 04:22:06 +00:00
|
|
|
case proto.ConnectionResponse_result_Which_error:
|
2020-06-02 18:19:19 +00:00
|
|
|
resultError, err := result.Error()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
cause, err := resultError.Cause()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
err = errors.New(cause)
|
|
|
|
if resultError.ShouldRetry() {
|
|
|
|
err = RetryErrorAfter(err, time.Duration(resultError.RetryAfter()))
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
case proto.ConnectionResponse_result_Which_connectionDetails:
|
2020-06-02 18:19:19 +00:00
|
|
|
connDetails, err := result.ConnectionDetails()
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
details := new(ConnectionDetails)
|
|
|
|
if err = details.UnmarshalCapnproto(connDetails); err != nil {
|
|
|
|
return nil, wrapRPCError(err)
|
|
|
|
}
|
|
|
|
return details, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, newRPCError("unknown result which %d", result.Which())
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
func (c RegistrationServer_PogsClient) SendLocalConfiguration(ctx context.Context, config []byte) error {
|
2024-05-14 04:22:06 +00:00
|
|
|
client := proto.TunnelServer{Client: c.Client}
|
|
|
|
promise := client.UpdateLocalConfiguration(ctx, func(p proto.RegistrationServer_updateLocalConfiguration_Params) error {
|
2022-04-27 10:51:06 +00:00
|
|
|
if err := p.SetConfig(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := promise.Struct()
|
|
|
|
if err != nil {
|
|
|
|
return wrapRPCError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:10:30 +00:00
|
|
|
func (c RegistrationServer_PogsClient) UnregisterConnection(ctx context.Context) error {
|
2024-05-14 04:22:06 +00:00
|
|
|
client := proto.TunnelServer{Client: c.Client}
|
|
|
|
promise := client.UnregisterConnection(ctx, func(p proto.RegistrationServer_unregisterConnection_Params) error {
|
2020-06-02 18:19:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_, err := promise.Struct()
|
2020-06-18 13:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return wrapRPCError(err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-02 18:19:19 +00:00
|
|
|
}
|
2020-09-28 09:10:30 +00:00
|
|
|
|
|
|
|
type ClientInfo struct {
|
|
|
|
ClientID []byte `capnp:"clientId"` // must be a slice for capnp compatibility
|
|
|
|
Features []string
|
|
|
|
Version string
|
|
|
|
Arch string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionOptions struct {
|
|
|
|
Client ClientInfo
|
|
|
|
OriginLocalIP net.IP `capnp:"originLocalIp"`
|
|
|
|
ReplaceExisting bool
|
|
|
|
CompressionQuality uint8
|
|
|
|
NumPreviousAttempts uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
type TunnelAuth struct {
|
|
|
|
AccountTag string
|
|
|
|
TunnelSecret []byte
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (p *ConnectionOptions) MarshalCapnproto(s proto.ConnectionOptions) error {
|
|
|
|
return pogs.Insert(proto.ConnectionOptions_TypeID, s.Struct, p)
|
2020-09-28 09:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (p *ConnectionOptions) UnmarshalCapnproto(s proto.ConnectionOptions) error {
|
|
|
|
return pogs.Extract(p, proto.ConnectionOptions_TypeID, s.Struct)
|
2020-09-28 09:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (a *TunnelAuth) MarshalCapnproto(s proto.TunnelAuth) error {
|
|
|
|
return pogs.Insert(proto.TunnelAuth_TypeID, s.Struct, a)
|
2020-09-28 09:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (a *TunnelAuth) UnmarshalCapnproto(s proto.TunnelAuth) error {
|
|
|
|
return pogs.Extract(a, proto.TunnelAuth_TypeID, s.Struct)
|
2020-09-28 09:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionDetails struct {
|
2022-04-27 13:34:11 +00:00
|
|
|
UUID uuid.UUID
|
|
|
|
Location string
|
|
|
|
TunnelIsRemotelyManaged bool
|
2020-09-28 09:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (details *ConnectionDetails) MarshalCapnproto(s proto.ConnectionDetails) error {
|
2020-09-28 09:10:30 +00:00
|
|
|
if err := s.SetUuid(details.UUID[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.SetLocationName(details.Location); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-27 13:34:11 +00:00
|
|
|
s.SetTunnelIsRemotelyManaged(details.TunnelIsRemotelyManaged)
|
2020-09-28 09:10:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func (details *ConnectionDetails) UnmarshalCapnproto(s proto.ConnectionDetails) error {
|
2020-09-28 09:10:30 +00:00
|
|
|
uuidBytes, err := s.Uuid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
details.UUID, err = uuid.FromBytes(uuidBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
details.Location, err = s.LocationName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-27 13:34:11 +00:00
|
|
|
details.TunnelIsRemotelyManaged = s.TunnelIsRemotelyManaged()
|
2020-09-28 09:10:30 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:22:06 +00:00
|
|
|
func MarshalError(s proto.ConnectionError, err error) error {
|
2020-09-28 09:10:30 +00:00
|
|
|
if err := s.SetCause(err.Error()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if retryableErr, ok := err.(*RetryableError); ok {
|
|
|
|
s.SetShouldRetry(true)
|
|
|
|
s.SetRetryAfter(int64(retryableErr.Delay))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|