Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitda189ae

Browse files
committed
feat(cli): add p2p diagnostics to ping
1 parentc8eacc6 commitda189ae

File tree

7 files changed

+124
-1
lines changed

7 files changed

+124
-1
lines changed

‎agent/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (a *agent) apiHandler() http.Handler {
3737
}
3838
promHandler:=PrometheusMetricsHandler(a.prometheusRegistry,a.logger)
3939
r.Get("/api/v0/listening-ports",lp.handler)
40+
r.Get("/api/v0/netcheck",a.HandleNetCheck)
4041
r.Get("/debug/logs",a.HandleHTTPDebugLogs)
4142
r.Get("/debug/magicsock",a.HandleHTTPDebugMagicsock)
4243
r.Get("/debug/magicsock/debug-logging/{state}",a.HandleHTTPMagicsockDebugLoggingState)

‎agent/health.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package agent
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/coder/coder/v2/coderd/healthcheck/health"
7+
"github.com/coder/coder/v2/coderd/httpapi"
8+
"github.com/coder/coder/v2/codersdk/healthsdk"
9+
)
10+
11+
func (a*agent)HandleNetCheck(rw http.ResponseWriter,r*http.Request) {
12+
ni:=a.TailnetConn().GetNetInfo()
13+
14+
httpapi.Write(r.Context(),rw,http.StatusOK, healthsdk.AgentNetcheckReport{
15+
BaseReport: healthsdk.BaseReport{
16+
Severity:health.SeverityOK,
17+
},
18+
NetInfo:ni,
19+
})
20+
}

‎cli/cliui/agent.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010

1111
"github.com/google/uuid"
1212
"golang.org/x/xerrors"
13+
"tailscale.com/tailcfg"
1314

1415
"github.com/coder/coder/v2/codersdk"
16+
"github.com/coder/coder/v2/codersdk/healthsdk"
17+
"github.com/coder/coder/v2/codersdk/workspacesdk"
1518
"github.com/coder/coder/v2/tailnet"
1619
)
1720

@@ -346,3 +349,50 @@ func PeerDiagnostics(w io.Writer, d tailnet.PeerDiagnostics) {
346349
_,_=fmt.Fprint(w,"✘ Wireguard is not connected\n")
347350
}
348351
}
352+
353+
typeConnDiagsstruct {
354+
Info*workspacesdk.AgentConnectionInfo
355+
PingP2Pbool
356+
LocalNetInfo*tailcfg.NetInfo
357+
FetchAgentNetInfofunc() (healthsdk.AgentNetcheckReport,error)
358+
// TODO: More diagnostics
359+
}
360+
361+
funcConnDiagnostics(w io.Writer,dConnDiags) {
362+
ifd.PingP2P {
363+
_,_=fmt.Fprintln(w,"✔ You are connected directly, peer-to-peer (p2p).")
364+
return
365+
}
366+
_,_=fmt.Fprintln(w,"❗ You are connected via a DERP relay, not directly, peer-to-peer (p2p).")
367+
368+
ifd.Info==nil {
369+
return
370+
}
371+
372+
ifd.Info.DisableDirectConnections {
373+
_,_=fmt.Fprintln(w,"❗ Your Coder administrator has blocked direct connections.")
374+
return
375+
}
376+
377+
if!d.Info.DERPMap.HasSTUN() {
378+
_,_=fmt.Fprintln(w,"✘ The workspace agent appears to be unable to reach any STUN servers.\nhttps://coder.com/docs/networking/stun")
379+
}
380+
381+
ifd.LocalNetInfo==nil {
382+
return
383+
}
384+
385+
ifd.LocalNetInfo.MappingVariesByDestIP.EqualBool(true) {
386+
_,_=fmt.Fprintln(w,"❗ Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers.")
387+
}
388+
389+
agentNetcheck,err:=d.FetchAgentNetInfo()
390+
iferr!=nil {
391+
_,_=fmt.Fprintf(w,"Failed to retrieve netcheck report from agent: %v\n",err)
392+
return
393+
}
394+
395+
ifagentNetcheck.NetInfo.MappingVariesByDestIP.EqualBool(true) {
396+
_,_=fmt.Fprintln(w,"❗ Agent is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers.")
397+
}
398+
}

