@@ -71,7 +71,10 @@ func (p *Server) Start(ctx context.Context) error {
71
71
if err != nil {
72
72
select {
73
73
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
+ }
75
78
return
76
79
default :
77
80
p .logger .Error ("Failed to accept connection" , "error" , err )
@@ -290,7 +293,12 @@ func (p *Server) handleConnect(w http.ResponseWriter, r *http.Request) {
290
293
p .logger .Error ("Failed to hijack connection" , "error" , err )
291
294
return
292
295
}
293
- defer func () { _ = 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
+ }()
294
302
295
303
// Send 200 Connection established response manually
296
304
_ , 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) {
416
424
417
425
// handleConnectionWithTLSDetection detects TLS vs HTTP and handles appropriately
418
426
func (p * Server ) handleConnectionWithTLSDetection (conn net.Conn ) {
419
- defer func () { _ = 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
+ }()
420
433
421
434
// Peek at first byte to detect protocol
422
435
buf := make ([]byte , 1 )
@@ -442,15 +455,25 @@ func (p *Server) handleConnectionWithTLSDetection(conn net.Conn) {
442
455
p .logger .Debug ("TLS handshake successful" )
443
456
// Use HTTP server with TLS connection
444
457
listener := newSingleConnectionListener (tlsConn )
445
- defer func () { _ = 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
+ }()
446
464
err = http .Serve (listener , http .HandlerFunc (p .handleDecryptedHTTPS ))
447
465
p .logger .Debug ("http.Serve completed for HTTPS" , "error" , err )
448
466
} else {
449
467
p .logger .Debug ("Detected HTTP request, handling normally" )
450
468
// Use HTTP server with regular connection
451
469
p .logger .Debug ("About to call http.Serve for HTTP connection" )
452
470
listener := newSingleConnectionListener (connWrapper )
453
- defer func () { _ = 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
+ }()
454
477
err = http .Serve (listener , http .HandlerFunc (p .handleHTTP ))
455
478
p .logger .Debug ("http.Serve completed" , "error" , err )
456
479
}
@@ -519,7 +542,10 @@ func (sl *singleConnectionListener) Close() error {
519
542
}
520
543
521
544
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
+ }
523
549
sl .conn = nil
524
550
}
525
551
return nil
@@ -625,51 +651,76 @@ func (p *Server) streamRequestToTarget(clientConn *tls.Conn, bufReader *bufio.Re
625
651
if err != nil {
626
652
return fmt .Errorf ("failed to connect to target %s: %v" , hostname , err )
627
653
}
628
- defer func () { _ = 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
+ }()
629
660
630
661
// Send HTTP request headers to target
631
662
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
+ }
633
667
634
668
// Send headers
635
669
for name , values := range req .Header {
636
670
for _ , value := range values {
637
671
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
+ }
639
676
}
640
677
}
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
+ }
642
682
643
683
// Stream request body and response bidirectionally
644
684
go func () {
645
685
// 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
+ }
647
690
}()
648
691
649
692
// 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
+
651
698
return nil
652
699
}
653
700
654
701
// handleConnectStreaming handles CONNECT requests with streaming TLS termination
655
702
func (p * Server ) handleConnectStreaming (tlsConn * tls.Conn , req * http.Request , hostname string ) {
656
703
p .logger .Debug ("Handling CONNECT request with streaming" , "hostname" , hostname )
657
-
704
+
658
705
// For CONNECT, we need to establish a tunnel but still maintain TLS termination
659
706
// This is the tricky part - we're already inside a TLS connection from the client
660
707
// The client is asking us to CONNECT to another server, but we want to intercept that too
661
-
708
+
662
709
// Send CONNECT response
663
710
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
+
666
717
// Now the client will try to do TLS handshake for the target server
667
718
// But we want to intercept and terminate it
668
719
// This means we need to do another level of TLS termination
669
-
720
+
670
721
// For now, let's create a simple tunnel and log that we're not inspecting
671
722
p .logger .Warn ("CONNECT tunnel established - content not inspected" , "hostname" , hostname )
672
-
723
+
673
724
// Create connection to real target
674
725
targetConn , err := net .Dial ("tcp" , req .Host )
675
726
if err != nil {
@@ -679,6 +730,15 @@ func (p *Server) handleConnectStreaming(tlsConn *tls.Conn, req *http.Request, ho
679
730
defer func () { _ = targetConn .Close () }()
680
731
681
732
// Bidirectional copy
682
- go func () { _ , _ = 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