From 4007601f734fb32b65314b90972d167cda14a49c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 6 Jun 2022 13:07:14 -0700 Subject: [PATCH] cmd/controlclient: wire up PingRequest peerapi pings too Updates tailscale/corp#754 Change-Id: I61ac3fc44783b54bd02455bcb0baf19159b7a9d2 Signed-off-by: Brad Fitzpatrick --- control/controlclient/direct.go | 26 +++++++++++++++----------- ipn/ipnlocal/local.go | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/control/controlclient/direct.go b/control/controlclient/direct.go index 7d9671dfb..bff285865 100644 --- a/control/controlclient/direct.go +++ b/control/controlclient/direct.go @@ -124,11 +124,10 @@ type Options struct { Pinger Pinger } -// Pinger is a subset of the wgengine.Engine interface, containing just the Ping method. +// Pinger is the LocalBackend.Ping method. type Pinger interface { - // Ping is a request to start a ping with the peer handling the given IP and - // then call cb with its ping latency & method. - Ping(ip netaddr.IP, pingType tailcfg.PingType, cb func(*ipnstate.PingResult)) + // Ping is a request to do a ping with the peer handling the given IP. + Ping(ctx context.Context, ip netaddr.IP, pingType tailcfg.PingType) (*ipnstate.PingResult, error) } type Decompressor interface { @@ -1208,10 +1207,8 @@ func answerPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, pinge } for _, t := range strings.Split(pr.Types, ",") { switch pt := tailcfg.PingType(t); pt { - case tailcfg.PingTSMP, tailcfg.PingDisco, tailcfg.PingICMP: + case tailcfg.PingTSMP, tailcfg.PingDisco, tailcfg.PingICMP, tailcfg.PingPeerAPI: go doPingerPing(logf, c, pr, pinger, pt) - // TODO(tailscale/corp#754) - // case "peerapi": default: logf("unsupported ping request type: %q", t) } @@ -1417,10 +1414,17 @@ func doPingerPing(logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, pin return } start := time.Now() - pinger.Ping(pr.IP, pingType, func(res *ipnstate.PingResult) { - // Currently does not check for error since we just return if it fails. - postPingResult(start, logf, c, pr, res.ToPingResponse(pingType)) - }) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + res, err := pinger.Ping(ctx, pr.IP, pingType) + if err != nil { + d := time.Since(start).Round(time.Millisecond) + logf("doPingerPing: ping error of type %q to %v after %v: %v", pingType, pr.IP, d, err) + return + } + postPingResult(start, logf, c, pr, res.ToPingResponse(pingType)) } func postPingResult(start time.Time, logf logger.Logf, c *http.Client, pr *tailcfg.PingRequest, res *tailcfg.PingResponse) error { diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 4fd5c60e6..334f9a407 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1035,7 +1035,7 @@ func (b *LocalBackend) Start(opts ipn.Options) error { DiscoPublicKey: discoPublic, DebugFlags: debugFlags, LinkMonitor: b.e.GetLinkMonitor(), - Pinger: b.e, + Pinger: b, PopBrowserURL: b.tellClientToBrowseToURL, Dialer: b.Dialer(),