2023-04-24 20:52:12 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-06-26 14:36:28 +02:00
|
|
|
"html/template"
|
2023-04-24 20:52:12 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-07-01 21:43:18 +02:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2024-06-26 14:36:28 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2023-07-01 21:43:18 +02:00
|
|
|
"time"
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy"
|
2024-05-28 16:45:15 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/memory"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/ttl"
|
2023-04-24 20:52:12 +02:00
|
|
|
bouncerChi "forge.cadoles.com/cadoles/bouncer/internal/chi"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/config"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
2024-06-26 14:36:28 +02:00
|
|
|
|
|
|
|
"github.com/Masterminds/sprig/v3"
|
2023-07-05 20:05:30 +02:00
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
2023-04-24 20:52:12 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
"github.com/pkg/errors"
|
2023-06-30 18:26:27 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2023-04-24 20:52:12 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2024-05-28 16:45:15 +02:00
|
|
|
serverConfig config.ProxyServerConfig
|
|
|
|
redisConfig config.RedisConfig
|
|
|
|
directorLayers []director.Layer
|
|
|
|
directorCacheTTL time.Duration
|
|
|
|
proxyRepository store.ProxyRepository
|
|
|
|
layerRepository store.LayerRepository
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start(ctx context.Context) (<-chan net.Addr, <-chan error) {
|
|
|
|
errs := make(chan error)
|
|
|
|
addrs := make(chan net.Addr)
|
|
|
|
|
|
|
|
go s.run(ctx, addrs, errs)
|
|
|
|
|
|
|
|
return addrs, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan error) {
|
|
|
|
defer func() {
|
|
|
|
close(errs)
|
|
|
|
close(addrs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := s.initRepositories(ctx); err != nil {
|
|
|
|
errs <- errors.WithStack(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.serverConfig.HTTP.Host, s.serverConfig.HTTP.Port))
|
|
|
|
if err != nil {
|
|
|
|
errs <- errors.WithStack(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs <- listener.Addr()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := listener.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
|
|
|
errs <- errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
if err := listener.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
|
|
|
log.Printf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
router := chi.NewRouter()
|
|
|
|
|
|
|
|
logger.Info(ctx, "http server listening")
|
|
|
|
|
|
|
|
director := director.New(
|
|
|
|
s.proxyRepository,
|
|
|
|
s.layerRepository,
|
2024-05-28 16:45:15 +02:00
|
|
|
director.WithLayers(s.directorLayers...),
|
|
|
|
director.WithLayerCache(
|
|
|
|
ttl.NewCache(
|
|
|
|
memory.NewCache[string, []*store.Layer](),
|
|
|
|
memory.NewCache[string, time.Time](),
|
|
|
|
s.directorCacheTTL,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
director.WithProxyCache(
|
|
|
|
ttl.NewCache(
|
|
|
|
memory.NewCache[string, []*store.Proxy](),
|
|
|
|
memory.NewCache[string, time.Time](),
|
|
|
|
s.directorCacheTTL,
|
|
|
|
),
|
|
|
|
),
|
2023-04-24 20:52:12 +02:00
|
|
|
)
|
|
|
|
|
2023-07-06 16:16:11 +02:00
|
|
|
if s.serverConfig.HTTP.UseRealIP {
|
|
|
|
router.Use(middleware.RealIP)
|
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
router.Use(middleware.RequestLogger(bouncerChi.NewLogFormatter()))
|
|
|
|
|
2023-07-05 20:05:30 +02:00
|
|
|
if s.serverConfig.Sentry.DSN != "" {
|
|
|
|
logger.Info(ctx, "enabling sentry http middleware")
|
|
|
|
|
|
|
|
sentryMiddleware := sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
router.Use(sentryMiddleware.Handle)
|
|
|
|
}
|
|
|
|
|
2023-06-30 18:26:27 +02:00
|
|
|
if s.serverConfig.Metrics.Enabled {
|
|
|
|
metrics := s.serverConfig.Metrics
|
|
|
|
|
|
|
|
logger.Info(ctx, "enabling metrics", logger.F("endpoint", metrics.Endpoint))
|
|
|
|
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
if metrics.BasicAuth != nil {
|
|
|
|
logger.Info(ctx, "enabling authentication on metrics endpoint")
|
|
|
|
|
|
|
|
r.Use(middleware.BasicAuth(
|
|
|
|
"metrics",
|
|
|
|
metrics.BasicAuth.CredentialsMap(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Handle(string(metrics.Endpoint), promhttp.Handler())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Use(director.Middleware())
|
|
|
|
|
|
|
|
handler := proxy.New(
|
|
|
|
proxy.WithRequestTransformers(
|
|
|
|
director.RequestTransformer(),
|
|
|
|
),
|
|
|
|
proxy.WithResponseTransformers(
|
|
|
|
director.ResponseTransformer(),
|
|
|
|
),
|
2023-07-01 21:43:18 +02:00
|
|
|
proxy.WithReverseProxyFactory(s.createReverseProxy),
|
2024-06-26 16:22:30 +02:00
|
|
|
proxy.WithDefaultHandler(http.HandlerFunc(s.handleDefault)),
|
2023-06-30 18:26:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
r.Handle("/*", handler)
|
|
|
|
})
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
if err := http.Serve(listener, router); err != nil && !errors.Is(err, net.ErrClosed) {
|
|
|
|
errs <- errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(ctx, "http server exiting")
|
|
|
|
}
|
|
|
|
|
2023-07-01 21:43:18 +02:00
|
|
|
func (s *Server) createReverseProxy(ctx context.Context, target *url.URL) *httputil.ReverseProxy {
|
|
|
|
reverseProxy := httputil.NewSingleHostReverseProxy(target)
|
|
|
|
|
|
|
|
dialConfig := s.serverConfig.Dial
|
|
|
|
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: time.Duration(*dialConfig.Timeout),
|
|
|
|
KeepAlive: time.Duration(*dialConfig.KeepAlive),
|
|
|
|
FallbackDelay: time.Duration(*dialConfig.FallbackDelay),
|
|
|
|
DualStack: bool(dialConfig.DualStack),
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:17:05 +02:00
|
|
|
httpTransport := s.serverConfig.Transport.AsTransport()
|
|
|
|
httpTransport.DialContext = dialer.DialContext
|
2023-07-01 21:43:18 +02:00
|
|
|
|
2024-05-23 15:17:05 +02:00
|
|
|
reverseProxy.Transport = httpTransport
|
2024-06-26 16:22:30 +02:00
|
|
|
reverseProxy.ErrorHandler = s.handleError
|
2023-07-01 21:43:18 +02:00
|
|
|
|
|
|
|
return reverseProxy
|
|
|
|
}
|
|
|
|
|
2024-06-26 16:22:30 +02:00
|
|
|
func (s *Server) handleDefault(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := errors.Errorf("no proxy target found")
|
|
|
|
|
|
|
|
logger.Error(r.Context(), "proxy error", logger.E(err))
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
|
|
|
|
s.renderErrorPage(w, r, err, http.StatusBadGateway, http.StatusText(http.StatusBadGateway))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleError(w http.ResponseWriter, r *http.Request, err error) {
|
2023-07-05 20:05:30 +02:00
|
|
|
err = errors.WithStack(err)
|
|
|
|
|
|
|
|
logger.Error(r.Context(), "proxy error", logger.E(err))
|
|
|
|
sentry.CaptureException(err)
|
2023-07-01 21:43:18 +02:00
|
|
|
|
2024-06-26 14:36:28 +02:00
|
|
|
s.renderErrorPage(w, r, err, http.StatusBadGateway, http.StatusText(http.StatusBadGateway))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) renderErrorPage(w http.ResponseWriter, r *http.Request, err error, statusCode int, status string) {
|
|
|
|
templateData := struct {
|
|
|
|
StatusCode int
|
|
|
|
Status string
|
|
|
|
Err error
|
|
|
|
Debug bool
|
|
|
|
}{
|
|
|
|
Debug: bool(s.serverConfig.Debug),
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Status: status,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
s.renderPage(w, r, "error", strconv.FormatInt(int64(statusCode), 10), templateData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) renderPage(w http.ResponseWriter, r *http.Request, page string, block string, templateData any) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
templatesConf := s.serverConfig.Templates
|
|
|
|
|
|
|
|
pattern := filepath.Join(string(templatesConf.Dir), page+".gohtml")
|
|
|
|
|
|
|
|
logger.Info(ctx, "loading proxy templates", logger.F("pattern", pattern))
|
|
|
|
|
|
|
|
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseGlob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, "could not load proxy templates", logger.E(errors.WithStack(err)))
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
blockTmpl := tmpl.Lookup(block)
|
|
|
|
if blockTmpl == nil {
|
|
|
|
blockTmpl = tmpl.Lookup("default")
|
|
|
|
}
|
|
|
|
|
|
|
|
if blockTmpl == nil {
|
|
|
|
logger.Error(ctx, "could not find template block nor default one", logger.F("block", block))
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := blockTmpl.Execute(w, templateData); err != nil {
|
|
|
|
logger.Error(ctx, "could not render proxy page", logger.E(errors.WithStack(err)))
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-07-01 21:43:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 20:52:12 +02:00
|
|
|
func NewServer(funcs ...OptionFunc) *Server {
|
|
|
|
opt := defaultOption()
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
2024-05-28 16:45:15 +02:00
|
|
|
serverConfig: opt.ServerConfig,
|
|
|
|
redisConfig: opt.RedisConfig,
|
|
|
|
directorLayers: opt.DirectorLayers,
|
|
|
|
directorCacheTTL: opt.DirectorCacheTTL,
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
}
|