Skip to content

Commit

Permalink
Add support for setting interface name and wireguard port (#1467)
Browse files Browse the repository at this point in the history
This PR adds support for setting the
wireguard interface name and port
with the netbird up command
  • Loading branch information
mlsmaycon committed Jan 15, 2024
1 parent ace5887 commit e9c967b
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 134 deletions.
6 changes: 5 additions & 1 deletion client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const (
externalIPMapFlag = "external-ip-map"
dnsResolverAddress = "dns-resolver-address"
enableRosenpassFlag = "enable-rosenpass"
preSharedKeyFlag = "preshared-key"
preSharedKeyFlag = "preshared-key"
interfaceNameFlag = "interface-name"
wireguardPortFlag = "wireguard-port"
)

var (
Expand All @@ -52,6 +54,8 @@ var (
natExternalIPs []string
customDNSAddress string
rosenpassEnabled bool
interfaceName string
wireguardPort uint16
rootCmd = &cobra.Command{
Use: "netbird",
Short: "",
Expand Down
42 changes: 41 additions & 1 deletion 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 All @@ -16,6 +17,7 @@ import (
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/proto"
"github.com/netbirdio/netbird/client/system"
"github.com/netbirdio/netbird/iface"
"github.com/netbirdio/netbird/util"
)

Expand All @@ -36,6 +38,8 @@ 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().Uint16Var(&wireguardPort, wireguardPortFlag, iface.DefaultWgPort, "Wireguard interface listening port")
}

func upFunc(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -86,10 +90,22 @@ func runInForegroundMode(ctx context.Context, cmd *cobra.Command) error {
CustomDNSAddress: customDNSAddressConverted,
}

if rootCmd.PersistentFlags().Changed(enableRosenpassFlag) {
if cmd.Flag(enableRosenpassFlag).Changed {
ic.RosenpassEnabled = &rosenpassEnabled
}

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

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

if rootCmd.PersistentFlags().Changed(preSharedKeyFlag) {
ic.PreSharedKey = &preSharedKey
}
Expand Down Expand Up @@ -161,6 +177,18 @@ func runInDaemonMode(ctx context.Context, cmd *cobra.Command) error {
loginRequest.RosenpassEnabled = &rosenpassEnabled
}

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

if cmd.Flag(wireguardPortFlag).Changed {
wp := int64(wireguardPort)
loginRequest.WireguardPort = &wp
}

var loginErr error

var loginResp *proto.LoginResponse
Expand Down Expand Up @@ -232,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
28 changes: 25 additions & 3 deletions client/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type ConfigInput struct {
NATExternalIPs []string
CustomDNSAddress []byte
RosenpassEnabled *bool
InterfaceName *string
WireguardPort *int
}

// Config Configuration type
Expand All @@ -59,7 +61,7 @@ type Config struct {
// SSHKey is a private SSH key in a PEM format
SSHKey string

// ExternalIP mappings, if different than the host interface IP
// ExternalIP mappings, if different from the host interface IP
//
// External IP must not be behind a CGNAT and port-forwarding for incoming UDP packets from WgPort on ExternalIP
// to WgPort on host interface IP must be present. This can take form of single port-forwarding rule, 1:1 DNAT
Expand Down Expand Up @@ -142,11 +144,10 @@ func createNewConfig(input ConfigInput) (*Config, error) {
if err != nil {
return nil, err
}

config := &Config{
SSHKey: string(pem),
PrivateKey: wgKey,
WgIface: iface.WgInterfaceDefault,
WgPort: iface.DefaultWgPort,
IFaceBlackList: []string{},
DisableIPv6Discovery: false,
NATExternalIPs: input.NATExternalIPs,
Expand All @@ -167,6 +168,16 @@ func createNewConfig(input ConfigInput) (*Config, error) {
config.ManagementURL = URL
}

config.WgPort = iface.DefaultWgPort
if input.WireguardPort != nil {
config.WgPort = *input.WireguardPort
}

config.WgIface = iface.WgInterfaceDefault
if input.InterfaceName != nil {
config.WgIface = *input.InterfaceName
}

if input.PreSharedKey != nil {
config.PreSharedKey = *input.PreSharedKey
}
Expand Down Expand Up @@ -243,6 +254,17 @@ func update(input ConfigInput) (*Config, error) {
config.WgPort = iface.DefaultWgPort
refresh = true
}

if input.WireguardPort != nil {
config.WgPort = *input.WireguardPort
refresh = true
}

if input.InterfaceName != nil {
config.WgIface = *input.InterfaceName
refresh = true
}

if input.NATExternalIPs != nil && len(config.NATExternalIPs) != len(input.NATExternalIPs) {
config.NATExternalIPs = input.NATExternalIPs
refresh = true
Expand Down

0 comments on commit e9c967b

Please sign in to comment.