Skip to content

Commit 6117f46

Browse files
chore: include if direct connection is over private network in ping diagnostics (coder#15313)
Whilst the `networking-troubleshooting` docs page already mentions that a direct connection can be established over a private network, even if there are no STUN servers, it's worth this is the case at the end of the ping output. This also removes a print statement that was dirtying up the diagnostic output, and corrects the name of the `--disable-direct-connections` flag.
1 parent c519a12 commit 6117f46

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

cli/cliui/agent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ func (d ConnDiags) splitDiagnostics() (general, client, agent []string) {
411411
}
412412

413413
if d.DisableDirect {
414-
general = append(general, "❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`")
414+
general = append(general, "❗ Direct connections are disabled locally, by `--disable-direct-connections` or `CODER_DISABLE_DIRECT_CONNECTIONS`.\n"+
415+
" They may still be established over a private network.")
415416
if !d.Verbose {
416417
return general, client, agent
417418
}

cli/ping.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (r *RootCmd) ping() *serpent.Command {
118118
workspaceName,
119119
)
120120
if err != nil {
121+
spin.Stop()
121122
return err
122123
}
123124

@@ -128,7 +129,6 @@ func (r *RootCmd) ping() *serpent.Command {
128129
}
129130

130131
if r.disableDirect {
131-
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
132132
opts.BlockEndpoints = true
133133
}
134134
if !r.disableNetworkTelemetry {
@@ -137,6 +137,7 @@ func (r *RootCmd) ping() *serpent.Command {
137137
wsClient := workspacesdk.New(client)
138138
conn, err := wsClient.DialAgent(ctx, workspaceAgent.ID, opts)
139139
if err != nil {
140+
spin.Stop()
140141
return err
141142
}
142143
defer conn.Close()
@@ -168,6 +169,7 @@ func (r *RootCmd) ping() *serpent.Command {
168169

169170
connInfo, err := wsClient.AgentConnectionInfoGeneric(diagCtx)
170171
if err != nil || connInfo.DERPMap == nil {
172+
spin.Stop()
171173
return xerrors.Errorf("Failed to retrieve connection info from server: %w\n", err)
172174
}
173175
connDiags.ConnInfo = connInfo
@@ -197,6 +199,11 @@ func (r *RootCmd) ping() *serpent.Command {
197199
results := &pingSummary{
198200
Workspace: workspaceName,
199201
}
202+
var (
203+
pong *ipnstate.PingResult
204+
dur time.Duration
205+
p2p bool
206+
)
200207
n := 0
201208
start := time.Now()
202209
pingLoop:
@@ -207,7 +214,7 @@ func (r *RootCmd) ping() *serpent.Command {
207214
n++
208215

209216
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
210-
dur, p2p, pong, err := conn.Ping(ctx)
217+
dur, p2p, pong, err = conn.Ping(ctx)
211218
cancel()
212219
results.addResult(pong)
213220
if err != nil {
@@ -275,10 +282,15 @@ func (r *RootCmd) ping() *serpent.Command {
275282
}
276283
}
277284

278-
if didP2p {
279-
_, _ = fmt.Fprintf(inv.Stderr, "✔ You are connected directly (p2p)\n")
285+
if p2p {
286+
msg := "✔ You are connected directly (p2p)"
287+
if pong != nil && isPrivateEndpoint(pong.Endpoint) {
288+
msg += ", over a private network"
289+
}
290+
_, _ = fmt.Fprintln(inv.Stderr, msg)
280291
} else {
281-
_, _ = fmt.Fprintf(inv.Stderr, "❗ You are connected via a DERP relay, not directly (p2p)\n%s#common-problems-with-direct-connections\n", connDiags.TroubleshootingURL)
292+
_, _ = fmt.Fprintf(inv.Stderr, "❗ You are connected via a DERP relay, not directly (p2p)\n"+
293+
" %s#common-problems-with-direct-connections\n", connDiags.TroubleshootingURL)
282294
}
283295

284296
results.Write(inv.Stdout)
@@ -329,3 +341,11 @@ func isAWSIP(awsRanges *cliutil.AWSIPRanges, ni *tailcfg.NetInfo) bool {
329341
}
330342
return false
331343
}
344+
345+
func isPrivateEndpoint(endpoint string) bool {
346+
ip, err := netip.ParseAddrPort(endpoint)
347+
if err != nil {
348+
return false
349+
}
350+
return ip.Addr().IsPrivate()
351+
}

0 commit comments

Comments
 (0)