2023-04-24 20:52:12 +02:00
|
|
|
package director
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy"
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
2024-05-28 16:45:15 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
2023-04-24 20:52:12 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
|
|
|
"github.com/pkg/errors"
|
2023-06-30 18:26:27 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-04-24 20:52:12 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Director struct {
|
|
|
|
proxyRepository store.ProxyRepository
|
|
|
|
layerRepository store.LayerRepository
|
|
|
|
layerRegistry *LayerRegistry
|
2024-05-28 16:45:15 +02:00
|
|
|
|
|
|
|
proxyCache cache.Cache[string, []*store.Proxy]
|
|
|
|
layerCache cache.Cache[string, []*store.Layer]
|
2024-09-27 10:09:25 +02:00
|
|
|
|
|
|
|
handleError HandleErrorFunc
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
proxies, err := d.getProxies(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return r, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:21:01 +01:00
|
|
|
url := getRequestURL(r)
|
2024-04-12 16:41:11 +02:00
|
|
|
ctx = withOriginalURL(ctx, url)
|
|
|
|
ctx = logger.With(ctx, logger.F("url", url.String()))
|
2024-03-29 09:21:01 +01:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
layers := make([]*store.Layer, 0)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
for _, p := range proxies {
|
|
|
|
for _, from := range p.From {
|
2024-03-29 09:21:01 +01:00
|
|
|
if matches := wildcard.Match(url.String(), from); !matches {
|
2023-04-24 20:52:12 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-02 12:13:26 +02:00
|
|
|
proxyCtx := logger.With(ctx,
|
2024-06-26 16:22:30 +02:00
|
|
|
logger.F("proxy", p.Name),
|
|
|
|
logger.F("host", r.Host),
|
|
|
|
logger.F("remoteAddr", r.RemoteAddr),
|
|
|
|
)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
metricProxyRequestsTotal.With(prometheus.Labels{metricLabelProxy: string(p.Name)}).Add(1)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-10-02 12:13:26 +02:00
|
|
|
proxyLayers, err := d.getLayers(proxyCtx, p.Name)
|
2024-06-26 16:22:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return r, errors.WithStack(err)
|
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
layers = append(layers, proxyLayers...)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
if p.To == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
toURL, err := url.Parse(p.To)
|
|
|
|
if err != nil {
|
|
|
|
return r, errors.WithStack(err)
|
|
|
|
}
|
2024-06-24 17:18:31 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
r.URL.Host = toURL.Host
|
|
|
|
r.URL.Scheme = toURL.Scheme
|
|
|
|
r.URL.Path = toURL.JoinPath(r.URL.Path).Path
|
2023-06-30 18:26:27 +02:00
|
|
|
|
2024-10-02 12:13:26 +02:00
|
|
|
proxyCtx = withLayers(proxyCtx, layers)
|
|
|
|
r = r.WithContext(proxyCtx)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
return r, nil
|
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = withLayers(ctx, layers)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
const proxiesCacheKey = "proxies"
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
func (d *Director) getProxies(ctx context.Context) ([]*store.Proxy, error) {
|
2024-05-28 16:45:15 +02:00
|
|
|
proxies, exists := d.proxyCache.Get(proxiesCacheKey)
|
|
|
|
if exists {
|
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
headers, err := d.proxyRepository.QueryProxy(ctx, store.WithProxyQueryEnabled(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(store.ByProxyWeight(headers))
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
proxies = make([]*store.Proxy, 0, len(headers))
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
for _, h := range headers {
|
|
|
|
if !h.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy, err := d.proxyRepository.GetProxy(ctx, h.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
d.proxyCache.Set(proxiesCacheKey, proxies)
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) getLayers(ctx context.Context, proxyName store.ProxyName) ([]*store.Layer, error) {
|
2024-05-28 16:45:15 +02:00
|
|
|
cacheKey := "layers-" + string(proxyName)
|
|
|
|
|
|
|
|
layers, exists := d.layerCache.Get(cacheKey)
|
|
|
|
if exists {
|
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
headers, err := d.layerRepository.QueryLayers(ctx, proxyName, store.WithLayerQueryEnabled(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(store.ByLayerWeight(headers))
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
layers = make([]*store.Layer, 0, len(headers))
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
for _, h := range headers {
|
|
|
|
if !h.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
layer, err := d.layerRepository.GetLayer(ctx, proxyName, h.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
layers = append(layers, layer)
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
d.layerCache.Set(cacheKey, layers)
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
return layers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) RequestTransformer() proxy.RequestTransformer {
|
|
|
|
return func(r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2024-09-27 10:09:25 +02:00
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
layers, err := ctxLayers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errContextKeyNotFound) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 10:15:08 +02:00
|
|
|
logger.Error(ctx, "could not retrieve layers from context", logger.CapturedE(errors.WithStack(err)))
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, layer := range layers {
|
|
|
|
transformerLayer, ok := d.layerRegistry.GetRequestTransformer(layer.Type)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
transformer := transformerLayer.RequestTransformer(layer)
|
|
|
|
|
|
|
|
transformer(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) ResponseTransformer() proxy.ResponseTransformer {
|
|
|
|
return func(r *http.Response) error {
|
|
|
|
ctx := r.Request.Context()
|
|
|
|
layers, err := ctxLayers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errContextKeyNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, layer := range layers {
|
|
|
|
transformerLayer, ok := d.layerRegistry.GetResponseTransformer(layer.Type)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
transformer := transformerLayer.ResponseTransformer(layer)
|
|
|
|
|
|
|
|
if err := transformer(r); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) Middleware() proxy.Middleware {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2024-09-27 10:09:25 +02:00
|
|
|
ctx := withHandleError(r.Context(), d.handleError)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
r, err := d.rewriteRequest(r)
|
|
|
|
if err != nil {
|
2024-09-27 10:09:25 +02:00
|
|
|
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not rewrite request"))
|
2023-04-24 20:52:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 10:09:25 +02:00
|
|
|
ctx = r.Context()
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
layers, err := ctxLayers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errContextKeyNotFound) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 10:09:25 +02:00
|
|
|
HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not retrieve proxy and layers from context"))
|
2023-04-24 20:52:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpMiddlewares := make([]proxy.Middleware, 0)
|
|
|
|
for _, layer := range layers {
|
|
|
|
middleware, ok := d.layerRegistry.GetMiddleware(layer.Type)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
httpMiddlewares = append(httpMiddlewares, middleware.Middleware(layer))
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := createMiddlewareChain(next, httpMiddlewares)
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
func New(proxyRepository store.ProxyRepository, layerRepository store.LayerRepository, funcs ...OptionFunc) *Director {
|
|
|
|
opts := NewOptions(funcs...)
|
2023-04-24 20:52:12 +02:00
|
|
|
|
2024-05-28 16:45:15 +02:00
|
|
|
registry := NewLayerRegistry(opts.Layers...)
|
|
|
|
|
|
|
|
return &Director{
|
|
|
|
proxyRepository: proxyRepository,
|
|
|
|
layerRepository: layerRepository,
|
|
|
|
layerRegistry: registry,
|
|
|
|
proxyCache: opts.ProxyCache,
|
|
|
|
layerCache: opts.LayerCache,
|
2024-09-27 10:09:25 +02:00
|
|
|
handleError: opts.HandleError,
|
2024-05-28 16:45:15 +02:00
|
|
|
}
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|