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

Commitcf5bc72

Browse files
authored
Merge branch 'main' into restructure-new
2 parents4059f9f +13e5c51 commitcf5bc72

File tree

88 files changed

+1911
-920
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1911
-920
lines changed

‎cli/agent.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
5050
slogJSONPathstring
5151
slogStackdriverPathstring
5252
blockFileTransferbool
53+
agentHeaderCommandstring
54+
agentHeader []string
5355
)
5456
cmd:=&serpent.Command{
5557
Use:"agent",
@@ -176,6 +178,14 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
176178
// with large payloads can take a bit. e.g. startup scripts
177179
// may take a while to insert.
178180
client.SDK.HTTPClient.Timeout=30*time.Second
181+
// Attach header transport so we process --agent-header and
182+
// --agent-header-command flags
183+
headerTransport,err:=headerTransport(ctx,r.agentURL,agentHeader,agentHeaderCommand)
184+
iferr!=nil {
185+
returnxerrors.Errorf("configure header transport: %w",err)
186+
}
187+
headerTransport.Transport=client.SDK.HTTPClient.Transport
188+
client.SDK.HTTPClient.Transport=headerTransport
179189

180190
// Enable pprof handler
181191
// This prevents the pprof import from being accidentally deleted.
@@ -361,6 +371,18 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
361371
Value:serpent.StringOf(&pprofAddress),
362372
Description:"The address to serve pprof.",
363373
},
374+
{
375+
Flag:"agent-header-command",
376+
Env:"CODER_AGENT_HEADER_COMMAND",
377+
Value:serpent.StringOf(&agentHeaderCommand),
378+
Description:"An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.",
379+
},
380+
{
381+
Flag:"agent-header",
382+
Env:"CODER_AGENT_HEADER",
383+
Value:serpent.StringArrayOf(&agentHeader),
384+
Description:"Additional HTTP headers added to all requests. Provide as "+`key=value`+". Can be specified multiple times.",
385+
},
364386
{
365387
Flag:"no-reap",
366388

‎cli/agent_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package cli_test
33
import (
44
"context"
55
"fmt"
6+
"net/http"
7+
"net/http/httptest"
68
"os"
79
"path/filepath"
810
"runtime"
911
"strings"
12+
"sync/atomic"
1013
"testing"
1114

1215
"github.com/google/uuid"
@@ -229,6 +232,43 @@ func TestWorkspaceAgent(t *testing.T) {
229232
require.Equal(t,codersdk.AgentSubsystemEnvbox,resources[0].Agents[0].Subsystems[0])
230233
require.Equal(t,codersdk.AgentSubsystemExectrace,resources[0].Agents[0].Subsystems[1])
231234
})
235+
t.Run("Header",func(t*testing.T) {
236+
t.Parallel()
237+
238+
varurlstring
239+
varcalledint64
240+
srv:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
241+
assert.Equal(t,"wow",r.Header.Get("X-Testing"))
242+
assert.Equal(t,"Ethan was Here!",r.Header.Get("Cool-Header"))
243+
assert.Equal(t,"very-wow-"+url,r.Header.Get("X-Process-Testing"))
244+
assert.Equal(t,"more-wow",r.Header.Get("X-Process-Testing2"))
245+
atomic.AddInt64(&called,1)
246+
w.WriteHeader(http.StatusGone)
247+
}))
248+
defersrv.Close()
249+
url=srv.URL
250+
coderURLEnv:="$CODER_URL"
251+
ifruntime.GOOS=="windows" {
252+
coderURLEnv="%CODER_URL%"
253+
}
254+
255+
logDir:=t.TempDir()
256+
inv,_:=clitest.New(t,
257+
"agent",
258+
"--auth","token",
259+
"--agent-token","fake-token",
260+
"--agent-url",srv.URL,
261+
"--log-dir",logDir,
262+
"--agent-header","X-Testing=wow",
263+
"--agent-header","Cool-Header=Ethan was Here!",
264+
"--agent-header-command","printf X-Process-Testing=very-wow-"+coderURLEnv+"'\\r\\n'X-Process-Testing2=more-wow",
265+
)
266+
267+
clitest.Start(t,inv)
268+
require.Eventually(t,func()bool {
269+
returnatomic.LoadInt64(&called)>0
270+
},testutil.WaitShort,testutil.IntervalFast)
271+
})
232272
}
233273

