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)
| 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. |
| 642 | func 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 |
nothing calls this directly
no test coverage detected