@@ -27,10 +27,13 @@ import (
2727 "go.uber.org/atomic"
2828 gossh "golang.org/x/crypto/ssh"
2929 "golang.org/x/xerrors"
30+ "inet.af/netaddr"
31+ "tailscale.com/types/key"
3032
3133 "cdr.dev/slog"
3234 "github.com/coder/coder/agent/usershell"
3335 "github.com/coder/coder/peer"
36+ "github.com/coder/coder/peer/peerwg"
3437 "github.com/coder/coder/peerbroker"
3538 "github.com/coder/coder/pty"
3639 "github.com/coder/retry"
@@ -43,20 +46,31 @@ const (
4346)
4447
4548type Options struct {
49+ EnableWireguard bool
50+ UploadWireguardKeys UploadWireguardKeys
51+ ListenWireguardPeers ListenWireguardPeers
4652 ReconnectingPTYTimeout time.Duration
4753 EnvironmentVariables map [string ]string
4854 Logger slog.Logger
4955}
5056
5157type Metadata struct {
52- OwnerEmail string `json:"owner_email"`
53- OwnerUsername string `json:"owner_username"`
54- EnvironmentVariables map [string ]string `json:"environment_variables"`
55- StartupScript string `json:"startup_script"`
56- Directory string `json:"directory"`
58+ WireguardAddresses []netaddr.IPPrefix `json:"addresses"`
59+ OwnerEmail string `json:"owner_email"`
60+ OwnerUsername string `json:"owner_username"`
61+ EnvironmentVariables map [string ]string `json:"environment_variables"`
62+ StartupScript string `json:"startup_script"`
63+ Directory string `json:"directory"`
64+ }
65+
66+ type WireguardPublicKeys struct {
67+ Public key.NodePublic `json:"public"`
68+ Disco key.DiscoPublic `json:"disco"`
5769}
5870
5971type Dialer func (ctx context.Context , logger slog.Logger ) (Metadata , * peerbroker.Listener , error )
72+ type UploadWireguardKeys func (ctx context.Context , keys WireguardPublicKeys ) error
73+ type ListenWireguardPeers func (ctx context.Context , logger slog.Logger ) (<- chan peerwg.Handshake , func (), error )
6074
6175func New (dialer Dialer , options * Options ) io.Closer {
6276 if options == nil {
@@ -73,6 +87,9 @@ func New(dialer Dialer, options *Options) io.Closer {
7387 closeCancel : cancelFunc ,
7488 closed : make (chan struct {}),
7589 envVars : options .EnvironmentVariables ,
90+ enableWireguard : options .EnableWireguard ,
91+ postKeys : options .UploadWireguardKeys ,
92+ listenWireguardPeers : options .ListenWireguardPeers ,
7693 }
7794 server .init (ctx )
7895 return server
@@ -95,6 +112,11 @@ type agent struct {
95112 metadata atomic.Value
96113 startupScript atomic.Bool
97114 sshServer * ssh.Server
115+
116+ enableWireguard bool
117+ network * peerwg.Network
118+ postKeys UploadWireguardKeys
119+ listenWireguardPeers ListenWireguardPeers
98120}
99121
100122func (a * agent ) run (ctx context.Context ) {
@@ -138,6 +160,13 @@ func (a *agent) run(ctx context.Context) {
138160 }()
139161 }
140162
163+ if a .enableWireguard {
164+ err = a .startWireguard (ctx , metadata .WireguardAddresses )
165+ if err != nil {
166+ a .logger .Error (ctx , "start wireguard" , slog .Error (err ))
167+ }
168+ }
169+
141170 for {
142171 conn , err := peerListener .Accept ()
143172 if err != nil {
@@ -366,17 +395,17 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
366395
367396 // Load environment variables passed via the agent.
368397 // These should override all variables we manually specify.
369- for key , value := range metadata .EnvironmentVariables {
398+ for envKey , value := range metadata .EnvironmentVariables {
370399 // Expanding environment variables allows for customization
371400 // of the $PATH, among other variables. Customers can prepand
372401 // or append to the $PATH, so allowing expand is required!
373- cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , key , os .ExpandEnv (value )))
402+ cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , envKey , os .ExpandEnv (value )))
374403 }
375404
376405 // Agent-level environment variables should take over all!
377406 // This is used for setting agent-specific variables like "CODER_AGENT_TOKEN".
378- for key , value := range a .envVars {
379- cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , key , value ))
407+ for envKey , value := range a .envVars {
408+ cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , envKey , value ))
380409 }
381410
382411 return cmd , nil
0 commit comments