|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "cdr.dev/slog" |
| 15 | + "cdr.dev/slog/sloggers/sloghuman" |
| 16 | + "github.com/hashicorp/yamux" |
| 17 | + "github.com/pion/webrtc/v3" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + "golang.org/x/xerrors" |
| 20 | + "nhooyr.io/websocket" |
| 21 | + |
| 22 | + "cdr.dev/coder-cli/internal/x/xcobra" |
| 23 | + "cdr.dev/coder-cli/internal/x/xwebrtc" |
| 24 | + "cdr.dev/coder-cli/pkg/proto" |
| 25 | +) |
| 26 | + |
| 27 | +func agentCmd() *cobra.Command { |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "agent", |
| 30 | + Short: "Run the workspace agent", |
| 31 | + Long: "Connect to Coder and start running a p2p agent", |
| 32 | + Hidden: true, |
| 33 | + } |
| 34 | + |
| 35 | + cmd.AddCommand( |
| 36 | + startCmd(), |
| 37 | + ) |
| 38 | + return cmd |
| 39 | +} |
| 40 | + |
| 41 | +func startCmd() *cobra.Command { |
| 42 | + var ( |
| 43 | + token string |
| 44 | + ) |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "start [coderURL] --token=[token]", |
| 47 | + Args: xcobra.ExactArgs(1), |
| 48 | + Short: "starts the coder agent", |
| 49 | + Long: "starts the coder agent", |
| 50 | + Example: `# start the agent and connect with a Coder agent token |
| 51 | +
|
| 52 | +coder agent start https://my-coder.com --token xxxx-xxxx |
| 53 | +
|
| 54 | +# start the agent and use CODER_AGENT_TOKEN env var for auth token |
| 55 | +
|
| 56 | +coder agent start https://my-coder.com |
| 57 | +`, |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + ctx := cmd.Context() |
| 60 | + log := slog.Make(sloghuman.Sink(cmd.OutOrStdout())) |
| 61 | + |
| 62 | + // Pull the URL from the args and do some sanity check. |
| 63 | + rawURL := args[0] |
| 64 | + if rawURL == "" || !strings.HasPrefix(rawURL, "http") { |
| 65 | + return xerrors.Errorf("invalid URL") |
| 66 | + } |
| 67 | + u, err := url.Parse(rawURL) |
| 68 | + if err != nil { |
| 69 | + return xerrors.Errorf("parse url: %w", err) |
| 70 | + } |
| 71 | + // Remove the trailing '/' if any. |
| 72 | + u.Path = "/api/private/envagent/listen" |
| 73 | + |
| 74 | + if token == "" { |
| 75 | + var ok bool |
| 76 | + token, ok = os.LookupEnv("CODER_AGENT_TOKEN") |
| 77 | + if !ok { |
| 78 | + return xerrors.New("must pass --token or set the CODER_AGENT_TOKEN env variable") |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + q := u.Query() |
| 83 | + q.Set("service_token", token) |
| 84 | + u.RawQuery = q.Encode() |
| 85 | + |
| 86 | + ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15) |
| 87 | + defer cancelFunc() |
| 88 | + log.Info(ctx, "connecting to broker", slog.F("url", u.String())) |
| 89 | + conn, res, err := websocket.Dial(ctx, u.String(), nil) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("dial: %w", err) |
| 92 | + } |
| 93 | + _ = res.Body.Close() |
| 94 | + nc := websocket.NetConn(context.Background(), conn, websocket.MessageBinary) |
| 95 | + session, err := yamux.Server(nc, nil) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("open: %w", err) |
| 98 | + } |
| 99 | + log.Info(ctx, "connected to broker. awaiting connection requests") |
| 100 | + for { |
| 101 | + st, err := session.AcceptStream() |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("accept stream: %w", err) |
| 104 | + } |
| 105 | + stream := &stream{ |
| 106 | + logger: log.Named(fmt.Sprintf("stream %d", st.StreamID())), |
| 107 | + stream: st, |
| 108 | + } |
| 109 | + go stream.listen() |
| 110 | + } |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + cmd.Flags().StringVar(&token, "token", "", "coder agent token") |
| 115 | + return cmd |
| 116 | +} |
| 117 | + |
| 118 | +type stream struct { |
| 119 | + stream *yamux.Stream |
| 120 | + logger slog.Logger |
| 121 | + |
| 122 | + rtc *webrtc.PeerConnection |
| 123 | +} |
| 124 | + |
| 125 | +// writes an error and closes. |
| 126 | +func (s *stream) fatal(err error) { |
| 127 | + _ = s.write(proto.Message{ |
| 128 | + Error: err.Error(), |
| 129 | + }) |
| 130 | + s.logger.Error(context.Background(), err.Error(), slog.Error(err)) |
| 131 | + _ = s.stream.Close() |
| 132 | +} |
| 133 | + |
| 134 | +func (s *stream) listen() { |
| 135 | + decoder := json.NewDecoder(s.stream) |
| 136 | + for { |
| 137 | + var msg proto.Message |
| 138 | + err := decoder.Decode(&msg) |
| 139 | + if err == io.EOF { |
| 140 | + break |
| 141 | + } |
| 142 | + if err != nil { |
| 143 | + s.fatal(err) |
| 144 | + return |
| 145 | + } |
| 146 | + s.processMessage(msg) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func (s *stream) write(msg proto.Message) error { |
| 151 | + d, err := json.Marshal(&msg) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + _, err = s.stream.Write(d) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (s *stream) processMessage(msg proto.Message) { |
| 163 | + s.logger.Debug(context.Background(), "processing message", slog.F("msg", msg)) |
| 164 | + |
| 165 | + if msg.Error != "" { |
| 166 | + s.fatal(xerrors.New(msg.Error)) |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + if msg.Candidate != "" { |
| 171 | + if s.rtc == nil { |
| 172 | + s.fatal(xerrors.New("rtc connection must be started before candidates are sent")) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + s.logger.Debug(context.Background(), "accepted ice candidate", slog.F("candidate", msg.Candidate)) |
| 177 | + err := proto.AcceptICECandidate(s.rtc, &msg) |
| 178 | + if err != nil { |
| 179 | + s.fatal(err) |
| 180 | + return |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if msg.Offer != nil { |
| 185 | + rtc, err := xwebrtc.NewPeerConnection() |
| 186 | + if err != nil { |
| 187 | + s.fatal(fmt.Errorf("create connection: %w", err)) |
| 188 | + return |
| 189 | + } |
| 190 | + flushCandidates := proto.ProxyICECandidates(rtc, s.stream) |
| 191 | + |
| 192 | + err = rtc.SetRemoteDescription(*msg.Offer) |
| 193 | + if err != nil { |
| 194 | + s.fatal(fmt.Errorf("set remote desc: %w", err)) |
| 195 | + return |
| 196 | + } |
| 197 | + answer, err := rtc.CreateAnswer(nil) |
| 198 | + if err != nil { |
| 199 | + s.fatal(fmt.Errorf("create answer: %w", err)) |
| 200 | + return |
| 201 | + } |
| 202 | + err = rtc.SetLocalDescription(answer) |
| 203 | + if err != nil { |
| 204 | + s.fatal(fmt.Errorf("set local desc: %w", err)) |
| 205 | + return |
| 206 | + } |
| 207 | + flushCandidates() |
| 208 | + |
| 209 | + err = s.write(proto.Message{ |
| 210 | + Answer: rtc.LocalDescription(), |
| 211 | + }) |
| 212 | + if err != nil { |
| 213 | + s.fatal(fmt.Errorf("send local desc: %w", err)) |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) { |
| 218 | + s.logger.Info(context.Background(), "state changed", slog.F("new", pcs)) |
| 219 | + }) |
| 220 | + rtc.OnDataChannel(s.processDataChannel) |
| 221 | + s.rtc = rtc |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +func (s *stream) processDataChannel(channel *webrtc.DataChannel) { |
| 226 | + if channel.Protocol() == "ping" { |
| 227 | + channel.OnOpen(func() { |
| 228 | + rw, err := channel.Detach() |
| 229 | + if err != nil { |
| 230 | + return |
| 231 | + } |
| 232 | + d := make([]byte, 64) |
| 233 | + _, _ = rw.Read(d) |
| 234 | + _, _ = rw.Write(d) |
| 235 | + }) |
| 236 | + return |
| 237 | + } |
| 238 | + |
| 239 | + prto, port, err := xwebrtc.ParseProxyDataChannel(channel) |
| 240 | + if err != nil { |
| 241 | + s.fatal(fmt.Errorf("failed to parse proxy data channel: %w", err)) |
| 242 | + return |
| 243 | + } |
| 244 | + if prto != "tcp" { |
| 245 | + s.fatal(fmt.Errorf("client provided unsupported protocol: %s", prto)) |
| 246 | + return |
| 247 | + } |
| 248 | + |
| 249 | + conn, err := net.Dial(prto, fmt.Sprintf("localhost:%d", port)) |
| 250 | + if err != nil { |
| 251 | + s.fatal(fmt.Errorf("failed to dial client port: %d", port)) |
| 252 | + return |
| 253 | + } |
| 254 | + |
| 255 | + channel.OnOpen(func() { |
| 256 | + s.logger.Debug(context.Background(), "proxying data channel to local port", slog.F("port", port)) |
| 257 | + rw, err := channel.Detach() |
| 258 | + if err != nil { |
| 259 | + _ = channel.Close() |
| 260 | + s.logger.Error(context.Background(), "detach client data channel", slog.Error(err)) |
| 261 | + return |
| 262 | + } |
| 263 | + go func() { |
| 264 | + _, _ = io.Copy(rw, conn) |
| 265 | + }() |
| 266 | + go func() { |
| 267 | + _, _ = io.Copy(conn, rw) |
| 268 | + }() |
| 269 | + }) |
| 270 | +} |
0 commit comments