From d7498b0c03e8fea28429d0bcc25eee3e1d7b96ca Mon Sep 17 00:00:00 2001 From: cthuang Date: Thu, 8 Oct 2020 10:48:10 +0100 Subject: [PATCH] TUN-3449: Use flag to select transport protocol implementation --- cmd/cloudflared/tunnel/cmd.go | 1 + cmd/cloudflared/tunnel/subcommand_context.go | 6 +++- cmd/cloudflared/tunnel/subcommands.go | 9 ++++++ origin/tunnel.go | 29 ++++++++++++++++++-- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 940f4473..5b74a236 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -719,6 +719,7 @@ func tunnelFlags(shouldHide bool) []cli.Flag { Value: false, Hidden: shouldHide, }), + selectProtocolFlag, }...) return flags diff --git a/cmd/cloudflared/tunnel/subcommand_context.go b/cmd/cloudflared/tunnel/subcommand_context.go index 5774bced..3183786b 100644 --- a/cmd/cloudflared/tunnel/subcommand_context.go +++ b/cmd/cloudflared/tunnel/subcommand_context.go @@ -260,12 +260,16 @@ func (sc *subcommandContext) run(tunnelID uuid.UUID) error { return err } + protocol, ok := origin.ParseProtocol(sc.c.String("protocol")) + if !ok { + return fmt.Errorf("%s is not valid protocol. %s", sc.c.String("protocol"), availableProtocol) + } return StartServer( sc.c, version, shutdownC, graceShutdownC, - &origin.NamedTunnelConfig{Auth: *credentials, ID: tunnelID}, + &origin.NamedTunnelConfig{Auth: *credentials, ID: tunnelID, Protocol: protocol}, sc.logger, sc.isUIEnabled, ) diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 74846e0b..a5b84e96 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -30,6 +30,7 @@ import ( const ( credFileFlagAlias = "cred-file" + availableProtocol = "Available protocols: http2, Go's implementation and h2mux, Cloudflare's implementation of HTTP/2." ) var ( @@ -83,6 +84,13 @@ var ( Aliases: []string{"f"}, Usage: "Allows you to delete a tunnel, even if it has active connections.", } + selectProtocolFlag = &cli.StringFlag{ + Name: "protocol", + Value: "h2mux", + Aliases: []string{"p"}, + Usage: fmt.Sprintf("Protocol implementation to connect with Cloudflare's edge network. %s", availableProtocol), + Hidden: true, + } ) func buildCreateCommand() *cli.Command { @@ -309,6 +317,7 @@ func buildRunCommand() *cli.Command { flags := []cli.Flag{ forceFlag, credentialsFileFlag, + selectProtocolFlag, } flags = append(flags, configureProxyFlags(false)...) return &cli.Command{ diff --git a/origin/tunnel.go b/origin/tunnel.go index 564e17e0..f15cd3b0 100644 --- a/origin/tunnel.go +++ b/origin/tunnel.go @@ -188,9 +188,28 @@ func (c *TunnelConfig) IsTrialTunnel() bool { } type NamedTunnelConfig struct { - Auth pogs.TunnelAuth - ID uuid.UUID - Client pogs.ClientInfo + Auth pogs.TunnelAuth + ID uuid.UUID + Client pogs.ClientInfo + Protocol Protocol +} + +type Protocol int64 + +const ( + h2muxProtocol Protocol = iota + http2Protocol +) + +func ParseProtocol(s string) (Protocol, bool) { + switch s { + case "h2mux": + return h2muxProtocol, true + case "http2": + return http2Protocol, true + default: + return 0, false + } } func StartTunnelDaemon(ctx context.Context, config *TunnelConfig, connectedSignal *signal.Signal, cloudflaredID uuid.UUID, reconnectCh chan ReconnectSignal) error { @@ -284,6 +303,10 @@ func ServeTunnel( connectionTag := uint8ToString(connectionIndex) + if config.NamedTunnel != nil && config.NamedTunnel.Protocol == http2Protocol { + return ServeNamedTunnel(ctx, config, connectionIndex, addr, connectedFuse, reconnectCh) + } + // Returns error from parsing the origin URL or handshake errors handler, originLocalAddr, err := NewTunnelHandler(ctx, config, addr, connectionIndex, bufferPool) if err != nil {