|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + |
| 10 | + "cdr.dev/slog" |
| 11 | + "github.com/hashicorp/yamux" |
| 12 | + "github.com/pion/webrtc/v3" |
| 13 | + "golang.org/x/xerrors" |
| 14 | + |
| 15 | + "cdr.dev/coder-cli/internal/x/xwebrtc" |
| 16 | + "cdr.dev/coder-cli/pkg/proto" |
| 17 | +) |
| 18 | + |
| 19 | +type stream struct { |
| 20 | + stream *yamux.Stream |
| 21 | + logger slog.Logger |
| 22 | + |
| 23 | + rtc *webrtc.PeerConnection |
| 24 | +} |
| 25 | + |
| 26 | +// writes an error and closes. |
| 27 | +func (s *stream) fatal(err error) { |
| 28 | + _ = s.write(proto.Message{ |
| 29 | + Error: err.Error(), |
| 30 | + }) |
| 31 | + s.logger.Error(context.Background(), err.Error(), slog.Error(err)) |
| 32 | + _ = s.stream.Close() |
| 33 | +} |
| 34 | + |
| 35 | +func (s *stream) listen() { |
| 36 | + decoder := json.NewDecoder(s.stream) |
| 37 | + for { |
| 38 | + var msg proto.Message |
| 39 | + err := decoder.Decode(&msg) |
| 40 | + if err == io.EOF { |
| 41 | + break |
| 42 | + } |
| 43 | + if err != nil { |
| 44 | + s.fatal(err) |
| 45 | + return |
| 46 | + } |
| 47 | + s.processMessage(msg) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (s *stream) write(msg proto.Message) error { |
| 52 | + d, err := json.Marshal(&msg) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + _, err = s.stream.Write(d) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (s *stream) processMessage(msg proto.Message) { |
| 64 | + s.logger.Debug(context.Background(), "processing message", slog.F("msg", msg)) |
| 65 | + |
| 66 | + if msg.Error != "" { |
| 67 | + s.fatal(xerrors.New(msg.Error)) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + if msg.Candidate != "" { |
| 72 | + if s.rtc == nil { |
| 73 | + s.fatal(xerrors.New("rtc connection must be started before candidates are sent")) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + s.logger.Debug(context.Background(), "accepted ice candidate", slog.F("candidate", msg.Candidate)) |
| 78 | + err := proto.AcceptICECandidate(s.rtc, &msg) |
| 79 | + if err != nil { |
| 80 | + s.fatal(err) |
| 81 | + return |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if msg.Offer != nil { |
| 86 | + rtc, err := xwebrtc.NewPeerConnection() |
| 87 | + if err != nil { |
| 88 | + s.fatal(fmt.Errorf("create connection: %w", err)) |
| 89 | + return |
| 90 | + } |
| 91 | + flushCandidates := proto.ProxyICECandidates(rtc, s.stream) |
| 92 | + |
| 93 | + err = rtc.SetRemoteDescription(*msg.Offer) |
| 94 | + if err != nil { |
| 95 | + s.fatal(fmt.Errorf("set remote desc: %w", err)) |
| 96 | + return |
| 97 | + } |
| 98 | + answer, err := rtc.CreateAnswer(nil) |
| 99 | + if err != nil { |
| 100 | + s.fatal(fmt.Errorf("create answer: %w", err)) |
| 101 | + return |
| 102 | + } |
| 103 | + err = rtc.SetLocalDescription(answer) |
| 104 | + if err != nil { |
| 105 | + s.fatal(fmt.Errorf("set local desc: %w", err)) |
| 106 | + return |
| 107 | + } |
| 108 | + flushCandidates() |
| 109 | + |
| 110 | + err = s.write(proto.Message{ |
| 111 | + Answer: rtc.LocalDescription(), |
| 112 | + }) |
| 113 | + if err != nil { |
| 114 | + s.fatal(fmt.Errorf("send local desc: %w", err)) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) { |
| 119 | + s.logger.Info(context.Background(), "state changed", slog.F("new", pcs)) |
| 120 | + }) |
| 121 | + rtc.OnDataChannel(s.processDataChannel) |
| 122 | + s.rtc = rtc |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (s *stream) processDataChannel(channel *webrtc.DataChannel) { |
| 127 | + if channel.Protocol() == "ping" { |
| 128 | + channel.OnOpen(func() { |
| 129 | + rw, err := channel.Detach() |
| 130 | + if err != nil { |
| 131 | + return |
| 132 | + } |
| 133 | + d := make([]byte, 64) |
| 134 | + _, err = rw.Read(d) |
| 135 | + if err != nil { |
| 136 | + s.logger.Error(context.Background(), "read ping", slog.Error(err)) |
| 137 | + return |
| 138 | + } |
| 139 | + _, err = rw.Write(d) |
| 140 | + if err != nil { |
| 141 | + s.logger.Error(context.Background(), "write ping", slog.Error(err)) |
| 142 | + return |
| 143 | + } |
| 144 | + }) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + prto, port, err := xwebrtc.ParseProxyDataChannel(channel) |
| 149 | + if err != nil { |
| 150 | + s.fatal(fmt.Errorf("failed to parse proxy data channel: %w", err)) |
| 151 | + return |
| 152 | + } |
| 153 | + if prto != "tcp" { |
| 154 | + s.fatal(fmt.Errorf("client provided unsupported protocol: %s", prto)) |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + conn, err := net.Dial(prto, fmt.Sprintf("localhost:%d", port)) |
| 159 | + if err != nil { |
| 160 | + s.fatal(fmt.Errorf("failed to dial client port: %d", port)) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + channel.OnOpen(func() { |
| 165 | + s.logger.Debug(context.Background(), "proxying data channel to local port", slog.F("port", port)) |
| 166 | + rw, err := channel.Detach() |
| 167 | + if err != nil { |
| 168 | + _ = channel.Close() |
| 169 | + s.logger.Error(context.Background(), "detach client data channel", slog.Error(err)) |
| 170 | + return |
| 171 | + } |
| 172 | + go func() { |
| 173 | + _, err = io.Copy(rw, conn) |
| 174 | + if err != nil { |
| 175 | + s.logger.Error(context.Background(), "copy to conn", slog.Error(err)) |
| 176 | + } |
| 177 | + }() |
| 178 | + go func() { |
| 179 | + _, _ = io.Copy(conn, rw) |
| 180 | + if err != nil { |
| 181 | + s.logger.Error(context.Background(), "copy from conn", slog.Error(err)) |
| 182 | + } |
| 183 | + }() |
| 184 | + }) |
| 185 | +} |
0 commit comments