Skip to content

Commit

Permalink
[client] make /var/lib/netbird paths configurable (#3084)
Browse files Browse the repository at this point in the history
- NB_STATE_DIR
- NB_UNCLEAN_SHUTDOWN_RESOLV_FILE
- NB_DNS_STATE_FILE
  • Loading branch information
nazarewk authored Jan 13, 2025
1 parent 8154069 commit 522dd44
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
24 changes: 24 additions & 0 deletions client/configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package configs

import (
"os"
"path/filepath"
"runtime"
)

var StateDir string

func init() {
StateDir = os.Getenv("NB_STATE_DIR")
if StateDir != "" {
return
}
switch runtime.GOOS {
case "windows":
StateDir = filepath.Join(os.Getenv("PROGRAMDATA"), "Netbird")
case "darwin", "linux":
StateDir = "/var/lib/netbird"
case "freebsd", "openbsd", "netbsd", "dragonfly":
StateDir = "/var/db/netbird"
}
}
18 changes: 18 additions & 0 deletions client/internal/dns/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !android

package dns

import (
"github.com/netbirdio/netbird/client/configs"
"os"
"path/filepath"
)

var fileUncleanShutdownResolvConfLocation string

func init() {
fileUncleanShutdownResolvConfLocation = os.Getenv("NB_UNCLEAN_SHUTDOWN_RESOLV_FILE")
if fileUncleanShutdownResolvConfLocation == "" {
fileUncleanShutdownResolvConfLocation = filepath.Join(configs.StateDir, "resolv.conf")
}
}
5 changes: 0 additions & 5 deletions client/internal/dns/consts_freebsd.go

This file was deleted.

7 changes: 0 additions & 7 deletions client/internal/dns/consts_linux.go

This file was deleted.

15 changes: 4 additions & 11 deletions client/internal/statemanager/path.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package statemanager

import (
"github.com/netbirdio/netbird/client/configs"
"os"
"path/filepath"
"runtime"
)

// GetDefaultStatePath returns the path to the state file based on the operating system
// It returns an empty string if the path cannot be determined.
func GetDefaultStatePath() string {
switch runtime.GOOS {
case "windows":
return filepath.Join(os.Getenv("PROGRAMDATA"), "Netbird", "state.json")
case "darwin", "linux":
return "/var/lib/netbird/state.json"
case "freebsd", "openbsd", "netbsd", "dragonfly":
return "/var/db/netbird/state.json"
if path := os.Getenv("NB_DNS_STATE_FILE"); path != "" {
return path
}

return ""

return filepath.Join(configs.StateDir, "state.json")
}

0 comments on commit 522dd44

Please sign in to comment.