Added check for loopback proxy enable state

- Added check and show 521 if the loopback proxy endpoint is disabled
This commit is contained in:
Toby Chui
2025-10-10 15:51:00 +08:00
parent e77f947d1d
commit cf2cf18136
2 changed files with 16 additions and 5 deletions

View File

@@ -272,6 +272,11 @@ func (ep *ProxyEndpoint) Remove() error {
return nil return nil
} }
// Check if the proxy endpoint is enabled
func (ep *ProxyEndpoint) IsEnabled() bool {
return !ep.Disabled
}
// Write changes to runtime without respawning the proxy handler // Write changes to runtime without respawning the proxy handler
// use prepare -> remove -> add if you change anything in the endpoint // use prepare -> remove -> add if you change anything in the endpoint
// that effects the proxy routing src / dest // that effects the proxy routing src / dest

View File

@@ -111,15 +111,21 @@ func (router *Router) rewriteURL(rooturl string, requestURL string) string {
// this prevents unnecessary external DNS lookup and connection, return true if swapped and request is already handled // this prevents unnecessary external DNS lookup and connection, return true if swapped and request is already handled
// by the loopback handler. Only continue if return is false // by the loopback handler. Only continue if return is false
func (h *ProxyHandler) upstreamHostSwap(w http.ResponseWriter, r *http.Request, selectedUpstream *loadbalance.Upstream) bool { func (h *ProxyHandler) upstreamHostSwap(w http.ResponseWriter, r *http.Request, selectedUpstream *loadbalance.Upstream) bool {
upstreamHostanme := selectedUpstream.OriginIpOrDomain upstreamHostname := selectedUpstream.OriginIpOrDomain
if strings.Contains(upstreamHostanme, ":") { if strings.Contains(upstreamHostname, ":") {
upstreamHostanme = strings.Split(upstreamHostanme, ":")[0] upstreamHostname = strings.Split(upstreamHostname, ":")[0]
} }
loopbackProxyEndpoint := h.Parent.GetProxyEndpointFromHostname(upstreamHostanme) loopbackProxyEndpoint := h.Parent.GetProxyEndpointFromHostname(upstreamHostname)
if loopbackProxyEndpoint != nil { if loopbackProxyEndpoint != nil {
//This is a loopback request. Swap the target to the loopback target //This is a loopback request. Swap the target to the loopback target
//h.Parent.Option.Logger.PrintAndLog("proxy", "Detected a loopback request to self. Swap the target to "+loopbackProxyEndpoint.RootOrMatchingDomain, nil) //h.Parent.Option.Logger.PrintAndLog("proxy", "Detected a loopback request to self. Swap the target to "+loopbackProxyEndpoint.RootOrMatchingDomain, nil)
h.hostRequest(w, r, loopbackProxyEndpoint) if loopbackProxyEndpoint.IsEnabled() {
h.hostRequest(w, r, loopbackProxyEndpoint)
} else {
//Endpoint disabled, return 503
http.ServeFile(w, r, "./web/rperror.html")
h.Parent.logRequest(r, false, 521, "host-http", r.Host, upstreamHostname)
}
return true return true
} }
return false return false