fix(wireguard): add xray-style timestamps to streamed logs · PasarGuard/node@216b1f3 · GitHub
Skip to content

Commit 216b1f3

Browse files
committed
fix(wireguard): add xray-style timestamps to streamed logs
1 parent ee28f96 commit 216b1f3

4 files changed

Lines changed: 73 additions & 1 deletion

File tree

backend/wireguard/jobs.go

Lines changed: 1 addition & 0 deletions

backend/wireguard/log.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package wireguard
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type logSeverity string
9+
10+
const (
11+
wireGuardLogTimestampFormat = "2006/01/02 15:04:05.000000"
12+
13+
logSeverityInfo logSeverity = "Info"
14+
logSeverityWarning logSeverity = "Warning"
15+
logSeverityError logSeverity = "Error"
16+
)
17+
18+
func formatWireGuardLogLine(severity logSeverity, message string) string {
19+
timestamp := time.Now().Format(wireGuardLogTimestampFormat)
20+
return fmt.Sprintf("%s [%s] wireguard: %s", timestamp, severity, message)
21+
}
22+
23+
func (wg *WireGuard) emitInfoLogf(format string, args ...any) {
24+
wg.emitLogf(logSeverityInfo, format, args...)
25+
}
26+
27+
func (wg *WireGuard) emitWarningLogf(format string, args ...any) {
28+
wg.emitLogf(logSeverityWarning, format, args...)
29+
}
30+
31+
func (wg *WireGuard) emitErrorLogf(format string, args ...any) {
32+
wg.emitLogf(logSeverityError, format, args...)
33+
}
34+
35+
func (wg *WireGuard) emitLogf(severity logSeverity, format string, args ...any) {
36+
wg.emitLog(severity, fmt.Sprintf(format, args...))
37+
}
38+
39+
func (wg *WireGuard) emitLog(severity logSeverity, message string) {
40+
wg.mu.RLock()
41+
defer wg.mu.RUnlock()
42+
43+
wg.emitLogLocked(severity, message)
44+
}
45+
46+
// emitLogLocked sends to the backend log channel while the caller already holds wg.mu.
47+
func (wg *WireGuard) emitLogLocked(severity logSeverity, message string) {
48+
if wg.logChan == nil {
49+
return
50+
}
51+
52+
select {
53+
case wg.logChan <- formatWireGuardLogLine(severity, message):
54+
default:
55+
}
56+
}

backend/wireguard/wireguard.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func newWithManagerFactory(cfg *config.Config, wgConfig *Config, users []*common
195195
wg.mu.Unlock()
196196

197197
log.Println("wireguard started, Version:", wg.Version())
198-
wg.logChan <- fmt.Sprintf("WireGuard interface %s initialized successfully", wgConfig.InterfaceName)
198+
wg.emitInfoLogf("WireGuard interface %s initialized successfully", wgConfig.InterfaceName)
199199

200200
return wg, nil
201201
}
@@ -266,6 +266,7 @@ func (wg *WireGuard) restartLocked() error {
266266
wg.mu.Unlock()
267267

268268
log.Println("dynamically reconfiguring wireguard interface")
269+
wg.emitInfoLogf("dynamically reconfiguring wireguard interface")
269270

270271
config := wgtypes.Config{
271272
PrivateKey: &privateKey,
@@ -279,6 +280,7 @@ func (wg *WireGuard) restartLocked() error {
279280
}
280281

281282
log.Println("wireguard interface reconfigured successfully without downtime")
283+
wg.emitInfoLogf("wireguard interface reconfigured successfully without downtime")
282284
return nil
283285
}
284286

@@ -327,12 +329,14 @@ func (wg *WireGuard) shutdownLocked() {
327329
if wg.manager != nil {
328330
if err := wg.manager.Close(); err != nil {
329331
log.Printf("error closing manager: %v", err)
332+
wg.emitLogLocked(logSeverityError, fmt.Sprintf("error closing manager: %v", err))
330333
}
331334
wg.manager = nil
332335
}
333336

334337
wg.state = lifecycleStopped
335338
wg.version = ""
339+
wg.emitLogLocked(logSeverityInfo, "wireguard shutdown complete")
336340
if wg.logChan != nil {
337341
close(wg.logChan)
338342
wg.logChan = nil

backend/wireguard/wireguard_lifecycle_test.go

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)