2021-01-21 20:33:54 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package wglog contains logging helpers for wireguard-go.
|
|
|
|
package wglog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-05-25 01:00:46 +01:00
|
|
|
"sync"
|
2021-01-21 20:33:54 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
2021-05-25 20:42:22 +01:00
|
|
|
"golang.zx2c4.com/wireguard/device"
|
2021-10-28 01:42:33 +01:00
|
|
|
"tailscale.com/types/key"
|
2021-01-21 20:33:54 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2021-01-21 20:33:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Logger is a wireguard-go log wrapper that cleans up and rewrites log lines.
|
|
|
|
// It can be modified at run time to adjust to new wireguard-go configurations.
|
|
|
|
type Logger struct {
|
|
|
|
DeviceLogger *device.Logger
|
2021-10-28 01:42:33 +01:00
|
|
|
replace atomic.Value // of map[string]string
|
|
|
|
mu sync.Mutex // protects strs
|
|
|
|
strs map[key.NodePublic]*strCache // cached strs used to populate replace
|
2021-05-25 01:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// strCache holds a wireguard-go and a Tailscale style peer string.
|
|
|
|
type strCache struct {
|
|
|
|
wg, ts string
|
|
|
|
used bool // track whether this strCache was used in a particular round
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger creates a new logger for use with wireguard-go.
|
|
|
|
// This logger silences repetitive/unhelpful noisy log lines
|
|
|
|
// and rewrites peer keys from wireguard-go into Tailscale format.
|
|
|
|
func NewLogger(logf logger.Logf) *Logger {
|
|
|
|
ret := new(Logger)
|
|
|
|
wrapper := func(format string, args ...interface{}) {
|
2021-04-27 17:44:51 +01:00
|
|
|
if strings.Contains(format, "Routine:") && !strings.Contains(format, "receive incoming") {
|
2021-01-21 20:33:54 +00:00
|
|
|
// wireguard-go logs as it starts and stops routines.
|
|
|
|
// Drop those; there are a lot of them, and they're just noise.
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 17:44:51 +01:00
|
|
|
if strings.Contains(format, "Failed to send data packet") {
|
2021-02-01 22:37:58 +00:00
|
|
|
// Drop. See https://github.com/tailscale/tailscale/issues/1239.
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 17:44:51 +01:00
|
|
|
if strings.Contains(format, "Interface up requested") || strings.Contains(format, "Interface down requested") {
|
2021-02-23 05:57:51 +00:00
|
|
|
// Drop. Logs 1/s constantly while the tun device is open.
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/1388.
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 18:03:13 +01:00
|
|
|
replace, _ := ret.replace.Load().(map[string]string)
|
|
|
|
if replace == nil {
|
2021-01-21 20:33:54 +00:00
|
|
|
// No replacements specified; log as originally planned.
|
|
|
|
logf(format, args...)
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 18:03:13 +01:00
|
|
|
// Duplicate the args slice so that we can modify it.
|
|
|
|
// This is not always required, but the code required to avoid it is not worth the complexity.
|
|
|
|
newargs := make([]interface{}, len(args))
|
|
|
|
copy(newargs, args)
|
|
|
|
for i, arg := range newargs {
|
|
|
|
// We want to replace *device.Peer args with the Tailscale-formatted version of themselves.
|
|
|
|
// Using *device.Peer directly makes this hard to test, so we string any fmt.Stringers,
|
|
|
|
// and if the string ends up looking exactly like a known Peer, we replace it.
|
|
|
|
// This is slightly imprecise, in that we don't check the formatting verb. Oh well.
|
|
|
|
s, ok := arg.(fmt.Stringer)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wgStr := s.String()
|
|
|
|
tsStr, ok := replace[wgStr]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newargs[i] = tsStr
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
2021-04-27 18:03:13 +01:00
|
|
|
logf(format, newargs...)
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
ret.DeviceLogger = &device.Logger{
|
2021-02-03 23:24:13 +00:00
|
|
|
Verbosef: logger.WithPrefix(wrapper, "[v2] "),
|
|
|
|
Errorf: wrapper,
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
2021-10-28 01:42:33 +01:00
|
|
|
ret.strs = make(map[key.NodePublic]*strCache)
|
2021-01-21 20:33:54 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPeers adjusts x to rewrite the peer public keys found in peers.
|
|
|
|
// SetPeers is safe for concurrent use.
|
|
|
|
func (x *Logger) SetPeers(peers []wgcfg.Peer) {
|
2021-05-25 01:00:46 +01:00
|
|
|
x.mu.Lock()
|
|
|
|
defer x.mu.Unlock()
|
2021-01-21 20:33:54 +00:00
|
|
|
// Construct a new peer public key log rewriter.
|
2021-04-27 18:03:13 +01:00
|
|
|
replace := make(map[string]string)
|
2021-01-21 20:33:54 +00:00
|
|
|
for _, peer := range peers {
|
2021-05-25 01:00:46 +01:00
|
|
|
c, ok := x.strs[peer.PublicKey] // look up cached strs
|
|
|
|
if !ok {
|
2021-10-28 01:42:33 +01:00
|
|
|
wg := peer.PublicKey.WireGuardGoString()
|
2021-05-25 01:00:46 +01:00
|
|
|
ts := peer.PublicKey.ShortString()
|
|
|
|
c = &strCache{wg: wg, ts: ts}
|
|
|
|
x.strs[peer.PublicKey] = c
|
|
|
|
}
|
|
|
|
c.used = true
|
|
|
|
replace[c.wg] = c.ts
|
|
|
|
}
|
|
|
|
// Remove any unused cached strs.
|
|
|
|
for k, c := range x.strs {
|
|
|
|
if !c.used {
|
|
|
|
delete(x.strs, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Mark c as unused for next round.
|
|
|
|
c.used = false
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
2021-04-27 18:03:13 +01:00
|
|
|
x.replace.Store(replace)
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|