Skip to content

Commit

Permalink
enable/disable packetlogs at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Sep 6, 2024
1 parent 404b396 commit 8fe7aee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/configmanager/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ func (c *ConfigManager) refreshServerConfig(w http.ResponseWriter, r *http.Reque
returnError(w, fmt.Errorf("get vpn config error: %s", err), http.StatusBadRequest)
return
}
startPacketLogger := false
if vpnConfig.EnablePacketLogs && !c.VPNConfig.EnablePacketLogs {
startPacketLogger = true
}
c.VPNConfig.EnablePacketLogs = vpnConfig.EnablePacketLogs
c.VPNConfig.PacketLogsTypes = vpnConfig.PacketLogsTypes
if startPacketLogger {
go wireguard.RunPacketLogger(c.Storage, c.ClientCache, c.VPNConfig)
}
w.WriteHeader(http.StatusAccepted)
default:
returnError(w, fmt.Errorf("method not supported"), http.StatusBadRequest)
Expand Down
10 changes: 10 additions & 0 deletions pkg/wireguard/packetlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/gopacket/gopacket"
Expand All @@ -22,10 +23,18 @@ import (
"golang.org/x/sys/unix"
)

var (
PacketLoggerIsRunning sync.Mutex
)

func RunPacketLogger(storage storage.Iface, clientCache *ClientCache, vpnConfig *VPNConfig) {
if !vpnConfig.EnablePacketLogs {
return
}
fmt.Printf("starting packetlogger")
// ensure we only run a single instance of the packet logger
PacketLoggerIsRunning.Lock()
defer PacketLoggerIsRunning.Unlock()
// ensure logs dir is created
err := storage.EnsurePath(VPN_STATS_DIR)
if err != nil {
Expand Down Expand Up @@ -57,6 +66,7 @@ func RunPacketLogger(storage storage.Iface, clientCache *ClientCache, vpnConfig
logging.ErrorLog(fmt.Errorf("can't start packet inspector: %s", err))
return
}
defer handle.Close()
i := 0
for {
err := readPacket(storage, handle, clientCache)
Expand Down

0 comments on commit 8fe7aee

Please sign in to comment.