2024-06-25 14:03:49 +02:00
|
|
|
package rewriter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
proxy "forge.cadoles.com/Cadoles/go-proxy"
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
2024-09-24 15:46:42 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/util"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
|
|
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
2024-06-25 14:03:49 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
const LayerType store.LayerType = "rewriter"
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type Layer struct {
|
|
|
|
requestRuleEngine *util.RevisionedRuleEngine[*RequestVars, *LayerOptions]
|
|
|
|
responseRuleEngine *util.RevisionedRuleEngine[*ResponseVars, *LayerOptions]
|
|
|
|
}
|
2024-06-25 14:03:49 +02:00
|
|
|
|
|
|
|
func (l *Layer) LayerType() store.LayerType {
|
|
|
|
return LayerType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layer) Middleware(layer *store.Layer) proxy.Middleware {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
options, err := fromStoreOptions(layer.Options)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, "could not parse layer options", logger.E(errors.WithStack(err)))
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := wildcard.MatchAny(r.URL.String(), options.MatchURLs...)
|
|
|
|
if !matches {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
if err := l.applyRequestRules(ctx, r, layer.Revision, options); err != nil {
|
2024-06-25 14:03:49 +02:00
|
|
|
logger.Error(ctx, "could not apply request rules", logger.E(errors.WithStack(err)))
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResponseTransformer implements director.ResponseTransformerLayer.
|
|
|
|
func (l *Layer) ResponseTransformer(layer *store.Layer) proxy.ResponseTransformer {
|
|
|
|
return func(r *http.Response) error {
|
|
|
|
options, err := fromStoreOptions(layer.Options)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := wildcard.MatchAny(r.Request.URL.String(), options.MatchURLs...)
|
|
|
|
if !matches {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx := r.Request.Context()
|
|
|
|
|
|
|
|
if err := l.applyResponseRules(ctx, r, layer.Revision, options); err != nil {
|
2024-06-25 14:03:49 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 10:46:38 +02:00
|
|
|
func New(funcs ...OptionFunc) *Layer {
|
2024-09-24 15:46:42 +02:00
|
|
|
return &Layer{
|
|
|
|
requestRuleEngine: util.NewRevisionedRuleEngine(func(options *LayerOptions) (*rule.Engine[*RequestVars], error) {
|
|
|
|
engine, err := rule.NewEngine[*RequestVars](
|
|
|
|
rule.WithRules(options.Rules.Request...),
|
|
|
|
ruleHTTP.WithRequestFuncs(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}),
|
|
|
|
responseRuleEngine: util.NewRevisionedRuleEngine(func(options *LayerOptions) (*rule.Engine[*ResponseVars], error) {
|
|
|
|
engine, err := rule.NewEngine[*ResponseVars](
|
|
|
|
rule.WithRules(options.Rules.Response...),
|
|
|
|
ruleHTTP.WithResponseFuncs(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}),
|
|
|
|
}
|
2024-06-25 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ director.MiddlewareLayer = &Layer{}
|
|
|
|
_ director.ResponseTransformerLayer = &Layer{}
|
|
|
|
)
|