Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting interface name and wireguard port #1467

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 int
mlsmaycon marked this conversation as resolved.
Show resolved Hide resolved
rootCmd = &cobra.Command{
Use: "netbird",
Short: "",
Expand Down
22 changes: 21 additions & 1 deletion client/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 +37,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().IntVar(&wireguardPort, wireguardPortFlag, iface.DefaultWgPort, "Wireguard interface listening port")
}

func upFunc(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -86,10 +89,18 @@ 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 {
ic.InterfaceName = &interfaceName
}

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

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

if cmd.Flag(interfaceNameFlag).Changed {
loginRequest.InterfaceName = &interfaceName
}

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

var loginErr error

var loginResp *proto.LoginResponse
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
Loading
Loading