Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion client/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"
"os"
"os/signal"
"regexp"
Expand All @@ -18,6 +19,7 @@ import (

"github.com/netbirdio/netbird/client/internal/expose"
"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/shared/exposetarget"
"github.com/netbirdio/netbird/util"
)

Expand All @@ -31,6 +33,7 @@ var (
exposeNamePrefix string
exposeProtocol string
exposeExternalPort uint16
exposeTargetHost string
)

var exposeCmd = &cobra.Command{
Expand All @@ -40,7 +43,8 @@ var exposeCmd = &cobra.Command{
Example: ` netbird expose --with-password safe-pass 8080
netbird expose --protocol tcp 5432
netbird expose --protocol tcp --with-external-port 5433 5432
netbird expose --protocol tls --with-custom-domain tls.example.com 4443`,
netbird expose --protocol tls --with-custom-domain tls.example.com 4443
netbird expose 8080 --with-target-host other-service`,
RunE: exposeFn,
}

Expand All @@ -52,6 +56,7 @@ func init() {
exposeCmd.Flags().StringVar(&exposeNamePrefix, "with-name-prefix", "", "Prefix for the generated service name (e.g. --with-name-prefix my-app)")
exposeCmd.Flags().StringVar(&exposeProtocol, "protocol", "http", "Protocol to use: http, https, tcp, udp, or tls (e.g. --protocol tcp)")
exposeCmd.Flags().Uint16Var(&exposeExternalPort, "with-external-port", 0, "Public-facing external port on the proxy cluster (defaults to the target port for L4)")
exposeCmd.Flags().StringVar(&exposeTargetHost, "with-target-host", "", "Expose a host this peer can reach instead of a local port (e.g. --with-target-host other-service)")
}

// isClusterProtocol returns true for L4/TLS protocols that reject HTTP-style auth flags.
Expand Down Expand Up @@ -131,6 +136,14 @@ func validateExposeFlags(cmd *cobra.Command, portStr string) (uint64, error) {
return 0, fmt.Errorf("user groups cannot be empty")
}

if cmd.Flags().Changed("with-target-host") || exposeTargetHost != "" {
host, err := exposetarget.NormalizeHost(exposeTargetHost)
if err != nil {
return 0, err
}
exposeTargetHost = host
}

return port, nil
}

Expand Down Expand Up @@ -192,6 +205,7 @@ func exposeFn(cmd *cobra.Command, args []string) error {
UserGroups: exposeUserGroups,
Domain: exposeDomain,
NamePrefix: exposeNamePrefix,
TargetHost: exposeTargetHost,
}
if isClusterProtocol(exposeProtocol) {
req.ListenPort = uint32(resolveExternalPort(port))
Expand Down Expand Up @@ -241,10 +255,27 @@ func handleExposeReady(cmd *cobra.Command, stream proto.DaemonService_ExposeServ
if !ok {
return fmt.Errorf("unexpected expose event: %T", event.Event)
}
if err := confirmAppliedTargetHost(exposeTargetHost, ready.Ready); err != nil {
return err
}
printExposeReady(cmd, ready.Ready, port)
return nil
}

func confirmAppliedTargetHost(requested string, ready *proto.ExposeServiceReady) error {
if requested == "" {
return nil
}
applied := ready.GetTargetHost()
if applied == "" {
return fmt.Errorf("target host %q was not applied; upgrade the daemon and management server", requested)
}
if applied != requested {
return fmt.Errorf("applied target host %q does not match requested %q", applied, requested)
}
return nil
}

func printExposeReady(cmd *cobra.Command, r *proto.ExposeServiceReady, port uint64) {
cmd.Println("Service exposed successfully!")
cmd.Printf(" Name: %s\n", r.ServiceName)
Expand All @@ -256,6 +287,9 @@ func printExposeReady(cmd *cobra.Command, r *proto.ExposeServiceReady, port uint
}
cmd.Printf(" Protocol: %s\n", exposeProtocol)
cmd.Printf(" Internal: %d\n", port)
if r.GetTargetHost() != "" {
cmd.Printf(" Target: -> %s\n", net.JoinHostPort(r.GetTargetHost(), strconv.FormatUint(port, 10)))
}
if isClusterProtocol(exposeProtocol) {
cmd.Printf(" External: %s\n", extractPort(r.ServiceUrl, resolveExternalPort(port)))
}
Expand Down
Loading
Loading