MCPcopy
hub / github.com/keploy/keploy / PassThrough

Function PassThrough

pkg/agent/proxy/util/util.go:642–732  ·  view source on GitHub ↗

PassThrough function is used to pass the network traffic to the destination connection. It also closes the destination connection if the function returns an error.

(ctx context.Context, logger *zap.Logger, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, requestBuffer [][]byte)

Source from the content-addressed store, hash-verified

640// PassThrough function is used to pass the network traffic to the destination connection.
641// It also closes the destination connection if the function returns an error.
642func PassThrough(ctx context.Context, logger *zap.Logger, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, requestBuffer [][]byte) ([]byte, error) {
643 logger.Debug("passing through the network traffic to the destination server", zap.Any("Destination Addr", dstCfg.Addr))
644 // making destConn
645 var destConn net.Conn
646 var err error
647 if dstCfg.TLSCfg != nil {
648 logger.Debug("trying to establish a TLS connection with the destination server", zap.Any("Destination Addr", dstCfg.Addr))
649
650 destConn, err = tls.Dial("tcp", dstCfg.Addr, dstCfg.TLSCfg)
651 if err != nil {
652 utils.LogError(logger, err, "failed to dial the conn to destination server", zap.Any("server address", dstCfg.Addr), zap.String("next_step", NextStepDialDestination))
653 return nil, err
654 }
655 logger.Debug("TLS connection established with the destination server", zap.Any("Destination Addr", destConn.RemoteAddr().String()))
656 } else {
657 logger.Debug("trying to establish a connection with the destination server", zap.Any("Destination Addr", dstCfg.Addr))
658 destConn, err = net.Dial("tcp", dstCfg.Addr)
659 if err != nil {
660 utils.LogError(logger, err, "failed to dial the conn to destination server", zap.Any("server address", dstCfg.Addr), zap.String("next_step", NextStepDialDestination))
661 return nil, err
662 }
663 logger.Debug("connection established with the destination server", zap.Any("Destination Addr", destConn.RemoteAddr().String()))
664 }
665
666 logger.Debug("trying to forward requests to target", zap.Any("Destination Addr", destConn.RemoteAddr().String()))
667 for _, v := range requestBuffer {
668 _, err := destConn.Write(v)
669 if err != nil {
670 utils.LogError(logger, err, "failed to write request message to the destination server", zap.Any("Destination Addr", destConn.RemoteAddr().String()))
671 return nil, err
672 }
673 }
674
675 destBufferChannel := make(chan []byte)
676 clientBufferChannel := make(chan []byte)
677 errChannel := make(chan error, 2)
678 passthroughContext, cancel := context.WithCancel(ctx)
679 defer cancel()
680
681 go func() {
682 defer Recover(logger, clientConn, nil)
683 defer close(destBufferChannel)
684 defer func(destConn net.Conn) {
685 err := destConn.Close()
686 if err != nil {
687 utils.LogError(logger, err, "failed to close the destination connection")
688 }
689 }(destConn)
690
691 ReadBuffConn(passthroughContext, logger, destConn, destBufferChannel, errChannel, false)
692 }()
693
694 go func() {
695 defer Recover(logger, clientConn, nil)
696 defer close(clientBufferChannel)
697 ReadBuffConn(passthroughContext, logger, clientConn, clientBufferChannel, errChannel, false)
698 }()
699

Callers

nothing calls this directly

Calls 9

LogErrorFunction · 0.92
ReadBuffConnFunction · 0.85
RecoverFunction · 0.70
DebugMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
TimeoutMethod · 0.65
StringMethod · 0.45
RemoteAddrMethod · 0.45

Tested by

no test coverage detected