From bdff7579a368d88be5b74aa88d694b0c2baf477c Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Sat, 7 Sep 2024 21:21:50 -0500 Subject: [PATCH] log compression --- pkg/storage/iface.go | 1 + pkg/storage/local/path.go | 4 ++++ pkg/testing/mocks/storage.go | 24 +++++++++++++++++++++++ pkg/wireguard/packetlogger.go | 36 ++++++++++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/pkg/storage/iface.go b/pkg/storage/iface.go index 9ba91fe..212b1a4 100644 --- a/pkg/storage/iface.go +++ b/pkg/storage/iface.go @@ -8,6 +8,7 @@ type Iface interface { EnsureOwnership(filename, login string) error ReadDir(name string) ([]string, error) Remove(name string) error + Rename(oldName, newName string) error AppendFile(name string, data []byte) error ReadWriter Seeker diff --git a/pkg/storage/local/path.go b/pkg/storage/local/path.go index 03dee39..c4b5773 100644 --- a/pkg/storage/local/path.go +++ b/pkg/storage/local/path.go @@ -81,3 +81,7 @@ func (l *LocalStorage) ReadDir(pathname string) ([]string, error) { func (l *LocalStorage) Remove(name string) error { return os.Remove(path.Join(l.path, name)) } + +func (l *LocalStorage) Rename(oldName, newName string) error { + return os.Rename(oldName, newName) +} diff --git a/pkg/testing/mocks/storage.go b/pkg/testing/mocks/storage.go index 27f9d61..4bf82c8 100644 --- a/pkg/testing/mocks/storage.go +++ b/pkg/testing/mocks/storage.go @@ -69,6 +69,18 @@ func (m *MockReadWriter) OpenFileForWriting(name string) (io.WriteCloser, error) buf := bufio.NewWriter(bytes.NewBuffer(m.Data[name])) return &MyWriteCloser{buf}, nil } +func (m *MockReadWriter) Rename(oldName, newName string) error { + if m.Data == nil { + m.Data = make(map[string][]byte) + } + _, ok := m.Data[oldName] + if !ok { + return fmt.Errorf("file doesn't exist") + } + m.Data[newName] = m.Data[oldName] + delete(m.Data, oldName) + return nil +} type MockMemoryStorage struct { Data map[string][]byte @@ -77,6 +89,18 @@ type MockMemoryStorage struct { func (m *MockMemoryStorage) ConfigPath(filename string) string { return path.Join("config", filename) } +func (m *MockMemoryStorage) Rename(oldName, newName string) error { + if m.Data == nil { + m.Data = make(map[string][]byte) + } + _, ok := m.Data[oldName] + if !ok { + return fmt.Errorf("file doesn't exist") + } + m.Data[newName] = m.Data[oldName] + delete(m.Data, oldName) + return nil +} func (m *MockMemoryStorage) FileExists(name string) bool { if m.Data == nil { m.Data = make(map[string][]byte) diff --git a/pkg/wireguard/packetlogger.go b/pkg/wireguard/packetlogger.go index a329dcd..b1fbe13 100644 --- a/pkg/wireguard/packetlogger.go +++ b/pkg/wireguard/packetlogger.go @@ -3,8 +3,10 @@ package wireguard import ( "bufio" "bytes" + "compress/gzip" "encoding/binary" "fmt" + "io" "net" "net/http" "os" @@ -315,18 +317,23 @@ func packetLoggerLogRotation(storage storage.Iface) error { dateParsed, err := time.Parse("2006-01-02", filenameSplit[len(filenameSplit)-3]) if err == nil { if !dateutils.DateEqual(dateParsed, time.Now()) { - err := packetLoggerRotateLog(storage, filename) + err := packetLoggerCompressLog(storage, filename) if err != nil { return fmt.Errorf("rotate log error: %s", err) } + err = packetLoggerRenameLog(storage, filename) + if err != nil { + return fmt.Errorf("rotate log error (rename): %s", err) + } } + } } } return nil } -func packetLoggerRotateLog(storage storage.Iface, filename string) error { +func packetLoggerCompressLog(storage storage.Iface, filename string) error { reader, err := storage.OpenFile(path.Join(VPN_STATS_DIR, VPN_PACKETLOGGER_DIR, filename)) if err != nil { return fmt.Errorf("open file error (%s): %s", filename, err) @@ -337,6 +344,29 @@ func packetLoggerRotateLog(storage storage.Iface, filename string) error { } defer reader.Close() defer writer.Close() - // compress, write, rename + + gzipWriter, err := gzip.NewWriterLevel(writer, gzip.DefaultCompression) + if err != nil { + return fmt.Errorf("gzip writer error: %s", err) + } + _, err = io.Copy(gzipWriter, reader) + if err != nil { + return fmt.Errorf("copy error: %s", err) + } + err = gzipWriter.Close() + if err != nil { + return fmt.Errorf("file close error (gzip): %s", err) + } + return nil +} +func packetLoggerRenameLog(storage storage.Iface, filename string) error { + err := storage.Rename(path.Join(VPN_STATS_DIR, VPN_PACKETLOGGER_DIR, filename+".gz.tmp"), path.Join(VPN_STATS_DIR, VPN_PACKETLOGGER_DIR, filename+".gz")) + if err != nil { + return fmt.Errorf("rename error: %s", err) + } + err = storage.Remove(path.Join(VPN_STATS_DIR, VPN_PACKETLOGGER_DIR, filename)) + if err != nil { + return fmt.Errorf("delete log error: %s", err) + } return nil }