mirror of
https://github.com/1f349/violet.git
synced 2024-11-26 21:31:39 +00:00
Add internal loop detection
This commit is contained in:
parent
a16617b131
commit
0dab17ea85
@ -73,6 +73,11 @@ func setupRateLimiter(rateLimit uint64, next http.Handler) http.Handler {
|
||||
|
||||
func setupFaviconMiddleware(fav *favicons.Favicons, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Header.Get("X-Violet-Loop-Detect") == "1" {
|
||||
rw.WriteHeader(http.StatusLoopDetected)
|
||||
_, _ = rw.Write([]byte("Detected a routing loop\n"))
|
||||
return
|
||||
}
|
||||
if req.Header.Get("X-Violet-Raw-Favicon") != "1" {
|
||||
switch req.URL.Path {
|
||||
case "/favicon.svg", "/favicon.png", "/favicon.ico":
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/1f349/violet/proxy"
|
||||
"github.com/1f349/violet/utils"
|
||||
"github.com/google/uuid"
|
||||
websocket2 "github.com/gorilla/websocket"
|
||||
"github.com/rs/cors"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
@ -155,6 +156,8 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
req2.Header.Set("X-Violet-Loop-Detect", "1")
|
||||
|
||||
// serve request with reverse proxy
|
||||
var resp *http.Response
|
||||
if r.HasFlag(FlagIgnoreCert) {
|
||||
@ -167,6 +170,12 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
utils.RespondVioletError(rw, http.StatusBadGateway, "error receiving internal round trip response")
|
||||
return
|
||||
}
|
||||
if resp.StatusCode == http.StatusLoopDetected {
|
||||
u := uuid.New()
|
||||
log.Printf("[ServeRoute::ServeHTTP()] Loop Detected: %s %s '%s' -> '%s'\n", u, req.Method, req.URL.String(), req2.URL.String())
|
||||
utils.RespondVioletError(rw, http.StatusLoopDetected, "error loop detected: "+u.String())
|
||||
return
|
||||
}
|
||||
|
||||
// copy headers and status code
|
||||
copyHeader(rw.Header(), resp.Header)
|
||||
|
Loading…
Reference in New Issue
Block a user