MCPcopy
hub / github.com/ContainerSSH/ContainerSSH / newConnection

Method newConnection

agentprotocol/server.go:453–514  ·  view source on GitHub ↗
(
	protocol string,
	connectedAddress string,
	connectedPort uint32,
	origAddress string,
	origPort uint32,
	closeFunc func() error,
)

Source from the content-addressed store, hash-verified

451}
452
453func (c *ForwardCtx) newConnection(
454 protocol string,
455 connectedAddress string,
456 connectedPort uint32,
457 origAddress string,
458 origPort uint32,
459 closeFunc func() error,
460) (io.ReadWriteCloser, error) {
461 connInfo := NewConnectionPayload{
462 Protocol: protocol,
463 ConnectedAddress: connectedAddress,
464 ConnectedPort: connectedPort,
465 OriginatorAddress: origAddress,
466 OriginatorPort: origPort,
467 }
468 marInfo, err := cbor.Marshal(&connInfo)
469 if err != nil {
470 c.logger.Error(message.Wrap(
471 err,
472 message.MSSHConnected,
473 "Error marshalling new connection payload",
474 ))
475 return nil, err
476 }
477
478 bufferReader, bufferWriter := io.Pipe()
479 conn := Connection{
480 state: CONNECTION_STATE_WAITINIT,
481 initiator: true,
482 bufferReader: bufferReader,
483 bufferWriter: bufferWriter,
484 ctx: c,
485 logger: c.logger,
486 closeCallback: closeFunc,
487 }
488 conn.stateCond = sync.NewCond(&conn.lock)
489
490 c.connMapMu.Lock()
491 c.connectionId += 1
492 conn.id = c.connectionId
493 if _, ok := c.connMap[conn.id]; ok {
494 return nil, fmt.Errorf("Connection id already exists, something went terribly wrong")
495 }
496 c.connMap[conn.id] = &conn
497 c.waitGroup.Add(1)
498 c.connMapMu.Unlock()
499 err = c.writePacket(&Packet{
500 Type: PACKET_NEW_CONNECTION,
501 ConnectionId: conn.id,
502 Payload: marInfo,
503 })
504 if err != nil {
505 c.logger.Error(message.Wrap(
506 err,
507 message.MSSHConnected,
508 "Error writing new connection packet",
509 ))
510 return nil, err

Callers 2

NewConnectionTCPMethod · 0.95
NewConnectionUnixMethod · 0.95

Calls 6

writePacketMethod · 0.95
WrapFunction · 0.92
MarshalMethod · 0.65
ErrorMethod · 0.65
ErrorfMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected