Skip to content

Commit 76f3743

Browse files
committed
fix: start packet capture immediately on speedtest
1 parent 93d8812 commit 76f3743

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

cli/speedtest.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,22 @@ func (r *RootCmd) speedtest() *serpent.Command {
6060
if r.disableDirect {
6161
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
6262
}
63+
opts := &workspacesdk.DialAgentOptions{
64+
Logger: logger,
65+
}
66+
if pcapFile != "" {
67+
s := capture.New()
68+
opts.CaptureHook = s.LogPacket
69+
f, err := os.OpenFile(pcapFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
70+
if err != nil {
71+
return err
72+
}
73+
defer f.Close()
74+
unregister := s.RegisterOutput(f)
75+
defer unregister()
76+
}
6377
conn, err := workspacesdk.New(client).
64-
DialAgent(ctx, workspaceAgent.ID, &workspacesdk.DialAgentOptions{
65-
Logger: logger,
66-
})
78+
DialAgent(ctx, workspaceAgent.ID, opts)
6779
if err != nil {
6880
return err
6981
}
@@ -102,18 +114,6 @@ func (r *RootCmd) speedtest() *serpent.Command {
102114
conn.AwaitReachable(ctx)
103115
}
104116

105-
if pcapFile != "" {
106-
s := capture.New()
107-
conn.InstallCaptureHook(s.LogPacket)
108-
f, err := os.OpenFile(pcapFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
109-
if err != nil {
110-
return err
111-
}
112-
defer f.Close()
113-
unregister := s.RegisterOutput(f)
114-
defer unregister()
115-
}
116-
117117
var tsDir tsspeedtest.Direction
118118
switch direction {
119119
case "up":

codersdk/workspacesdk/workspacesdk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/xerrors"
1717
"nhooyr.io/websocket"
1818
"tailscale.com/tailcfg"
19+
"tailscale.com/wgengine/capture"
1920

2021
"cdr.dev/slog"
2122
"github.com/coder/coder/v2/codersdk"
@@ -176,6 +177,9 @@ type DialAgentOptions struct {
176177
// BlockEndpoints forced a direct connection through DERP. The Client may
177178
// have DisableDirect set which will override this value.
178179
BlockEndpoints bool
180+
// CaptureHook is a callback that captures Disco packets and packets sent
181+
// into the tailnet tunnel.
182+
CaptureHook capture.Callback
179183
}
180184

181185
func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *DialAgentOptions) (agentConn *AgentConn, err error) {
@@ -203,6 +207,7 @@ func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *
203207
DERPForceWebSockets: connInfo.DERPForceWebSockets,
204208
Logger: options.Logger,
205209
BlockEndpoints: c.client.DisableDirectConnections || options.BlockEndpoints,
210+
CaptureHook: options.CaptureHook,
206211
})
207212
if err != nil {
208213
return nil, xerrors.Errorf("create tailnet: %w", err)

tailnet/conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type Options struct {
9393
BlockEndpoints bool
9494
Logger slog.Logger
9595
ListenPort uint16
96+
// CaptureHook is a callback that captures Disco packets and packets sent
97+
// into the tailnet tunnel.
98+
CaptureHook capture.Callback
9699
}
97100

98101
// NodeID creates a Tailscale NodeID from the last 8 bytes of a UUID. It ensures
@@ -158,6 +161,7 @@ func NewConn(options *Options) (conn *Conn, err error) {
158161
wireguardEngine.Close()
159162
}
160163
}()
164+
wireguardEngine.InstallCaptureHook(options.CaptureHook)
161165
dialer.UseNetstackForIP = func(ip netip.Addr) bool {
162166
_, ok := wireguardEngine.PeerForIP(ip)
163167
return ok

0 commit comments

Comments
 (0)