From 95e86f289ec7cea91d22123e1a543b0e8919796a Mon Sep 17 00:00:00 2001 From: Yann Stepienik Date: Tue, 18 Apr 2023 18:55:59 +0100 Subject: [PATCH] [relase] v0.1.18-unstable3 - restore network clean up --- package.json | 2 +- src/proxy/SmartResponseWriter.go | 2 +- src/proxy/routerGen.go | 10 +++++++--- src/utils/types.go | 1 + 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index dc7140a..f89d139 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cosmos-server", - "version": "0.1.18-unstable2", + "version": "0.1.18-unstable3", "description": "", "main": "test-server.js", "bugs": { diff --git a/src/proxy/SmartResponseWriter.go b/src/proxy/SmartResponseWriter.go index 5e11387..d4af5f4 100644 --- a/src/proxy/SmartResponseWriter.go +++ b/src/proxy/SmartResponseWriter.go @@ -63,7 +63,7 @@ func (w *SmartResponseWriterWrapper) Write(p []byte) (int, error) { return 0, errors.New("Pending request cancelled due to SmartShield") } thro := shield.computeThrottle(w.policy, userConsumed) - utils.Debug(fmt.Sprintf("Throttle: %d", thro)) + w.ThrottleNext = 0 if thro > 0 { time.Sleep(time.Duration(thro) * time.Millisecond) diff --git a/src/proxy/routerGen.go b/src/proxy/routerGen.go index 25f6e61..f6e10e0 100644 --- a/src/proxy/routerGen.go +++ b/src/proxy/routerGen.go @@ -50,21 +50,21 @@ func RouterGen(route utils.ProxyRouteConfig, router *mux.Router, destination htt if route.UseHost { origin = origin.Host(route.Host) } - + if route.UsePathPrefix { if route.PathPrefix != "" && route.PathPrefix[0] != '/' { utils.Error("PathPrefix must start with a /", nil) } origin = origin.PathPrefix(route.PathPrefix) } - + if route.UsePathPrefix && route.StripPathPrefix { if route.PathPrefix != "" && route.PathPrefix[0] != '/' { utils.Error("PathPrefix must start with a /", nil) } destination = http.StripPrefix(route.PathPrefix, destination) } - + destination = SmartShieldMiddleware(route.SmartShield)(destination) originCORS := route.CORSOrigin @@ -102,6 +102,10 @@ func RouterGen(route utils.ProxyRouteConfig, router *mux.Router, destination htt )(destination) } + if route.MaxBandwith > 0 { + destination = utils.BandwithLimiterMiddleware(route.MaxBandwith)(destination) + } + origin.Handler(tokenMiddleware(route.AuthEnabled)(utils.CORSHeader(originCORS)((destination)))) utils.Log("Added route: [" + (string)(route.Mode) + "] " + route.Host + route.PathPrefix + " to " + route.Target + "") diff --git a/src/utils/types.go b/src/utils/types.go index 0b1aa7b..36f6851 100644 --- a/src/utils/types.go +++ b/src/utils/types.go @@ -125,6 +125,7 @@ type ProxyRouteConfig struct { ThrottlePerMinute int CORSOrigin string StripPathPrefix bool + MaxBandwith int64 AuthEnabled bool Target string `validate:"required"` SmartShield SmartShieldPolicy