Skip to content

Commit

Permalink
log compression
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Sep 8, 2024
1 parent 75fb5ec commit bdff757
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/storage/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/storage/local/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 24 additions & 0 deletions pkg/testing/mocks/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
36 changes: 33 additions & 3 deletions pkg/wireguard/packetlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package wireguard
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"fmt"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit bdff757

Please sign in to comment.