‎cli/ping.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/coder/coder/v2/cli/cliui"
1616
"github.com/coder/coder/v2/codersdk"
17+
"github.com/coder/coder/v2/codersdk/healthsdk"
1718
"github.com/coder/coder/v2/codersdk/workspacesdk"
1819
"github.com/coder/serpent"
1920
)
@@ -140,6 +141,18 @@ func (r *RootCmd) ping() *serpent.Command {
140141
ifn==int(pingNum) {
141142
diags:=conn.GetPeerDiagnostics()
142143
cliui.PeerDiagnostics(inv.Stdout,diags)
144+
145+
connDiags:= cliui.ConnDiags{
146+
PingP2P:didP2p,
147+
FetchAgentNetInfo:func() (healthsdk.AgentNetcheckReport,error) {
148+
returnconn.NetCheck(ctx)
149+
},
150+
}
151+
connInfo,err:=workspacesdk.New(client).AgentConnectionInfoGeneric(ctx)
152+
iferr==nil {
153+
connDiags.Info=&connInfo
154+
}
155+
cliui.ConnDiagnostics(inv.Stdout,connDiags)
143156
returnnil
144157
}
145158
}

‎codersdk/healthsdk/healthsdk.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,9 @@ type ClientNetcheckReport struct {
273273
DERPDERPHealthReport`json:"derp"`
274274
InterfacesInterfacesReport`json:"interfaces"`
275275
}
276+
277+
// @typescript-ignore AgentNetcheckReport
278+
typeAgentNetcheckReportstruct {
279+
BaseReport
280+
NetInfo*tailcfg.NetInfo`json:"net_info"`
281+
}

‎codersdk/workspacesdk/agentconn.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/coder/coder/v2/coderd/tracing"
2424
"github.com/coder/coder/v2/codersdk"
25+
"github.com/coder/coder/v2/codersdk/healthsdk"
2526
"github.com/coder/coder/v2/tailnet"
2627
)
2728

@@ -241,6 +242,23 @@ func (c *AgentConn) ListeningPorts(ctx context.Context) (codersdk.WorkspaceAgent
241242
returnresp,json.NewDecoder(res.Body).Decode(&resp)
242243
}
243244

245+
// NetCheck returns a network check report from the workspace agent.
246+
func (c*AgentConn)NetCheck(ctx context.Context) (healthsdk.AgentNetcheckReport,error) {
247+
ctx,span:=tracing.StartSpan(ctx)
248+
deferspan.End()
249+
res,err:=c.apiRequest(ctx,http.MethodGet,"/api/v0/netcheck",nil)
250+
iferr!=nil {
251+
return healthsdk.AgentNetcheckReport{},xerrors.Errorf("do request: %w",err)
252+
}
253+
deferres.Body.Close()
254+
ifres.StatusCode!=http.StatusOK {
255+
return healthsdk.AgentNetcheckReport{},codersdk.ReadBodyAsError(res)
256+
}
257+
258+
varresp healthsdk.AgentNetcheckReport
259+
returnresp,json.NewDecoder(res.Body).Decode(&resp)
260+
}
261+
244262
// DebugMagicsock makes a request to the workspace agent's magicsock debug endpoint.
245263
func (c*AgentConn)DebugMagicsock(ctx context.Context) ([]byte,error) {
246264
ctx,span:=tracing.StartSpan(ctx)

‎tailnet/conn.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ func NewConn(options *Options) (conn *Conn, err error) {
294294
}()
295295
ifserver.telemetryStore!=nil {
296296
server.wireguardEngine.SetNetInfoCallback(func(ni*tailcfg.NetInfo) {
297+
server.mutex.Lock()
298+
deferserver.mutex.Unlock()
299+
server.lastNetInfo=ni.Clone()
297300
server.telemetryStore.setNetInfo(ni)
298301
nodeUp.setNetInfo(ni)
299302
server.telemetryStore.pingPeer(server)
@@ -304,7 +307,12 @@ func NewConn(options *Options) (conn *Conn, err error) {
304307
})
305308
goserver.watchConnChange()
306309
}else {
307-
server.wireguardEngine.SetNetInfoCallback(nodeUp.setNetInfo)
310+
server.wireguardEngine.SetNetInfoCallback(func(ni*tailcfg.NetInfo) {
311+
server.mutex.Lock()
312+
deferserver.mutex.Unlock()
313+
server.lastNetInfo=ni.Clone()
314+
nodeUp.setNetInfo(ni)
315+
})
308316
}
309317
server.wireguardEngine.SetStatusCallback(nodeUp.setStatus)
310318
server.magicConn.SetDERPForcedWebsocketCallback(nodeUp.setDERPForcedWebsocket)
@@ -373,6 +381,13 @@ type Conn struct {
373381
watchCancelfunc()
374382

375383
trafficStats*connstats.Statistics
384+
lastNetInfo*tailcfg.NetInfo
385+
}
386+
387+
func (c*Conn)GetNetInfo()*tailcfg.NetInfo {
388+
c.mutex.Lock()
389+
deferc.mutex.Unlock()
390+
returnc.lastNetInfo.Clone()
376391
}
377392

378393
func (c*Conn)SetTunnelDestination(id uuid.UUID) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp