diff --git a/code/client/global/conf.go b/code/client/global/conf.go index 0dd31a9..354dc0a 100644 --- a/code/client/global/conf.go +++ b/code/client/global/conf.go @@ -2,6 +2,7 @@ package global import ( "crypto/md5" + "natpass/code/utils" "os" "github.com/lwch/runtime" @@ -23,7 +24,7 @@ type Configure struct { Server string Enc [md5.Size]byte LogDir string - LogSize int + LogSize utils.Bytes LogRotate int Tunnels []Tunnel } @@ -34,9 +35,9 @@ func LoadConf(dir string) *Configure { Server string `yaml:"server"` Secret string `yaml:"secret"` Log struct { - Dir string `yaml:"dir"` - Size int `yaml:"size"` - Rotate int `yaml:"rotate"` + Dir string `yaml:"dir"` + Size utils.Bytes `yaml:"size"` + Rotate int `yaml:"rotate"` } `yaml:"log"` Tunnel []Tunnel `yaml:"tunnel"` } diff --git a/code/client/main.go b/code/client/main.go index c4526a6..212676f 100644 --- a/code/client/main.go +++ b/code/client/main.go @@ -33,7 +33,7 @@ func main() { cfg := global.LoadConf(*conf) - logging.SetSizeRotate(cfg.LogDir, "np-cli", cfg.LogSize, cfg.LogRotate, true) + logging.SetSizeRotate(cfg.LogDir, "np-cli", int(cfg.LogSize.Bytes()), cfg.LogRotate, true) conn, err := tls.Dial("tcp", cfg.Server, nil) runtime.Assert(err) diff --git a/code/server/global/conf.go b/code/server/global/conf.go index 1787697..0a4edc5 100644 --- a/code/server/global/conf.go +++ b/code/server/global/conf.go @@ -2,6 +2,7 @@ package global import ( "crypto/md5" + "natpass/code/utils" "os" "github.com/lwch/runtime" @@ -14,7 +15,7 @@ type Configure struct { TLSKey string TLSCrt string LogDir string - LogSize int + LogSize utils.Bytes LogRotate int } @@ -23,9 +24,9 @@ func LoadConf(dir string) *Configure { Listen uint16 `yaml:"listen"` Secret string `yaml:"secret"` Log struct { - Dir string `yaml:"dir"` - Size int `yaml:"size"` - Rotate int `yaml:"rotate"` + Dir string `yaml:"dir"` + Size utils.Bytes `yaml:"size"` + Rotate int `yaml:"rotate"` } `yaml:"log"` TLS struct { Key string `yaml:"key"` diff --git a/code/server/main.go b/code/server/main.go index 325679e..83d66fe 100644 --- a/code/server/main.go +++ b/code/server/main.go @@ -31,7 +31,7 @@ func main() { cfg := global.LoadConf(*conf) - logging.SetSizeRotate(cfg.LogDir, "np-svr", cfg.LogSize, cfg.LogRotate, true) + logging.SetSizeRotate(cfg.LogDir, "np-svr", int(cfg.LogSize.Bytes()), cfg.LogRotate, true) cert, err := tls.LoadX509KeyPair(cfg.TLSCrt, cfg.TLSKey) runtime.Assert(err) diff --git a/code/utils/bytes.go b/code/utils/bytes.go new file mode 100644 index 0000000..a524ae4 --- /dev/null +++ b/code/utils/bytes.go @@ -0,0 +1,23 @@ +package utils + +import "github.com/dustin/go-humanize" + +type Bytes uint64 + +func (bt *Bytes) UnmarshalYAML(unmarshal func(interface{}) error) error { + var str string + err := unmarshal(&str) + if err != nil { + return err + } + n, err := humanize.ParseBytes(str) + if err != nil { + return err + } + *bt = Bytes(n) + return nil +} + +func (bt *Bytes) Bytes() uint64 { + return uint64(*bt) +} diff --git a/go.mod b/go.mod index 3be3662..be2e6a7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module natpass go 1.16 require ( + github.com/dustin/go-humanize v1.0.0 github.com/lwch/daemon v0.0.0-20210628111519-5911af6cec06 github.com/lwch/logging v0.0.0-20210528090125-a154917d90c6 github.com/lwch/runtime v0.0.0-20190520054850-8c97e19e0c6d diff --git a/go.sum b/go.sum index fc20cf0..6db6ebd 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=