MCPcopy Index your code
hub / github.com/coder/coder / ReconnectingPTY

Method ReconnectingPTY

codersdk/workspacesdk/agentconn.go:242–278  ·  view source on GitHub ↗

ReconnectingPTY spawns a new reconnecting terminal session. `ReconnectingPTYRequest` should be JSON marshaled and written to the returned net.Conn. Raw terminal output will be read from the returned net.Conn.

(ctx context.Context, id uuid.UUID, height, width uint16, command string, initOpts ...AgentReconnectingPTYInitOption)

Source from the content-addressed store, hash-verified

240// `ReconnectingPTYRequest` should be JSON marshaled and written to the returned net.Conn.
241// Raw terminal output will be read from the returned net.Conn.
242func (c *agentConn) ReconnectingPTY(ctx context.Context, id uuid.UUID, height, width uint16, command string, initOpts ...AgentReconnectingPTYInitOption) (net.Conn, error) {
243 ctx, span := tracing.StartSpan(ctx)
244 defer span.End()
245
246 if !c.AwaitReachable(ctx) {
247 return nil, xerrors.Errorf("workspace agent not reachable in time: %v", ctx.Err())
248 }
249
250 conn, err := c.Conn.DialContextTCP(ctx, netip.AddrPortFrom(c.agentAddress(), AgentReconnectingPTYPort))
251 if err != nil {
252 return nil, err
253 }
254 rptyInit := AgentReconnectingPTYInit{
255 ID: id,
256 Height: height,
257 Width: width,
258 Command: command,
259 }
260 for _, o := range initOpts {
261 o(&rptyInit)
262 }
263 data, err := json.Marshal(rptyInit)
264 if err != nil {
265 _ = conn.Close()
266 return nil, err
267 }
268 data = append(make([]byte, 2), data...)
269 // #nosec G115 - Safe conversion as the data length is expected to be within uint16 range for PTY initialization
270 binary.LittleEndian.PutUint16(data, uint16(len(data)-2))
271
272 _, err = conn.Write(data)
273 if err != nil {
274 _ = conn.Close()
275 return nil, err
276 }
277 return conn, nil
278}
279
280// SSH pipes the SSH protocol over the returned net.Conn.
281// This connects to the built-in SSH server in the workspace agent.

Callers

nothing calls this directly

Calls 9

AwaitReachableMethod · 0.95
agentAddressMethod · 0.95
StartSpanFunction · 0.92
ErrMethod · 0.80
DialContextTCPMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.65
ErrorfMethod · 0.45
MarshalMethod · 0.45

Tested by

no test coverage detected