From 8fe7aee91705924349ba8959d069ac537b46e8b1 Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Fri, 6 Sep 2024 13:16:20 -0500 Subject: [PATCH] enable/disable packetlogs at runtime --- pkg/configmanager/handlers.go | 7 +++++++ pkg/wireguard/packetlogger.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/configmanager/handlers.go b/pkg/configmanager/handlers.go index 05f7a4f..c0b5314 100644 --- a/pkg/configmanager/handlers.go +++ b/pkg/configmanager/handlers.go @@ -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) diff --git a/pkg/wireguard/packetlogger.go b/pkg/wireguard/packetlogger.go index ef51b5e..e9331d1 100644 --- a/pkg/wireguard/packetlogger.go +++ b/pkg/wireguard/packetlogger.go @@ -12,6 +12,7 @@ import ( "runtime" "strconv" "strings" + "sync" "time" "github.com/gopacket/gopacket" @@ -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 { @@ -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)