Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ MGATE_HTTP_WITHOUT_TLS_TARGET_HOST=localhost
MGATE_HTTP_WITHOUT_TLS_TARGET_PORT=8888
MGATE_HTTP_WITHOUT_TLS_TARGET_PATH=/messages


MGATE_HTTP_WITH_TLS_PORT=8087
MGATE_HTTP_WITH_TLS_PATH_PREFIX=/mgate-http
MGATE_HTTP_WITH_TLS_TARGET_PROTOCOL=http
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func main() {
}

// mGate server for HTTP without TLS
httpProxy, err := http.NewProxy(httpConfig, handler, logger, []string{}, []string{})
httpProxy, err := http.NewProxy(httpConfig, handler, logger, []string{}, []string{}, nil)
if err != nil {
panic(err)
}
Expand All @@ -149,7 +149,7 @@ func main() {
}

// mGate server for HTTP with TLS
httpTLSProxy, err := http.NewProxy(httpTLSConfig, handler, logger, []string{}, []string{})
httpTLSProxy, err := http.NewProxy(httpTLSConfig, handler, logger, []string{}, []string{}, nil)
if err != nil {
panic(err)
}
Expand All @@ -164,7 +164,7 @@ func main() {
}

// mGate server for HTTP with mTLS
httpMTLSProxy, err := http.NewProxy(httpMTLSConfig, handler, logger, []string{}, []string{})
httpMTLSProxy, err := http.NewProxy(httpMTLSConfig, handler, logger, []string{}, []string{}, nil)
if err != nil {
panic(err)
}
Expand Down
38 changes: 25 additions & 13 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Username: username,
}

if p.interceptor != nil {
var err error
r, err = p.interceptor.Intercept(r.Context(), r)
if err != nil {
encodeError(w, http.StatusBadRequest, err)
p.logger.Error("failed to intercept request", slog.Any("error", err))
return
}
}

if isWebSocketRequest(r) {
//nolint:contextcheck // handleWebSocket does not need context
p.handleWebSocket(w, r, s)
Expand Down Expand Up @@ -140,15 +150,16 @@ func encodeError(w http.ResponseWriter, defStatusCode int, err error) {

// Proxy represents HTTP Proxy.
type Proxy struct {
config mgate.Config
target *httputil.ReverseProxy
session session.Handler
logger *slog.Logger
wsUpgrader websocket.Upgrader
bypass Checker
config mgate.Config
target *httputil.ReverseProxy
session session.Handler
logger *slog.Logger
wsUpgrader websocket.Upgrader
bypass Checker
interceptor Interceptor
}

func NewProxy(config mgate.Config, handler session.Handler, logger *slog.Logger, allowedOrigins []string, bypassPaths []string) (Proxy, error) {
func NewProxy(config mgate.Config, handler session.Handler, logger *slog.Logger, allowedOrigins []string, bypassPaths []string, interceptor Interceptor) (Proxy, error) {
targetUrl := &url.URL{
Scheme: config.TargetProtocol,
Host: net.JoinHostPort(config.TargetHost, config.TargetPort),
Expand All @@ -162,12 +173,13 @@ func NewProxy(config mgate.Config, handler session.Handler, logger *slog.Logger,
wsUpgrader := websocket.Upgrader{CheckOrigin: checkOrigin(allowedOrigins)}

return Proxy{
config: config,
target: httputil.NewSingleHostReverseProxy(targetUrl),
session: handler,
logger: logger,
wsUpgrader: wsUpgrader,
bypass: bpc,
config: config,
target: httputil.NewSingleHostReverseProxy(targetUrl),
session: handler,
logger: logger,
wsUpgrader: wsUpgrader,
bypass: bpc,
interceptor: interceptor,
}, nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/http/interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package http

import (
"context"
"net/http"
)

// Interceptor is an interface for mGate intercept hook.
type Interceptor interface {
// Intercept is called on every request flowing through the Proxy.
// Requests can be modified before being sent to the broker or the client.
// If the interceptor returns a non-nil request, the modified request is sent.
// The error indicates unsuccessful interception and mGate is cancelling the request.
Intercept(ctx context.Context, r *http.Request) (*http.Request, error)
}
Loading