Merge pull request #102 from jkaninda/refactor

chore: disable error interceptor on websocket
This commit is contained in:
2024-11-14 22:33:06 +01:00
committed by GitHub
4 changed files with 22 additions and 6 deletions

View File

@@ -43,14 +43,17 @@ func (rec *responseRecorder) Write(data []byte) (int, error) {
// ErrorInterceptor Middleware intercepts backend errors
func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if the connection is a WebSocket
if isWebSocketRequest(r) {
next.ServeHTTP(w, r)
return
}
rec := newResponseRecorder(w)
next.ServeHTTP(rec, r)
w.Header().Set("Proxied-By", "Goma Gateway")
w.Header().Del("Server") //Delete server name
if canIntercept(rec.statusCode, intercept.Errors) {
logger.Debug("An error occurred in the backend, %d", rec.statusCode)
logger.Error("Backend error: %d", rec.statusCode)
logger.Error("Request to %s resulted in error with status code %d\n", r.URL.Path, rec.statusCode)
RespondWithError(w, rec.statusCode, http.StatusText(rec.statusCode))
return
} else {
// No error: write buffered response to client
w.WriteHeader(rec.statusCode)
@@ -63,6 +66,9 @@ func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handle
})
}
func isWebSocketRequest(r *http.Request) bool {
return r.Header.Get("Upgrade") == "websocket" && r.Header.Get("Connection") == "Upgrade"
}
func canIntercept(code int, errors []int) bool {
return slices.Contains(errors, code)
}

View File

@@ -16,6 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"crypto/tls"
"fmt"
"github.com/jkaninda/goma-gateway/internal/middleware"
"github.com/jkaninda/goma-gateway/pkg/logger"
@@ -85,6 +86,11 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
r.URL.Path = strings.Replace(r.URL.Path, fmt.Sprintf("%s/", proxyRoute.path), proxyRoute.rewrite, 1)
}
}
// Custom transport with InsecureSkipVerify
proxy.Transport = &http.Transport{TLSClientConfig: &tls.Config{
InsecureSkipVerify: proxyRoute.insecureSkipVerify,
},
}
w.Header().Set("Proxied-By", gatewayName) //Set Server name
w.Header().Del("Server") // Remove the Server header
// Custom error handler for proxy errors

View File

@@ -118,6 +118,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
disableHostFording: route.DisableHostFording,
methods: route.Methods,
cors: route.Cors,
insecureSkipVerify: route.InsecureSkipVerify,
}
secureRouter := r.PathPrefix(util.ParseRoutePath(route.Path, midPath)).Subrouter()
//callBackRouter := r.PathPrefix(util.ParseRoutePath(route.Path, "/callback")).Subrouter()
@@ -225,6 +226,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
methods: route.Methods,
disableHostFording: route.DisableHostFording,
cors: route.Cors,
insecureSkipVerify: route.InsecureSkipVerify,
}
// create route
router := r.PathPrefix(route.Path).Subrouter()

View File

@@ -150,6 +150,7 @@ type Route struct {
// Destination Defines backend URL
Destination string `yaml:"destination"`
Backends []string `yaml:"backends"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify"`
// HealthCheck Defines the backend is health
HealthCheck RouteHealthCheck `yaml:"healthCheck"`
// Cors contains the route cors headers
@@ -243,6 +244,7 @@ type ProxyRoute struct {
methods []string
cors Cors
disableHostFording bool
insecureSkipVerify bool
}
type RoutePath struct {
route Route