Files

42 lines
812 B
Go
Raw Permalink Normal View History

2026-05-26 16:32:55 +03:00
package deconnector
import (
"bufio"
2026-05-27 10:58:13 +03:00
"context"
2026-05-26 16:32:55 +03:00
"net"
"net/http"
)
2026-05-27 10:58:13 +03:00
func (d *Deconnector) Handle(ctx context.Context, clientConn net.Conn) {
2026-05-26 16:32:55 +03:00
defer clientConn.Close()
req, err := http.ReadRequest(bufio.NewReader(clientConn))
if err != nil {
d.app.Logger().WithError(err).Error("failed to read request")
return
}
if req.Method == http.MethodConnect {
_, port, _ := net.SplitHostPort(req.Host)
if port == "443" {
2026-05-27 10:58:13 +03:00
d.handleTunnel(ctx, clientConn, req.Host)
2026-05-26 16:32:55 +03:00
} else {
2026-05-27 10:58:13 +03:00
d.handleDeconnect(ctx, clientConn, req)
2026-05-26 16:32:55 +03:00
}
} else {
req.RequestURI = ""
if req.URL.Host == "" {
req.URL.Host = req.Host
}
2026-05-26 18:39:45 +03:00
2026-05-26 16:32:55 +03:00
req.URL.Scheme = "http"
d.app.Logger().
WithField("method", req.Method).
WithField("url", req.URL).
Info("Forwarding HTTP request")
2026-05-27 10:58:13 +03:00
d.forwardHTTP(ctx, clientConn, req)
2026-05-26 16:32:55 +03:00
}
}