From c2e90a2a97058b82c6543b8c2eb4fb1482390716 Mon Sep 17 00:00:00 2001 From: Chinmay Pai Date: Thu, 20 Apr 2023 19:30:22 +0530 Subject: [PATCH] feat: add support for custom device hostname (#789) Configure via --hostname (or -n) flag in the `up` and `login` commands --------- Signed-off-by: Chinmay D. Pai --- client/cmd/login.go | 6 ++++++ client/cmd/root.go | 2 ++ client/cmd/up.go | 6 ++++++ client/system/info.go | 9 +++++++++ client/system/info_android.go | 10 +--------- client/system/info_darwin.go | 3 ++- client/system/info_freebsd.go | 3 ++- client/system/info_linux.go | 3 ++- client/system/info_test.go | 9 +++++++++ client/system/info_windows.go | 3 ++- 10 files changed, 41 insertions(+), 13 deletions(-) diff --git a/client/cmd/login.go b/client/cmd/login.go index 92d69b6ee..72ff1e81c 100644 --- a/client/cmd/login.go +++ b/client/cmd/login.go @@ -15,6 +15,7 @@ import ( "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/proto" + "github.com/netbirdio/netbird/client/system" ) var loginCmd = &cobra.Command{ @@ -32,6 +33,11 @@ var loginCmd = &cobra.Command{ ctx := internal.CtxInitState(context.Background()) + if hostName != "" { + // nolint + ctx = context.WithValue(ctx, system.DeviceNameCtxKey, hostName) + } + // workaround to run without service if logFile == "console" { err = handleRebrand(cmd) diff --git a/client/cmd/root.go b/client/cmd/root.go index 0b2739bbc..770bd21fa 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -45,6 +45,7 @@ var ( managementURL string adminURL string setupKey string + hostName string preSharedKey string natExternalIPs []string customDNSAddress string @@ -94,6 +95,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the the log will be output to stdout") rootCmd.PersistentFlags().StringVarP(&setupKey, "setup-key", "k", "", "Setup key obtained from the Management Service Dashboard (used to register peer)") rootCmd.PersistentFlags().StringVar(&preSharedKey, "preshared-key", "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.") + rootCmd.PersistentFlags().StringVarP(&hostName, "hostname", "n", "", "Sets a custom hostname for the device") rootCmd.AddCommand(serviceCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(downCmd) diff --git a/client/cmd/up.go b/client/cmd/up.go index fc576e8d4..7adbe5054 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -15,6 +15,7 @@ import ( "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/internal/peer" "github.com/netbirdio/netbird/client/proto" + "github.com/netbirdio/netbird/client/system" "github.com/netbirdio/netbird/util" ) @@ -55,6 +56,11 @@ func upFunc(cmd *cobra.Command, args []string) error { ctx := internal.CtxInitState(cmd.Context()) + if hostName != "" { + // nolint + ctx = context.WithValue(ctx, system.DeviceNameCtxKey, hostName) + } + if foregroundMode { return runInForegroundMode(ctx, cmd) } diff --git a/client/system/info.go b/client/system/info.go index 15b26d0e2..a495ed1e9 100644 --- a/client/system/info.go +++ b/client/system/info.go @@ -43,6 +43,15 @@ func extractUserAgent(ctx context.Context) string { return "" } +// extractDeviceName extracts device name from context or returns the default system name +func extractDeviceName(ctx context.Context, defaultName string) string { + v, ok := ctx.Value(DeviceNameCtxKey).(string) + if !ok { + return defaultName + } + return v +} + // GetDesktopUIUserAgent returns the Desktop ui user agent func GetDesktopUIUserAgent() string { return "netbird-desktop-ui/" + version.NetbirdVersion() diff --git a/client/system/info_android.go b/client/system/info_android.go index 9ea9c0487..9a1a7befb 100644 --- a/client/system/info_android.go +++ b/client/system/info_android.go @@ -24,21 +24,13 @@ func GetInfo(ctx context.Context) *Info { } gio := &Info{Kernel: kernel, Core: osVersion(), Platform: "unknown", OS: "android", OSVersion: osVersion(), GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} - gio.Hostname = extractDeviceName(ctx) + gio.Hostname = extractDeviceName(ctx, "android") gio.WiretrusteeVersion = version.NetbirdVersion() gio.UIVersion = extractUserAgent(ctx) return gio } -func extractDeviceName(ctx context.Context) string { - v, ok := ctx.Value(DeviceNameCtxKey).(string) - if !ok { - return "android" - } - return v -} - func uname() []string { res := run("/system/bin/uname", "-a") return strings.Split(res, " ") diff --git a/client/system/info_darwin.go b/client/system/info_darwin.go index 82e3d94ca..e153539bb 100644 --- a/client/system/info_darwin.go +++ b/client/system/info_darwin.go @@ -32,7 +32,8 @@ func GetInfo(ctx context.Context) *Info { swVersion = []byte(release) } gio := &Info{Kernel: sysName, OSVersion: strings.TrimSpace(string(swVersion)), Core: release, Platform: machine, OS: sysName, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} - gio.Hostname, _ = os.Hostname() + systemHostname, _ := os.Hostname() + gio.Hostname = extractDeviceName(ctx, systemHostname) gio.WiretrusteeVersion = version.NetbirdVersion() gio.UIVersion = extractUserAgent(ctx) diff --git a/client/system/info_freebsd.go b/client/system/info_freebsd.go index de4537f1d..6c2d8a701 100644 --- a/client/system/info_freebsd.go +++ b/client/system/info_freebsd.go @@ -24,7 +24,8 @@ func GetInfo(ctx context.Context) *Info { osStr = strings.Replace(osStr, "\r\n", "", -1) osInfo := strings.Split(osStr, " ") gio := &Info{Kernel: osInfo[0], Core: osInfo[1], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} - gio.Hostname, _ = os.Hostname() + systemHostname, _ := os.Hostname() + gio.Hostname = extractDeviceName(ctx, systemHostname) gio.WiretrusteeVersion = version.NetbirdVersion() gio.UIVersion = extractUserAgent(ctx) diff --git a/client/system/info_linux.go b/client/system/info_linux.go index e3215cd09..7d43fc035 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -50,7 +50,8 @@ func GetInfo(ctx context.Context) *Info { osName = osInfo[3] } gio := &Info{Kernel: osInfo[0], Core: osInfo[1], Platform: osInfo[2], OS: osName, OSVersion: osVer, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} - gio.Hostname, _ = os.Hostname() + systemHostname, _ := os.Hostname() + gio.Hostname = extractDeviceName(ctx, systemHostname) gio.WiretrusteeVersion = version.NetbirdVersion() gio.UIVersion = extractUserAgent(ctx) diff --git a/client/system/info_test.go b/client/system/info_test.go index 11137c04c..9cc059bf3 100644 --- a/client/system/info_test.go +++ b/client/system/info_test.go @@ -24,3 +24,12 @@ func Test_UIVersion(t *testing.T) { got := GetInfo(ctx) assert.Equal(t, want, got.UIVersion) } + +func Test_CustomHostname(t *testing.T) { + // nolint + ctx := context.WithValue(context.Background(), DeviceNameCtxKey, "custom-host") + want := "custom-host" + + got := GetInfo(ctx) + assert.Equal(t, want, got.Hostname) +} diff --git a/client/system/info_windows.go b/client/system/info_windows.go index b4548a74b..c8c3276c9 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -16,7 +16,8 @@ import ( func GetInfo(ctx context.Context) *Info { ver := getOSVersion() gio := &Info{Kernel: "windows", OSVersion: ver, Core: ver, Platform: "unknown", OS: "windows", GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} - gio.Hostname, _ = os.Hostname() + systemHostname, _ := os.Hostname() + gio.Hostname = extractDeviceName(ctx, systemHostname) gio.WiretrusteeVersion = version.NetbirdVersion() gio.UIVersion = extractUserAgent(ctx)