234274
funcmatchAgentWithVersion(rs []codersdk.WorkspaceResource)bool {

‎cli/cliui/agent.go

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -351,53 +351,100 @@ func PeerDiagnostics(w io.Writer, d tailnet.PeerDiagnostics) {
351351
}
352352

353353
typeConnDiagsstruct {
354-
ConnInfo*workspacesdk.AgentConnectionInfo
354+
ConnInfo workspacesdk.AgentConnectionInfo
355355
PingP2Pbool
356356
DisableDirectbool
357357
LocalNetInfo*tailcfg.NetInfo
358358
LocalInterfaces*healthsdk.InterfacesReport
359359
AgentNetcheck*healthsdk.AgentNetcheckReport
360+
ClientIPIsAWSbool
361+
AgentIPIsAWSbool
362+
Verbosebool
360363
// TODO: More diagnostics
361364
}
362365

363-
funcConnDiagnostics(w io.Writer,dConnDiags) {
366+
func (dConnDiags)Write(w io.Writer) {
367+
_,_=fmt.Fprintln(w,"")
368+
general,client,agent:=d.splitDiagnostics()
369+
for_,msg:=rangegeneral {
370+
_,_=fmt.Fprintln(w,msg)
371+
}
372+
iflen(client)>0 {
373+
_,_=fmt.Fprint(w,"Possible client-side issues with direct connection:\n\n")
374+
for_,msg:=rangeclient {
375+
_,_=fmt.Fprintf(w," - %s\n\n",msg)
376+
}
377+
}
378+
iflen(agent)>0 {
379+
_,_=fmt.Fprint(w,"Possible agent-side issues with direct connections:\n\n")
380+
for_,msg:=rangeagent {
381+
_,_=fmt.Fprintf(w," - %s\n\n",msg)
382+
}
383+
}
384+
}
385+
386+
func (dConnDiags)splitDiagnostics() (general,client,agent []string) {
387+
ifd.PingP2P {
388+
general=append(general,"✔ You are connected directly (p2p)")
389+
}else {
390+
general=append(general,"❗ You are connected via a DERP relay, not directly (p2p)")
391+
}
392+
364393
ifd.AgentNetcheck!=nil {
365394
for_,msg:=ranged.AgentNetcheck.Interfaces.Warnings {
366-
_,_=fmt.Fprintf(w,"❗ Agent: %s\n",msg.Message)
395+
agent=append(agent,msg.Message)
367396
}
368397
}
369398

370399
ifd.LocalInterfaces!=nil {
371400
for_,msg:=ranged.LocalInterfaces.Warnings {
372-
_,_=fmt.Fprintf(w,"❗ Client: %s\n",msg.Message)
401+
client=append(client,msg.Message)
373402
}
374403
}
375404

376-
ifd.PingP2P {
377-
_,_=fmt.Fprint(w,"✔ You are connected directly (p2p)\n")
378-
return
405+
ifd.PingP2P&&!d.Verbose {
406+
returngeneral,client,agent
379407
}
380-
_,_=fmt.Fprint(w,"❗ You are connected via a DERP relay, not directly (p2p)\n")
381408

382409
ifd.DisableDirect {
383-
_,_=fmt.Fprint(w,"❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`\n")
384-
return
410+
general=append(general,"❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`")
411+
if!d.Verbose {
412+
returngeneral,client,agent
413+
}
385414
}
386415

387-
ifd.ConnInfo!=nil&&d.ConnInfo.DisableDirectConnections {
388-
_,_=fmt.Fprint(w,"❗ Your Coder administrator has blocked direct connections\n")
389-
return
416+
ifd.ConnInfo.DisableDirectConnections {
417+
general=append(general,"❗ Your Coder administrator has blocked direct connections")
418+
if!d.Verbose {
419+
returngeneral,client,agent
420+
}
390421
}
391422

392-
ifd.ConnInfo!=nil&&d.ConnInfo.DERPMap!=nil&&!d.ConnInfo.DERPMap.HasSTUN() {
393-
_,_=fmt.Fprint(w,"✘ The DERP map is not configured to use STUN, which will prevent direct connections from starting outside of local networks\n")
423+
if!d.ConnInfo.DERPMap.HasSTUN() {
424+
general=append(general,"The DERP map is not configured to use STUN")
425+
}elseifd.LocalNetInfo!=nil&&!d.LocalNetInfo.UDP {
426+
client=append(client,"Client could not connect to STUN over UDP")
394427
}
395428

396429
ifd.LocalNetInfo!=nil&&d.LocalNetInfo.MappingVariesByDestIP.EqualBool(true) {
397-
_,_=fmt.Fprint(w,"❗ Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers\n")
430+
client=append(client,"Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers")
431+
}
432+
433+
ifd.AgentNetcheck!=nil&&d.AgentNetcheck.NetInfo!=nil {
434+
ifd.AgentNetcheck.NetInfo.MappingVariesByDestIP.EqualBool(true) {
435+
agent=append(agent,"Agent is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers")
436+
}
437+
if!d.AgentNetcheck.NetInfo.UDP {
438+
agent=append(agent,"Agent could not connect to STUN over UDP")
439+
}
440+
}
441+
442+
ifd.ClientIPIsAWS {
443+
client=append(client,"Client IP address is within an AWS range (AWS uses hard NAT)")
398444
}
399445

400-
ifd.AgentNetcheck!=nil&&d.AgentNetcheck.NetInfo!=nil&&d.AgentNetcheck.NetInfo.MappingVariesByDestIP.EqualBool(true) {
401-
_,_=fmt.Fprint(w,"Agentis potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers\n")
446+
ifd.AgentIPIsAWS {
447+
agent=append(agent,"AgentIP address is within an AWS range (AWS uses hard NAT)")
402448
}
449+
returngeneral,client,agent
403450
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp