diff --git a/code/client/main.go b/code/client/main.go index 3f67044..70fa808 100644 --- a/code/client/main.go +++ b/code/client/main.go @@ -99,6 +99,7 @@ func main() { switch *act { case "install": runtime.Assert(sv.Install()) + utils.BuildLogDir(cfg.LogDir, *user) case "uninstall": runtime.Assert(sv.Uninstall()) default: diff --git a/code/server/global/conf.go b/code/server/global/conf.go index 7dfde98..c458c30 100644 --- a/code/server/global/conf.go +++ b/code/server/global/conf.go @@ -3,6 +3,8 @@ package global import ( "crypto/md5" "natpass/code/utils" + "os" + "path/filepath" "time" "github.com/lwch/runtime" @@ -42,6 +44,11 @@ func LoadConf(dir string) *Configure { } `yaml:"tls"` } runtime.Assert(yaml.Decode(dir, &cfg)) + if filepath.IsAbs(cfg.Log.Dir) { + dir, err := os.Executable() + runtime.Assert(err) + cfg.Log.Dir = filepath.Join(filepath.Dir(dir), cfg.Log.Dir) + } return &Configure{ Listen: cfg.Listen, Enc: md5.Sum([]byte(cfg.Secret)), diff --git a/code/server/main.go b/code/server/main.go index edc5ffe..efee3d4 100644 --- a/code/server/main.go +++ b/code/server/main.go @@ -6,6 +6,7 @@ import ( "fmt" "natpass/code/server/global" "natpass/code/server/handler" + "natpass/code/utils" "net" "os" "path/filepath" @@ -102,6 +103,7 @@ func main() { switch *act { case "install": runtime.Assert(sv.Install()) + utils.BuildLogDir(cfg.LogDir, *user) case "uninstall": runtime.Assert(sv.Uninstall()) default: diff --git a/code/utils/utils.go b/code/utils/utils.go new file mode 100644 index 0000000..363520d --- /dev/null +++ b/code/utils/utils.go @@ -0,0 +1,20 @@ +package utils + +import ( + "os" + "os/user" + "strconv" + + "github.com/lwch/runtime" +) + +func BuildLogDir(dir, u string) { + runtime.Assert(os.MkdirAll(dir, 0755)) + if len(u) > 0 { + us, err := user.Lookup(u) + runtime.Assert(err) + uid, _ := strconv.ParseInt(us.Uid, 10, 64) + gid, _ := strconv.ParseInt(us.Gid, 10, 64) + runtime.Assert(os.Chown(dir, int(uid), int(gid))) + } +} diff --git a/conf/common.yaml b/conf/common.yaml index c90781f..064d65f 100644 --- a/conf/common.yaml +++ b/conf/common.yaml @@ -4,6 +4,6 @@ link: read_timeout: 1s # 读取数据包超时时间 write_timeout: 1s # 发送数据包超时时间 log: - dir: ./logs # 路径 + dir: ./logs # 路径,相对于可执行文件所在目录的相对路径 size: 50M # 单个文件大小 rotate: 7 # 保留数量 \ No newline at end of file