Skip to content

chore: include if direct connection is over private network in ping diagnostics #15313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/cliui/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ func (d ConnDiags) splitDiagnostics() (general, client, agent []string) {
}

if d.DisableDirect {
general = append(general, "❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`")
general = append(general, "❗ Direct connections are disabled locally, by `--disable-direct-connections` or `CODER_DISABLE_DIRECT_CONNECTIONS`.\n"+
" They may still be established over a private network.")
if !d.Verbose {
return general, client, agent
}
Expand Down
30 changes: 25 additions & 5 deletions cli/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (r *RootCmd) ping() *serpent.Command {
workspaceName,
)
if err != nil {
spin.Stop()
return err
}

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

if r.disableDirect {
_, _ = fmt.Fprintln(inv.Stderr, "Direct connections disabled.")
opts.BlockEndpoints = true
}
if !r.disableNetworkTelemetry {
Expand All @@ -137,6 +137,7 @@ func (r *RootCmd) ping() *serpent.Command {
wsClient := workspacesdk.New(client)
conn, err := wsClient.DialAgent(ctx, workspaceAgent.ID, opts)
if err != nil {
spin.Stop()
return err
}
defer conn.Close()
Expand Down Expand Up @@ -168,6 +169,7 @@ func (r *RootCmd) ping() *serpent.Command {

connInfo, err := wsClient.AgentConnectionInfoGeneric(diagCtx)
if err != nil || connInfo.DERPMap == nil {
spin.Stop()
return xerrors.Errorf("Failed to retrieve connection info from server: %w\n", err)
}
connDiags.ConnInfo = connInfo
Expand Down Expand Up @@ -197,6 +199,11 @@ func (r *RootCmd) ping() *serpent.Command {
results := &pingSummary{
Workspace: workspaceName,
}
var (
pong *ipnstate.PingResult
dur time.Duration
p2p bool
)
n := 0
start := time.Now()
pingLoop:
Expand All @@ -207,7 +214,7 @@ func (r *RootCmd) ping() *serpent.Command {
n++

ctx, cancel := context.WithTimeout(ctx, pingTimeout)
dur, p2p, pong, err := conn.Ping(ctx)
dur, p2p, pong, err = conn.Ping(ctx)
cancel()
results.addResult(pong)
if err != nil {
Expand Down Expand Up @@ -275,10 +282,15 @@ func (r *RootCmd) ping() *serpent.Command {
}
}

if didP2p {
_, _ = fmt.Fprintf(inv.Stderr, "✔ You are connected directly (p2p)\n")
if p2p {
msg := "✔ You are connected directly (p2p)"
if pong != nil && isPrivateEndpoint(pong.Endpoint) {
msg += ", over a private network"
}
_, _ = fmt.Fprintln(inv.Stderr, msg)
} else {
_, _ = fmt.Fprintf(inv.Stderr, "❗ You are connected via a DERP relay, not directly (p2p)\n%s#common-problems-with-direct-connections\n", connDiags.TroubleshootingURL)
_, _ = fmt.Fprintf(inv.Stderr, "❗ You are connected via a DERP relay, not directly (p2p)\n"+
" %s#common-problems-with-direct-connections\n", connDiags.TroubleshootingURL)
}

results.Write(inv.Stdout)
Expand Down Expand Up @@ -329,3 +341,11 @@ func isAWSIP(awsRanges *cliutil.AWSIPRanges, ni *tailcfg.NetInfo) bool {
}
return false
}

func isPrivateEndpoint(endpoint string) bool {
ip, err := netip.ParseAddrPort(endpoint)
if err != nil {
return false
}
return ip.Addr().IsPrivate()
}
Loading