From e33d2f19284449ae9666c0a4fa52ba3bcc638c8d Mon Sep 17 00:00:00 2001 From: naison <895703375@qq.com> Date: Wed, 7 May 2025 08:08:26 +0000 Subject: [PATCH] hotfix: fix init dir --- cmd/kubevpn/cmds/alias.go | 4 +-- cmd/kubevpn/cmds/daemon.go | 5 ++- cmd/kubevpn/cmds/status.go | 2 +- pkg/config/config.go | 13 ------- pkg/config/const.go | 72 ++++++++++++++++++++++++------------- pkg/daemon/action/logs.go | 13 +++---- pkg/daemon/action/server.go | 9 ----- pkg/daemon/daemon.go | 2 +- 8 files changed, 60 insertions(+), 60 deletions(-) diff --git a/cmd/kubevpn/cmds/alias.go b/cmd/kubevpn/cmds/alias.go index 12a74e86..61cd9ad8 100644 --- a/cmd/kubevpn/cmds/alias.go +++ b/cmd/kubevpn/cmds/alias.go @@ -119,7 +119,7 @@ func CmdAlias(f cmdutil.Factory) *cobra.Command { return nil }, } - cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFilePath()), "Path to the kubevpnconfig file to use for CLI requests.") + cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFile()), "Path to the kubevpnconfig file to use for CLI requests.") cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml") return cmd } @@ -135,7 +135,7 @@ func ParseAndGet(localFile, remoteAddr string, aliasName string) ([]Config, erro path = remoteAddr content, err = util.DownloadFileStream(path) } else { - path = config.GetConfigFilePath() + path = config.GetConfigFile() content, err = os.ReadFile(path) } if err != nil { diff --git a/cmd/kubevpn/cmds/daemon.go b/cmd/kubevpn/cmds/daemon.go index 157c1b7d..31898cc3 100644 --- a/cmd/kubevpn/cmds/daemon.go +++ b/cmd/kubevpn/cmds/daemon.go @@ -16,7 +16,6 @@ import ( "github.com/wencaiwulue/kubevpn/v2/pkg/config" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon" - "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action" "github.com/wencaiwulue/kubevpn/v2/pkg/dns" "github.com/wencaiwulue/kubevpn/v2/pkg/util" ) @@ -42,7 +41,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command { } else { go util.StartupPProf(config.PProfPort) } - return initLogfile(action.GetDaemonLogPath(opt.IsSudo)) + return initLogfile(config.GetDaemonLogPath(opt.IsSudo)) }, RunE: func(cmd *cobra.Command, args []string) (err error) { defer opt.Stop() @@ -53,7 +52,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command { if opt.IsSudo { for _, profile := range pprof.Profiles() { func() { - file, e := os.Create(filepath.Join(config.PprofPath, profile.Name())) + file, e := os.Create(filepath.Join(config.GetPProfPath(), profile.Name())) if e != nil { return } diff --git a/cmd/kubevpn/cmds/status.go b/cmd/kubevpn/cmds/status.go index 38cd27a8..3f582e8d 100644 --- a/cmd/kubevpn/cmds/status.go +++ b/cmd/kubevpn/cmds/status.go @@ -102,7 +102,7 @@ func CmdStatus(f cmdutil.Factory) *cobra.Command { }, } cmd.Flags().StringVar(&aliasName, "alias", "", "Alias name, query connect status by alias config name") - cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFilePath()), "Path to the kubevpnconfig file to use for CLI requests.") + cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFile()), "Path to the kubevpnconfig file to use for CLI requests.") cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml") cmd.Flags().StringVarP(&format, "output", "o", FormatTable, fmt.Sprintf("Output format. One of: (%s, %s, %s)", FormatJson, FormatYaml, FormatTable)) return cmd diff --git a/pkg/config/config.go b/pkg/config/config.go index b0c0acfb..17f55b8a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,8 +2,6 @@ package config import ( "net" - "os" - "path/filepath" "sync" "time" @@ -111,10 +109,6 @@ var ( GitHubOAuthToken = "" OriginImage = "ghcr.io/kubenetworks/kubevpn:" + Version - - DaemonPath string - HomePath string - PprofPath string ) var ( @@ -142,13 +136,6 @@ func init() { if err != nil { panic(err) } - dir, err := os.UserHomeDir() - if err != nil { - panic(err) - } - DaemonPath = filepath.Join(dir, HOME, Daemon) - HomePath = filepath.Join(dir, HOME) - PprofPath = filepath.Join(dir, HOME, Daemon, PProfDir) } var Debug bool diff --git a/pkg/config/const.go b/pkg/config/const.go index 256333ca..0d3ba038 100644 --- a/pkg/config/const.go +++ b/pkg/config/const.go @@ -6,19 +6,17 @@ import ( "path/filepath" "github.com/pkg/errors" - - "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/elevate" ) const ( HOME = ".kubevpn" Daemon = "daemon" - SockPath = "daemon.sock" - SudoSockPath = "sudo_daemon.sock" + SockPath = "user_daemon.sock" + SudoSockPath = "root_daemon.sock" - PidPath = "daemon.pid" - SudoPidPath = "sudo_daemon.pid" + PidPath = "user_daemon.pid" + SudoPidPath = "root_daemon.pid" UserLogFile = "user_daemon.log" SudoLogFile = "root_daemon.log" @@ -28,28 +26,41 @@ const ( TmpDir = "tmp" ) -//go:embed config.yaml -var config []byte +var ( + daemonPath string + homePath string + + //go:embed config.yaml + config []byte +) func init() { - if elevate.IsAdmin() { - return + dir, err := os.UserHomeDir() + if err != nil { + panic(err) } + homePath = filepath.Join(dir, HOME) + daemonPath = filepath.Join(dir, HOME, Daemon) - var paths = []string{DaemonPath, PprofPath, GetSyncthingPath(), GetTempPath()} + var paths = []string{homePath, daemonPath, GetPProfPath(), GetSyncthingPath(), GetTempPath()} for _, path := range paths { - err := os.MkdirAll(path, 0755) - if err != nil { - panic(err) - } - err = os.Chmod(path, 0755) - if err != nil { + _, err = os.Stat(path) + if errors.Is(err, os.ErrNotExist) { + err = os.MkdirAll(path, 0755) + if err != nil { + panic(err) + } + err = os.Chmod(path, 0755) + if err != nil { + panic(err) + } + } else if err != nil { panic(err) } } - path := filepath.Join(HomePath, ConfigFile) - _, err := os.Stat(path) + path := filepath.Join(homePath, ConfigFile) + _, err = os.Stat(path) if errors.Is(err, os.ErrNotExist) { err = os.WriteFile(path, config, 0644) } @@ -63,7 +74,7 @@ func GetSockPath(isSudo bool) string { if isSudo { name = SudoSockPath } - return filepath.Join(DaemonPath, name) + return filepath.Join(daemonPath, name) } func GetPidPath(isSudo bool) string { @@ -71,17 +82,28 @@ func GetPidPath(isSudo bool) string { if isSudo { name = SudoPidPath } - return filepath.Join(DaemonPath, name) + return filepath.Join(daemonPath, name) } func GetSyncthingPath() string { - return filepath.Join(DaemonPath, SyncthingDir) + return filepath.Join(daemonPath, SyncthingDir) } -func GetConfigFilePath() string { - return filepath.Join(HomePath, ConfigFile) +func GetConfigFile() string { + return filepath.Join(homePath, ConfigFile) } func GetTempPath() string { - return filepath.Join(HomePath, TmpDir) + return filepath.Join(homePath, TmpDir) +} + +func GetDaemonLogPath(isSudo bool) string { + if isSudo { + return filepath.Join(daemonPath, SudoLogFile) + } + return filepath.Join(daemonPath, UserLogFile) +} + +func GetPProfPath() string { + return filepath.Join(daemonPath, PProfDir) } diff --git a/pkg/daemon/action/logs.go b/pkg/daemon/action/logs.go index 1c152871..1ead0eb3 100644 --- a/pkg/daemon/action/logs.go +++ b/pkg/daemon/action/logs.go @@ -7,17 +7,18 @@ import ( "github.com/hpcloud/tail" + "github.com/wencaiwulue/kubevpn/v2/pkg/config" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc" ) func (svr *Server) Logs(req *rpc.LogRequest, resp rpc.Daemon_LogsServer) error { // only show latest N lines line := int64(max(req.Lines, -req.Lines)) - sudoLine, sudoSize, err := seekToLastLine(GetDaemonLogPath(true), line) + sudoLine, sudoSize, err := seekToLastLine(config.GetDaemonLogPath(true), line) if err != nil { return err } - userLine, userSize, err := seekToLastLine(GetDaemonLogPath(false), line) + userLine, userSize, err := seekToLastLine(config.GetDaemonLogPath(false), line) if err != nil { return err } @@ -51,12 +52,12 @@ func tee(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error { Logger: log.New(io.Discard, "", log.LstdFlags), Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart}, } - sudoFile, err := tail.TailFile(GetDaemonLogPath(true), sudoConfig) + sudoFile, err := tail.TailFile(config.GetDaemonLogPath(true), sudoConfig) if err != nil { return err } defer sudoFile.Stop() - userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig) + userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig) if err != nil { return err } @@ -108,12 +109,12 @@ func recent(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error { Logger: log.New(io.Discard, "", log.LstdFlags), Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart}, } - sudoFile, err := tail.TailFile(GetDaemonLogPath(true), sudoConfig) + sudoFile, err := tail.TailFile(config.GetDaemonLogPath(true), sudoConfig) if err != nil { return err } defer sudoFile.Stop() - userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig) + userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig) if err != nil { return err } diff --git a/pkg/daemon/action/server.go b/pkg/daemon/action/server.go index 05213857..758a2754 100644 --- a/pkg/daemon/action/server.go +++ b/pkg/daemon/action/server.go @@ -1,7 +1,6 @@ package action import ( - "path/filepath" "sync" "time" @@ -9,7 +8,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/metadata/metadatainformer" - "github.com/wencaiwulue/kubevpn/v2/pkg/config" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc" "github.com/wencaiwulue/kubevpn/v2/pkg/handler" ) @@ -33,10 +31,3 @@ type Server struct { ID string } - -func GetDaemonLogPath(isSudo bool) string { - if isSudo { - return filepath.Join(config.DaemonPath, config.SudoLogFile) - } - return filepath.Join(config.DaemonPath, config.UserLogFile) -} diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 6b962145..3aa4e109 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -44,7 +44,7 @@ type SvrOption struct { func (o *SvrOption) Start(ctx context.Context) error { l := &lumberjack.Logger{ - Filename: action.GetDaemonLogPath(o.IsSudo), + Filename: config.GetDaemonLogPath(o.IsSudo), MaxSize: 100, MaxAge: 3, MaxBackups: 3,