@@ -71,7 +71,10 @@ func (p *Server) Start(ctx context.Context) error {
7171 if err != nil {
7272 select {
7373 case <- ctx .Done ():
74- listener .Close ()
74+ err = listener .Close ()
75+ if err != nil {
76+ p .logger .Error ("Failed to close listener" , "error" , err )
77+ }
7578 return
7679 default :
7780 p .logger .Error ("Failed to accept connection" , "error" , err )
@@ -194,7 +197,7 @@ func (p *Server) forwardRequest(w http.ResponseWriter, r *http.Request, https bo
194197 http .Error (w , fmt .Sprintf ("Failed to make request: %v" , err ), http .StatusBadGateway )
195198 return
196199 }
197- defer resp .Body .Close ()
200+ defer func () { _ = resp .Body .Close () } ()
198201
199202 p .logger .Debug ("Received response" , "status" , resp .StatusCode , "target" , targetURL .String ())
200203
@@ -238,7 +241,7 @@ func (p *Server) writeBlockedResponse(w http.ResponseWriter, r *http.Request) {
238241 host = r .Host
239242 }
240243
241- fmt .Fprintf (w , `🚫 Request Blocked by Boundary
244+ _ , _ = fmt .Fprintf (w , `🚫 Request Blocked by Boundary
242245
243246Request: %s %s
244247Host: %s
@@ -290,7 +293,12 @@ func (p *Server) handleConnect(w http.ResponseWriter, r *http.Request) {
290293 p .logger .Error ("Failed to hijack connection" , "error" , err )
291294 return
292295 }
293- defer conn .Close ()
296+ defer func () {
297+ err := conn .Close ()
298+ if err != nil {
299+ p .logger .Error ("Failed to close connection" , "error" , err )
300+ }
301+ }()
294302
295303 // Send 200 Connection established response manually
296304 _ , err = conn .Write ([]byte ("HTTP/1.1 200 Connection established\r \n \r \n " ))
@@ -416,7 +424,12 @@ func (p *Server) handleDecryptedHTTPS(w http.ResponseWriter, r *http.Request) {
416424
417425// handleConnectionWithTLSDetection detects TLS vs HTTP and handles appropriately
418426func (p * Server ) handleConnectionWithTLSDetection (conn net.Conn ) {
419- defer conn .Close ()
427+ defer func () {
428+ err := conn .Close ()
429+ if err != nil {
430+ p .logger .Error ("Failed to close connection" , "error" , err )
431+ }
432+ }()
420433
421434 // Peek at first byte to detect protocol
422435 buf := make ([]byte , 1 )
@@ -442,15 +455,25 @@ func (p *Server) handleConnectionWithTLSDetection(conn net.Conn) {
442455 p .logger .Debug ("TLS handshake successful" )
443456 // Use HTTP server with TLS connection
444457 listener := newSingleConnectionListener (tlsConn )
445- defer listener .Close ()
458+ defer func () {
459+ err := listener .Close ()
460+ if err != nil {
461+ p .logger .Error ("Failed to close connection" , "error" , err )
462+ }
463+ }()
446464 err = http .Serve (listener , http .HandlerFunc (p .handleDecryptedHTTPS ))
447465 p .logger .Debug ("http.Serve completed for HTTPS" , "error" , err )
448466 } else {
449467 p .logger .Debug ("Detected HTTP request, handling normally" )
450468 // Use HTTP server with regular connection
451469 p .logger .Debug ("About to call http.Serve for HTTP connection" )
452470 listener := newSingleConnectionListener (connWrapper )
453- defer listener .Close ()
471+ defer func () {
472+ err := listener .Close ()
473+ if err != nil {
474+ p .logger .Error ("Failed to close connection" , "error" , err )
475+ }
476+ }()
454477 err = http .Serve (listener , http .HandlerFunc (p .handleHTTP ))
455478 p .logger .Debug ("http.Serve completed" , "error" , err )
456479 }
@@ -519,7 +542,10 @@ func (sl *singleConnectionListener) Close() error {
519542 }
520543
521544 if sl .conn != nil {
522- sl .conn .Close ()
545+ err := sl .conn .Close ()
546+ if err != nil {
547+ return fmt .Errorf ("failed to close connection: %w" , err )
548+ }
523549 sl .conn = nil
524550 }
525551 return nil
@@ -613,9 +639,9 @@ func (p *Server) constructFullURL(req *http.Request, hostname string) string {
613639
614640// writeBlockedResponseStreaming writes a blocked response directly to the TLS connection
615641func (p * Server ) writeBlockedResponseStreaming (tlsConn * tls.Conn , req * http.Request ) {
616- response := fmt .Sprintf ("HTTP/1.1 403 Forbidden\r \n Content-Type: text/plain\r \n Connection: close\r \n \r \n 🚫 Request Blocked by Boundary\n \n Request: %s %s\n Host: %s\n \n To allow this request, restart boundary with:\n --allow \" %s\" \n " ,
642+ response := fmt .Sprintf ("HTTP/1.1 403 Forbidden\r \n Content-Type: text/plain\r \n Connection: close\r \n \r \n 🚫 Request Blocked by Boundary\n \n Request: %s %s\n Host: %s\n \n To allow this request, restart boundary with:\n --allow \" %s\" \n " ,
617643 req .Method , req .URL .Path , req .Host , req .Host )
618- tlsConn .Write ([]byte (response ))
644+ _ , _ = tlsConn .Write ([]byte (response ))
619645}
620646
621647// streamRequestToTarget streams the HTTP request (including body) to the target server
@@ -625,60 +651,94 @@ func (p *Server) streamRequestToTarget(clientConn *tls.Conn, bufReader *bufio.Re
625651 if err != nil {
626652 return fmt .Errorf ("failed to connect to target %s: %v" , hostname , err )
627653 }
628- defer targetConn .Close ()
654+ defer func () {
655+ err := targetConn .Close ()
656+ if err != nil {
657+ p .logger .Error ("Failed to close target connection" , "error" , err )
658+ }
659+ }()
629660
630661 // Send HTTP request headers to target
631662 reqLine := fmt .Sprintf ("%s %s %s\r \n " , req .Method , req .URL .RequestURI (), req .Proto )
632- targetConn .Write ([]byte (reqLine ))
663+ _ , err = targetConn .Write ([]byte (reqLine ))
664+ if err != nil {
665+ return fmt .Errorf ("failed to write request line to target: %v" , err )
666+ }
633667
634668 // Send headers
635669 for name , values := range req .Header {
636670 for _ , value := range values {
637671 headerLine := fmt .Sprintf ("%s: %s\r \n " , name , value )
638- targetConn .Write ([]byte (headerLine ))
672+ _ , err = targetConn .Write ([]byte (headerLine ))
673+ if err != nil {
674+ return fmt .Errorf ("failed to write header to target: %v" , err )
675+ }
639676 }
640677 }
641- targetConn .Write ([]byte ("\r \n " )) // End of headers
678+ _ , err = targetConn .Write ([]byte ("\r \n " )) // End of headers
679+ if err != nil {
680+ return fmt .Errorf ("failed to write headers to target: %v" , err )
681+ }
642682
643683 // Stream request body and response bidirectionally
644684 go func () {
645685 // Stream request body: client -> target
646- io .Copy (targetConn , bufReader )
686+ _ , err := io .Copy (targetConn , bufReader )
687+ if err != nil {
688+ p .logger .Error ("Error copying request body to target" , "error" , err )
689+ }
647690 }()
648691
649692 // Stream response: target -> client
650- io .Copy (clientConn , targetConn )
693+ _ , err = io .Copy (clientConn , targetConn )
694+ if err != nil {
695+ p .logger .Error ("Error copying response from target to client" , "error" , err )
696+ }
697+
651698 return nil
652699}
653700
654701// handleConnectStreaming handles CONNECT requests with streaming TLS termination
655702func (p * Server ) handleConnectStreaming (tlsConn * tls.Conn , req * http.Request , hostname string ) {
656703 p .logger .Debug ("Handling CONNECT request with streaming" , "hostname" , hostname )
657-
704+
658705 // For CONNECT, we need to establish a tunnel but still maintain TLS termination
659706 // This is the tricky part - we're already inside a TLS connection from the client
660707 // The client is asking us to CONNECT to another server, but we want to intercept that too
661-
708+
662709 // Send CONNECT response
663710 response := "HTTP/1.1 200 Connection established\r \n \r \n "
664- tlsConn .Write ([]byte (response ))
665-
711+ _ , err := tlsConn .Write ([]byte (response ))
712+ if err != nil {
713+ p .logger .Error ("Failed to send CONNECT response" , "error" , err )
714+ return
715+ }
716+
666717 // Now the client will try to do TLS handshake for the target server
667718 // But we want to intercept and terminate it
668719 // This means we need to do another level of TLS termination
669-
720+
670721 // For now, let's create a simple tunnel and log that we're not inspecting
671722 p .logger .Warn ("CONNECT tunnel established - content not inspected" , "hostname" , hostname )
672-
723+
673724 // Create connection to real target
674725 targetConn , err := net .Dial ("tcp" , req .Host )
675726 if err != nil {
676727 p .logger .Error ("Failed to connect to CONNECT target" , "target" , req .Host , "error" , err )
677728 return
678729 }
679- defer targetConn .Close ()
680-
730+ defer func () { _ = targetConn .Close () } ()
731+
681732 // Bidirectional copy
682- go io .Copy (targetConn , tlsConn )
683- io .Copy (tlsConn , targetConn )
684- }
733+ go func () {
734+ _ , err := io .Copy (targetConn , tlsConn )
735+ if err != nil {
736+ p .logger .Error ("Error copying from client to target" , "error" , err )
737+ }
738+ }()
739+ _ , err = io .Copy (tlsConn , targetConn )
740+ if err != nil {
741+ p .logger .Error ("Error copying from target to client" , "error" , err )
742+ }
743+ p .logger .Debug ("CONNECT tunnel closed" , "hostname" , hostname )
744+ }
0 commit comments