2019-03-14 08:30:04 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-06 08:26:03 +00:00
|
|
|
"errors"
|
2019-03-14 08:30:04 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2022-06-27 13:16:08 +00:00
|
|
|
"strings"
|
2019-03-14 08:30:04 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-09-22 08:00:09 +00:00
|
|
|
"golang.org/x/net/http/httpguts"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StatusClientClosedRequest non-standard HTTP status code for client disconnection.
|
2019-03-14 08:30:04 +00:00
|
|
|
const StatusClientClosedRequest = 499
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StatusClientClosedRequestText non-standard HTTP status for client disconnection.
|
2019-03-14 08:30:04 +00:00
|
|
|
const StatusClientClosedRequestText = "Client Closed Request"
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func buildSingleHostProxy(target *url.URL, passHostHeader bool, flushInterval time.Duration, roundTripper http.RoundTripper, bufferPool httputil.BufferPool) http.Handler {
|
2024-01-08 08:10:06 +00:00
|
|
|
// Wrapping the roundTripper with the Tracing roundTripper,
|
|
|
|
// to handle the reverseProxy client span creation.
|
|
|
|
tracingRoundTripper := newTracingRoundTripper(roundTripper)
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
return &httputil.ReverseProxy{
|
|
|
|
Director: directorBuilder(target, passHostHeader),
|
2024-01-08 08:10:06 +00:00
|
|
|
Transport: tracingRoundTripper,
|
2022-11-16 10:38:07 +00:00
|
|
|
FlushInterval: flushInterval,
|
|
|
|
BufferPool: bufferPool,
|
|
|
|
ErrorHandler: errorHandler,
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func directorBuilder(target *url.URL, passHostHeader bool) func(req *http.Request) {
|
|
|
|
return func(outReq *http.Request) {
|
|
|
|
outReq.URL.Scheme = target.Scheme
|
|
|
|
outReq.URL.Host = target.Host
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
u := outReq.URL
|
|
|
|
if outReq.RequestURI != "" {
|
|
|
|
parsedURL, err := url.ParseRequestURI(outReq.RequestURI)
|
|
|
|
if err == nil {
|
|
|
|
u = parsedURL
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
outReq.URL.Path = u.Path
|
|
|
|
outReq.URL.RawPath = u.RawPath
|
2023-06-20 17:06:51 +00:00
|
|
|
// If a plugin/middleware adds semicolons in query params, they should be urlEncoded.
|
2022-11-16 10:38:07 +00:00
|
|
|
outReq.URL.RawQuery = strings.ReplaceAll(u.RawQuery, ";", "&")
|
|
|
|
outReq.RequestURI = "" // Outgoing request should not have RequestURI
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
outReq.Proto = "HTTP/1.1"
|
|
|
|
outReq.ProtoMajor = 1
|
|
|
|
outReq.ProtoMinor = 1
|
2019-12-18 10:22:06 +00:00
|
|
|
|
2023-03-21 11:00:46 +00:00
|
|
|
// Do not pass client Host header unless optsetter PassHostHeader is set.
|
2022-11-16 10:38:07 +00:00
|
|
|
if !passHostHeader {
|
|
|
|
outReq.Host = outReq.URL.Host
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
cleanWebSocketHeaders(outReq)
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
// cleanWebSocketHeaders Even if the websocket RFC says that headers should be case-insensitive,
|
|
|
|
// some servers need Sec-WebSocket-Key, Sec-WebSocket-Extensions, Sec-WebSocket-Accept,
|
|
|
|
// Sec-WebSocket-Protocol and Sec-WebSocket-Version to be case-sensitive.
|
|
|
|
// https://tools.ietf.org/html/rfc6455#page-20
|
|
|
|
func cleanWebSocketHeaders(req *http.Request) {
|
|
|
|
if !isWebSocketUpgrade(req) {
|
|
|
|
return
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
req.Header["Sec-WebSocket-Key"] = req.Header["Sec-Websocket-Key"]
|
|
|
|
delete(req.Header, "Sec-Websocket-Key")
|
|
|
|
|
|
|
|
req.Header["Sec-WebSocket-Extensions"] = req.Header["Sec-Websocket-Extensions"]
|
|
|
|
delete(req.Header, "Sec-Websocket-Extensions")
|
|
|
|
|
|
|
|
req.Header["Sec-WebSocket-Accept"] = req.Header["Sec-Websocket-Accept"]
|
|
|
|
delete(req.Header, "Sec-Websocket-Accept")
|
|
|
|
|
|
|
|
req.Header["Sec-WebSocket-Protocol"] = req.Header["Sec-Websocket-Protocol"]
|
|
|
|
delete(req.Header, "Sec-Websocket-Protocol")
|
|
|
|
|
|
|
|
req.Header["Sec-WebSocket-Version"] = req.Header["Sec-Websocket-Version"]
|
|
|
|
delete(req.Header, "Sec-Websocket-Version")
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 08:00:09 +00:00
|
|
|
func isWebSocketUpgrade(req *http.Request) bool {
|
2022-11-16 10:38:07 +00:00
|
|
|
return httpguts.HeaderValuesContainsToken(req.Header["Connection"], "Upgrade") &&
|
|
|
|
strings.EqualFold(req.Header.Get("Upgrade"), "websocket")
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorHandler(w http.ResponseWriter, req *http.Request, err error) {
|
2024-01-08 08:10:06 +00:00
|
|
|
statusCode := computeStatusCode(err)
|
|
|
|
|
|
|
|
logger := log.Ctx(req.Context())
|
|
|
|
logger.Debug().Err(err).Msgf("%d %s", statusCode, statusText(statusCode))
|
|
|
|
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
if _, werr := w.Write([]byte(statusText(statusCode))); werr != nil {
|
|
|
|
logger.Debug().Err(werr).Msg("Error while writing status code")
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func computeStatusCode(err error) int {
|
2022-11-16 10:38:07 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, io.EOF):
|
2024-01-08 08:10:06 +00:00
|
|
|
return http.StatusBadGateway
|
2022-11-16 10:38:07 +00:00
|
|
|
case errors.Is(err, context.Canceled):
|
2024-01-08 08:10:06 +00:00
|
|
|
return StatusClientClosedRequest
|
2022-11-16 10:38:07 +00:00
|
|
|
default:
|
|
|
|
var netErr net.Error
|
|
|
|
if errors.As(err, &netErr) {
|
|
|
|
if netErr.Timeout() {
|
2024-01-08 08:10:06 +00:00
|
|
|
return http.StatusGatewayTimeout
|
2022-11-16 10:38:07 +00:00
|
|
|
}
|
2024-01-08 08:10:06 +00:00
|
|
|
|
|
|
|
return http.StatusBadGateway
|
2022-11-16 10:38:07 +00:00
|
|
|
}
|
2022-09-22 08:00:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
return http.StatusInternalServerError
|
2022-09-22 08:00:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
func statusText(statusCode int) string {
|
|
|
|
if statusCode == StatusClientClosedRequest {
|
|
|
|
return StatusClientClosedRequestText
|
|
|
|
}
|
|
|
|
return http.StatusText(statusCode)
|
|
|
|
}
|