1
1
package proxy
2
2
3
3
import (
4
+ "bufio"
4
5
"context"
5
6
"crypto/tls"
6
7
"fmt"
7
8
"io"
8
9
"log/slog"
9
10
"net"
10
11
"net/http"
12
+ "net/http/httptest"
11
13
"net/url"
12
14
"time"
13
15
@@ -361,40 +363,68 @@ func (p *Server) handleConnect(w http.ResponseWriter, r *http.Request) {
361
363
}
362
364
363
365
// Perform TLS handshake with the client using our certificates
366
+ p .logger .Debug ("Starting TLS handshake" , "hostname" , hostname )
364
367
tlsConn := tls .Server (conn , p .tlsConfig )
365
368
err = tlsConn .Handshake ()
366
369
if err != nil {
367
370
p .logger .Error ("TLS handshake failed" , "hostname" , hostname , "error" , err )
368
371
return
369
372
}
373
+ p .logger .Debug ("TLS handshake successful" , "hostname" , hostname )
370
374
371
375
// Now we have a TLS connection - handle HTTPS requests
376
+ p .logger .Debug ("Starting HTTPS request handling" , "hostname" , hostname )
372
377
p .handleTLSConnection (tlsConn , hostname )
378
+ p .logger .Debug ("HTTPS request handling completed" , "hostname" , hostname )
373
379
}
374
380
375
381
// handleTLSConnection processes decrypted HTTPS requests over the TLS connection
376
382
func (p * Server ) handleTLSConnection (tlsConn * tls.Conn , hostname string ) {
377
- // Create an HTTP server to handle requests over the TLS connection
378
- server := & http.Server {
379
- Handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
380
- // Set the hostname if not already set
381
- if r .URL .Host == "" {
382
- r .URL .Host = hostname
383
+ p .logger .Debug ("Creating HTTP server for TLS connection" , "hostname" , hostname )
384
+
385
+ // Use ReadRequest to manually read HTTP requests from the TLS connection
386
+ bufReader := bufio .NewReader (tlsConn )
387
+ for {
388
+ // Read HTTP request from TLS connection
389
+ req , err := http .ReadRequest (bufReader )
390
+ if err != nil {
391
+ if err == io .EOF {
392
+ p .logger .Debug ("TLS connection closed by client" , "hostname" , hostname )
393
+ } else {
394
+ p .logger .Debug ("Failed to read HTTP request" , "hostname" , hostname , "error" , err )
383
395
}
384
- if r .URL .Scheme == "" {
385
- r .URL .Scheme = "https"
386
- }
387
-
388
- // Process this as an HTTPS request
389
- p .handleDecryptedHTTPS (w , r )
390
- }),
391
- }
396
+ break
397
+ }
398
+
399
+ p .logger .Debug ("Processing decrypted HTTPS request" , "hostname" , hostname , "method" , req .Method , "path" , req .URL .Path )
400
+
401
+ // Set the hostname and scheme if not already set
402
+ if req .URL .Host == "" {
403
+ req .URL .Host = hostname
404
+ }
405
+ if req .URL .Scheme == "" {
406
+ req .URL .Scheme = "https"
407
+ }
392
408
393
- // Serve HTTP over the TLS connection
394
- err := server .Serve (& singleConnListener {conn : tlsConn })
395
- if err != nil && err != io .EOF {
396
- p .logger .Debug ("TLS connection closed" , "hostname" , hostname , "error" , err )
409
+ // Create a response recorder to capture the response
410
+ recorder := httptest .NewRecorder ()
411
+
412
+ // Process the HTTPS request
413
+ p .handleDecryptedHTTPS (recorder , req )
414
+
415
+ // Write the response back to the TLS connection
416
+ resp := recorder .Result ()
417
+ err = resp .Write (tlsConn )
418
+ if err != nil {
419
+ p .logger .Debug ("Failed to write response" , "hostname" , hostname , "error" , err )
420
+ break
421
+ }
422
+
423
+ // Close connection after single request (HTTP/1.0 style)
424
+ break
397
425
}
426
+
427
+ p .logger .Debug ("TLS connection handling completed" , "hostname" , hostname )
398
428
}
399
429
400
430
// handleDecryptedHTTPS handles decrypted HTTPS requests and applies rules
@@ -417,26 +447,4 @@ func (p *Server) handleDecryptedHTTPS(w http.ResponseWriter, r *http.Request) {
417
447
418
448
// Forward the HTTPS request
419
449
p .forwardHTTPSRequest (w , r )
420
- }
421
-
422
- // singleConnListener wraps a single connection to implement net.Listener
423
- type singleConnListener struct {
424
- conn net.Conn
425
- used bool
426
- }
427
-
428
- func (l * singleConnListener ) Accept () (net.Conn , error ) {
429
- if l .used {
430
- return nil , io .EOF
431
- }
432
- l .used = true
433
- return l .conn , nil
434
- }
435
-
436
- func (l * singleConnListener ) Close () error {
437
- return l .conn .Close ()
438
- }
439
-
440
- func (l * singleConnListener ) Addr () net.Addr {
441
- return l .conn .LocalAddr ()
442
450
}
0 commit comments