Skip to content

Commit

Permalink
use uint16 for port flag
Browse files Browse the repository at this point in the history
check interface name on macOS
  • Loading branch information
mlsmaycon committed Jan 15, 2024
1 parent e4684ee commit a2db272
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
customDNSAddress string
rosenpassEnabled bool
interfaceName string
wireguardPort int
wireguardPort uint16
rootCmd = &cobra.Command{
Use: "netbird",
Short: "",
Expand Down
24 changes: 22 additions & 2 deletions client/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/netip"
"runtime"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,7 +39,7 @@ var (
func init() {
upCmd.PersistentFlags().BoolVarP(&foregroundMode, "foreground-mode", "F", false, "start service in foreground")
upCmd.PersistentFlags().StringVar(&interfaceName, interfaceNameFlag, iface.WgInterfaceDefault, "Wireguard interface name")
upCmd.PersistentFlags().IntVar(&wireguardPort, wireguardPortFlag, iface.DefaultWgPort, "Wireguard interface listening port")
upCmd.PersistentFlags().Uint16Var(&wireguardPort, wireguardPortFlag, iface.DefaultWgPort, "Wireguard interface listening port")
}

func upFunc(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -94,11 +95,15 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command) error {
}

if cmd.Flag(interfaceNameFlag).Changed {
if err := parseInterfaceName(interfaceName); err != nil {
return err
}
ic.InterfaceName = &interfaceName
}

if cmd.Flag(wireguardPortFlag).Changed {
ic.WireguardPort = &wireguardPort
p := int(wireguardPort)
ic.WireguardPort = &p
}

if rootCmd.PersistentFlags().Changed(preSharedKeyFlag) {
Expand Down Expand Up @@ -173,6 +178,9 @@ func runInDaemonMode(ctx context.Context, cmd *cobra.Command) error {
}

if cmd.Flag(interfaceNameFlag).Changed {
if err := parseInterfaceName(interfaceName); err != nil {
return err
}
loginRequest.InterfaceName = &interfaceName
}

Expand Down Expand Up @@ -252,6 +260,18 @@ func validateNATExternalIPs(list []string) error {
return nil
}

func parseInterfaceName(name string) error {
if runtime.GOOS != "darwin" {
return nil
}

if strings.HasPrefix(name, "utun") {
return nil
}

return fmt.Errorf("invalid interface name %s. Please use the prefix utun followed by a number on MacOS. e.g., utun1 or utun199", name)
}

func validateElement(element string) (int, error) {
if isValidIP(element) {
return ipInputType, nil
Expand Down

0 comments on commit a2db272

Please sign in to comment.