Skip to content

Commit bc14e92

Browse files
authored
feat: add option to speedtest to dump a pcap of network traffic (#11848)
1 parent b2bc3ff commit bc14e92

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

cli/speedtest.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package cli
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"time"
78

89
"github.com/jedib0t/go-pretty/v6/table"
910
"golang.org/x/xerrors"
1011
tsspeedtest "tailscale.com/net/speedtest"
12+
"tailscale.com/wgengine/capture"
1113

1214
"cdr.dev/slog"
1315
"cdr.dev/slog/sloggers/sloghuman"
@@ -21,6 +23,7 @@ func (r *RootCmd) speedtest() *clibase.Cmd {
2123
direct bool
2224
duration time.Duration
2325
direction string
26+
pcapFile string
2427
)
2528
client := new(codersdk.Client)
2629
cmd := &clibase.Cmd{
@@ -63,6 +66,7 @@ func (r *RootCmd) speedtest() *clibase.Cmd {
6366
return err
6467
}
6568
defer conn.Close()
69+
6670
if direct {
6771
ticker := time.NewTicker(time.Second)
6872
defer ticker.Stop()
@@ -95,6 +99,19 @@ func (r *RootCmd) speedtest() *clibase.Cmd {
9599
} else {
96100
conn.AwaitReachable(ctx)
97101
}
102+
103+
if pcapFile != "" {
104+
s := capture.New()
105+
conn.InstallCaptureHook(s.LogPacket)
106+
f, err := os.OpenFile(pcapFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
107+
if err != nil {
108+
return err
109+
}
110+
defer f.Close()
111+
unregister := s.RegisterOutput(f)
112+
defer unregister()
113+
}
114+
98115
var tsDir tsspeedtest.Direction
99116
switch direction {
100117
case "up":
@@ -146,6 +163,12 @@ func (r *RootCmd) speedtest() *clibase.Cmd {
146163
Default: tsspeedtest.DefaultDuration.String(),
147164
Value: clibase.DurationOf(&duration),
148165
},
166+
{
167+
Description: "Specifies a file to write a network capture to.",
168+
Flag: "pcap-file",
169+
Default: "",
170+
Value: clibase.StringOf(&pcapFile),
171+
},
149172
}
150173
return cmd
151174
}

cli/testdata/coder_speedtest_--help.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ OPTIONS:
1414
Specifies whether to run in reverse mode where the client receives and
1515
the server sends.
1616

17+
--pcap-file string
18+
Specifies a file to write a network capture to.
19+
1720
-t, --time duration (default: 5s)
1821
Specifies the duration to monitor traffic.
1922

docs/cli/speedtest.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tailnet/conn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
tslogger "tailscale.com/types/logger"
3131
"tailscale.com/types/netlogtype"
3232
"tailscale.com/wgengine"
33+
"tailscale.com/wgengine/capture"
3334
"tailscale.com/wgengine/magicsock"
3435
"tailscale.com/wgengine/netstack"
3536
"tailscale.com/wgengine/router"
@@ -310,6 +311,12 @@ type Conn struct {
310311
trafficStats *connstats.Statistics
311312
}
312313

314+
func (c *Conn) InstallCaptureHook(f capture.Callback) {
315+
c.mutex.Lock()
316+
defer c.mutex.Unlock()
317+
c.wireguardEngine.InstallCaptureHook(f)
318+
}
319+
313320
func (c *Conn) MagicsockSetDebugLoggingEnabled(enabled bool) {
314321
c.magicConn.SetDebugLoggingEnabled(enabled)
315322
}

0 commit comments

Comments
 (0)