Skip to content

Commit db65752

Browse files
committed
Implement ping for tailnet
1 parent 04367d9 commit db65752

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

agent/agent_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,14 @@ func TestAgent(t *testing.T) {
473473
t.Run("Tailnet", func(t *testing.T) {
474474
t.Parallel()
475475
derpMap := tailnettest.RunDERPAndSTUN(t)
476-
conn := setupSSHSession(t, agent.Metadata{
476+
conn := setupAgent(t, agent.Metadata{
477477
DERPMap: derpMap,
478-
})
478+
}, 0)
479479
defer conn.Close()
480-
output, err := conn.CombinedOutput("echo test")
481-
require.NoError(t, err)
482-
t.Log(string(output))
480+
require.Eventually(t, func() bool {
481+
_, err := conn.Ping()
482+
return err == nil
483+
}, testutil.WaitMedium, testutil.IntervalFast)
483484
})
484485
}
485486

agent/conn.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
"golang.org/x/crypto/ssh"
1717
"golang.org/x/xerrors"
18+
"tailscale.com/ipn/ipnstate"
19+
"tailscale.com/tailcfg"
1820

1921
"github.com/coder/coder/peer"
2022
"github.com/coder/coder/peerbroker/proto"
@@ -139,8 +141,22 @@ type TailnetConn struct {
139141
*tailnet.Conn
140142
}
141143

142-
func (*TailnetConn) Ping() (time.Duration, error) {
143-
return 0, nil
144+
func (c *TailnetConn) Ping() (time.Duration, error) {
145+
errCh := make(chan error, 1)
146+
durCh := make(chan time.Duration, 1)
147+
c.Conn.Ping(tailnetIP, tailcfg.PingICMP, func(pr *ipnstate.PingResult) {
148+
if pr.Err != "" {
149+
errCh <- xerrors.New(pr.Err)
150+
return
151+
}
152+
durCh <- time.Duration(pr.LatencySeconds * float64(time.Second))
153+
})
154+
select {
155+
case err := <-errCh:
156+
return 0, err
157+
case dur := <-durCh:
158+
return dur, nil
159+
}
144160
}
145161

146162
func (c *TailnetConn) CloseWithError(_ error) error {

0 commit comments

Comments
 (0)