various: pass logger.Logf through to more places

Updates #7537

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: Id89acab70ea678c8c7ff0f44792d54c7223337c6
pull/7545/head
Andrew Dunham 2 years ago
parent 958c89470b
commit 83fa17d26c

@ -212,6 +212,7 @@ func NewDirect(opts Options) (*Direct, error) {
Forward: dnscache.Get().Forward, // use default cache's forwarder Forward: dnscache.Get().Forward, // use default cache's forwarder
UseLastGood: true, UseLastGood: true,
LookupIPFallback: dnsfallback.Lookup, LookupIPFallback: dnsfallback.Lookup,
Logf: opts.Logf,
} }
tr := http.DefaultTransport.(*http.Transport).Clone() tr := http.DefaultTransport.(*http.Transport).Clone()
tr.Proxy = tshttpproxy.ProxyFromEnvironment tr.Proxy = tshttpproxy.ProxyFromEnvironment

@ -388,12 +388,14 @@ func (a *Dialer) tryURLUpgrade(ctx context.Context, u *url.URL, addr netip.Addr,
dns = &dnscache.Resolver{ dns = &dnscache.Resolver{
SingleHostStaticResult: []netip.Addr{addr}, SingleHostStaticResult: []netip.Addr{addr},
SingleHost: u.Hostname(), SingleHost: u.Hostname(),
Logf: a.Logf, // not a.logf method; we want to propagate nil-ness
} }
} else { } else {
dns = &dnscache.Resolver{ dns = &dnscache.Resolver{
Forward: dnscache.Get().Forward, Forward: dnscache.Get().Forward,
LookupIPFallback: dnsfallback.Lookup, LookupIPFallback: dnsfallback.Lookup,
UseLastGood: true, UseLastGood: true,
Logf: a.Logf, // not a.logf method; we want to propagate nil-ness
} }
} }

@ -384,6 +384,7 @@ func (f *forwarder) getKnownDoHClientForProvider(urlBase string) (c *http.Client
dialer := dnscache.Dialer(nsDialer.DialContext, &dnscache.Resolver{ dialer := dnscache.Dialer(nsDialer.DialContext, &dnscache.Resolver{
SingleHost: dohURL.Hostname(), SingleHost: dohURL.Hostname(),
SingleHostStaticResult: allIPs, SingleHostStaticResult: allIPs,
Logf: f.logf,
}) })
c = &http.Client{ c = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{

@ -250,10 +250,13 @@ func SetCachePath(path string) {
// logfunc stores the logging function to use for this package. // logfunc stores the logging function to use for this package.
var logfunc syncs.AtomicValue[logger.Logf] var logfunc syncs.AtomicValue[logger.Logf]
// SetLogger sets the logging function that this package will use. The default // SetLogger sets the logging function that this package will use, and returns
// logger if this function is not called is 'log.Printf'. // the old value (which may be nil).
func SetLogger(log logger.Logf) { //
logfunc.Store(log) // If this function is never called, or if this function is called with a nil
// value, 'log.Printf' will be used to print logs.
func SetLogger(log logger.Logf) (old logger.Logf) {
return logfunc.Swap(log)
} }
func logf(format string, args ...any) { func logf(format string, args ...any) {

@ -40,6 +40,7 @@ import (
"tailscale.com/logpolicy" "tailscale.com/logpolicy"
"tailscale.com/logtail" "tailscale.com/logtail"
"tailscale.com/logtail/filch" "tailscale.com/logtail/filch"
"tailscale.com/net/dnsfallback"
"tailscale.com/net/memnet" "tailscale.com/net/memnet"
"tailscale.com/net/proxymux" "tailscale.com/net/proxymux"
"tailscale.com/net/socks5" "tailscale.com/net/socks5"
@ -619,6 +620,25 @@ func (s *Server) logf(format string, a ...interface{}) {
log.Printf(format, a...) log.Printf(format, a...)
} }
// ReplaceGlobalLoggers will replace any Tailscale-specific package-global
// loggers with this Server's logger. It returns a function that, when called,
// will undo any changes made.
//
// Note that calling this function from multiple Servers will result in the
// last call taking all logs; logs are not duplicated.
func (s *Server) ReplaceGlobalLoggers() (undo func()) {
var undos []func()
oldDnsFallback := dnsfallback.SetLogger(s.logf)
undos = append(undos, func() { dnsfallback.SetLogger(oldDnsFallback) })
return func() {
for _, fn := range undos {
fn()
}
}
}
// printAuthURLLoop loops once every few seconds while the server is still running and // printAuthURLLoop loops once every few seconds while the server is still running and
// is in NeedsLogin state, printing out the auth URL. // is in NeedsLogin state, printing out the auth URL.
func (s *Server) printAuthURLLoop() { func (s *Server) printAuthURLLoop() {

Loading…
Cancel
Save