2019-11-21 17:03:13 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-28 09:10:30 +00:00
|
|
|
"io"
|
2022-06-02 17:57:37 +00:00
|
|
|
"net"
|
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,
|
2022-06-18 00:24:37 +00:00
|
|
|
edgeAddress net.IP,
|
2020-10-27 22:27:15 +00:00
|
|
|
observer *Observer,
|
2022-04-27 10:51:06 +00:00
|
|
|
) (*tunnelpogs.ConnectionDetails, error)
|
|
|
|
SendLocalConfiguration(
|
|
|
|
c context.Context,
|
|
|
|
config []byte,
|
|
|
|
observer *Observer,
|
2020-10-27 22:27:15 +00:00
|
|
|
) 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,
|
2022-06-18 00:24:37 +00:00
|
|
|
edgeAddress net.IP,
|
2020-10-08 10:12:26 +00:00
|
|
|
observer *Observer,
|
2022-04-27 10:51:06 +00:00
|
|
|
) (*tunnelpogs.ConnectionDetails, 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()
|
2022-04-27 10:51:06 +00:00
|
|
|
return nil, errDuplicationConnection
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
observer.metrics.regFail.WithLabelValues("server_error", "registerConnection").Inc()
|
2022-04-27 10:51:06 +00:00
|
|
|
return nil, serverRegistrationErrorFromRPC(err)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
observer.metrics.regSuccess.WithLabelValues("registerConnection").Inc()
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rsc *registrationServerClient) SendLocalConfiguration(ctx context.Context, config []byte, observer *Observer) (err error) {
|
|
|
|
observer.metrics.localConfigMetrics.pushes.Inc()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
observer.metrics.localConfigMetrics.pushesErrors.Inc()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rsc.client.SendLocalConfiguration(ctx, config)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
)
|