Compare commits
6 Commits
v2024.6.26
...
v2024.6.26
Author | SHA1 | Date | |
---|---|---|---|
f092520ee4 | |||
9084b6e05f | |||
5494abded4 | |||
059af1b6ee | |||
532f30c155 | |||
49f2ccbc7a |
@ -63,6 +63,9 @@ nfpms:
|
|||||||
- src: layers
|
- src: layers
|
||||||
dst: /etc/bouncer/layers
|
dst: /etc/bouncer/layers
|
||||||
type: config
|
type: config
|
||||||
|
- src: templates
|
||||||
|
dst: /etc/bouncer/templates
|
||||||
|
type: config
|
||||||
- dst: /etc/bouncer/bootstrap.d
|
- dst: /etc/bouncer/bootstrap.d
|
||||||
type: dir
|
type: dir
|
||||||
file_info:
|
file_info:
|
||||||
|
@ -22,6 +22,7 @@ RUN make GORELEASER_ARGS='build --rm-dist --single-target --snapshot' goreleaser
|
|||||||
|
|
||||||
# Patch config
|
# Patch config
|
||||||
RUN /src/dist/bouncer_linux_amd64_v1/bouncer -c '' config dump > /src/dist/bouncer_linux_amd64_v1/config.yml \
|
RUN /src/dist/bouncer_linux_amd64_v1/bouncer -c '' config dump > /src/dist/bouncer_linux_amd64_v1/config.yml \
|
||||||
|
&& yq -i '.proxy.templates.dir = "/usr/share/bouncer/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
||||||
&& yq -i '.layers.queue.templateDir = "/usr/share/bouncer/layers/queue/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
&& yq -i '.layers.queue.templateDir = "/usr/share/bouncer/layers/queue/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
||||||
&& yq -i '.layers.authn.templateDir = "/usr/share/bouncer/layers/authn/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
&& yq -i '.layers.authn.templateDir = "/usr/share/bouncer/layers/authn/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
||||||
&& yq -i '.admin.auth.privateKey = "/etc/bouncer/admin-key.json"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
&& yq -i '.admin.auth.privateKey = "/etc/bouncer/admin-key.json"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
||||||
@ -42,6 +43,7 @@ RUN mkdir -p /usr/local/bin /usr/share/bouncer/bin /etc/bouncer
|
|||||||
|
|
||||||
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/bouncer /usr/share/bouncer/bin/bouncer
|
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/bouncer /usr/share/bouncer/bin/bouncer
|
||||||
COPY --from=BUILD /src/layers /usr/share/bouncer/layers
|
COPY --from=BUILD /src/layers /usr/share/bouncer/layers
|
||||||
|
COPY --from=BUILD /src/templates /usr/share/bouncer/templates
|
||||||
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/config.yml /etc/bouncer/config.yml
|
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/config.yml /etc/bouncer/config.yml
|
||||||
|
|
||||||
RUN ln -s /usr/share/bouncer/bin/bouncer /usr/local/bin/bouncer
|
RUN ln -s /usr/share/bouncer/bin/bouncer /usr/local/bin/bouncer
|
||||||
|
11
README.md
11
README.md
@ -4,7 +4,16 @@
|
|||||||
|
|
||||||
# Bouncer
|
# Bouncer
|
||||||
|
|
||||||
Serveur mandataire inverse (_"reverse proxy"_) filtrant avec gestion de files d'attente dynamiques.
|
Serveur mandataire inverse (_"reverse proxy"_) avec fonctionnalités avancées pilotable par API REST.
|
||||||
|
|
||||||
|
## Fonctionnalités
|
||||||
|
|
||||||
|
- Authentification unique basée sur entêtes HTTP ("Trusted headers SSO") avec:
|
||||||
|
- Fournisseur d'identité OpenID Connect ;
|
||||||
|
- Basic Auth ;
|
||||||
|
- Origine réseau ;
|
||||||
|
- Gestion de files d'attente dynamiques pour maîtriser la charge sur les services protégés ;
|
||||||
|
- Réécriture dynamique des attributs (notamment entêtes HTTP) des requêtes/réponses via un DSL.
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
# Documentation
|
# Documentation
|
||||||
|
|
||||||
- [(FR) - Premiers pas](./fr/getting-started.md)
|
|
||||||
- [(FR) - Architecture générale](./fr/general-architecture.md)
|
- [(FR) - Architecture générale](./fr/general-architecture.md)
|
||||||
|
- [(FR) - Terminologie](./fr/terminology.md)
|
||||||
|
- [(FR) - Premiers pas](./fr/getting-started.md)
|
||||||
|
|
||||||
## Exemples
|
## Exemples
|
||||||
|
|
||||||
|
@ -2,31 +2,6 @@
|
|||||||
|
|
||||||
## Modèles de déploiement
|
## Modèles de déploiement
|
||||||
|
|
||||||
### Déploiement mono-noeud
|
### Mode mono-noeud
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Terminologie
|
|
||||||
|
|
||||||
Voici une liste des termes utilisés dans le lexique Bouncer.
|
|
||||||
|
|
||||||
### Proxy
|
|
||||||
|
|
||||||
Un "proxy" est une entité logique définissant le relation suivante:
|
|
||||||
|
|
||||||
- Un ou plusieurs patrons de filtrage sous la forme d'un patron d'URL avec le caractère `*` comme caractère générique. Ceux ci identifient le ou les domaines/chemins associés à l'entité;
|
|
||||||
- Une URL cible qui servira de base pour la réécriture des requêtes.
|
|
||||||
|
|
||||||
Un "proxy" peut avoir zéro ou plusieurs "layers" associés.
|
|
||||||
|
|
||||||
Un "proxy" peut être activé ou désactivé.
|
|
||||||
|
|
||||||
Un "proxy" a un poids qui définit son niveau de priorité dans la pile de traitement (plus son poids est élevé plus il est prioritaire).
|
|
||||||
|
|
||||||
### Layer
|
|
||||||
|
|
||||||
Un "layer" (calque) est une entité logique définissant un traitement à appliquer aux requêtes et/ou aux réponses transitant par un proxy.
|
|
||||||
|
|
||||||
Un "layer" peut être activé ou désactivé.
|
|
||||||
|
|
||||||
Un "layer" a un poids qui définit son niveau de priorité dans la pile de traitement (plus son poids est élevé plus il est prioritaire).
|
|
||||||
|
29
doc/fr/terminology.md
Normal file
29
doc/fr/terminology.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Terminologie
|
||||||
|
|
||||||
|
Voici une liste des termes utilisés dans le lexique Bouncer.
|
||||||
|
|
||||||
|
## Proxy
|
||||||
|
|
||||||
|
Un proxy est une entité logique définie par les propriétés suivantes:
|
||||||
|
|
||||||
|
- Il possède **un ou plusieurs filtres d'origine** sous la forme de motifs d'URL avec le caractère `*` comme joker. Ces filtres identifient le ou les URLs associées au proxy.
|
||||||
|
- Il peut avoir **zéro ou une URL cible**, qui servira de base pour la réécriture des requêtes. Si l'URL est absente, on parle alors de "passthrough" (voir note).
|
||||||
|
- Il peut avoir **zéro ou plusieurs "layers" associés**.
|
||||||
|
- Il peut être **activé ou désactivé**.
|
||||||
|
- Il a **un poids qui définit son niveau de priorité** dans la pile de traitement (plus son poids est élevé plus il est prioritaire).
|
||||||
|
|
||||||
|
Pour résumer un proxy répond à la question "_Quelle URL orienter vers quel serveur cible ?_".
|
||||||
|
|
||||||
|
> **Passthrough**
|
||||||
|
>
|
||||||
|
> Un proxy "passthrough" est un proxy n'ayant pas d'URL cible (champ vide). Dans ce cas si les motifs d'URLs correspondent à l'URL de la requête Bouncer appliquera les layers associés puis passera la main aux proxies suivants.
|
||||||
|
|
||||||
|
## Layer
|
||||||
|
|
||||||
|
Un layer est une entité logique définie par les propriétés suivantes:
|
||||||
|
|
||||||
|
- Il a **un type auquel est associé un schéma d'options** permettant de configurer son comportement.
|
||||||
|
- Il peut être **activé ou désactivé**.
|
||||||
|
- Il a **un poids qui définit son niveau de priorité** dans la pile de traitement (plus son poids est élevé plus il est prioritaire).
|
||||||
|
|
||||||
|
Pour résumer un layer répond à la question "_Quel traitement appliquer à la requête et/ou réponse ?_".
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.21
|
|||||||
toolchain go1.22.0
|
toolchain go1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20230701194111-c6b3d482cca6
|
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20240626132607-e1db6466a926
|
||||||
github.com/Masterminds/sprig/v3 v3.2.3
|
github.com/Masterminds/sprig/v3 v3.2.3
|
||||||
github.com/bsm/redislock v0.9.4
|
github.com/bsm/redislock v0.9.4
|
||||||
github.com/btcsuite/btcd/btcutil v1.1.3
|
github.com/btcsuite/btcd/btcutil v1.1.3
|
||||||
|
4
go.sum
4
go.sum
@ -8,8 +8,8 @@ cloud.google.com/go/logging v1.7.0 h1:CJYxlNNNNAMkHp9em/YEXcfJg+rPDg7YfwoRpMU+t5
|
|||||||
cloud.google.com/go/logging v1.7.0/go.mod h1:3xjP2CjkM3ZkO73aj4ASA5wRPGGCRrPIAeNqVNkzY8M=
|
cloud.google.com/go/logging v1.7.0/go.mod h1:3xjP2CjkM3ZkO73aj4ASA5wRPGGCRrPIAeNqVNkzY8M=
|
||||||
cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI=
|
cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI=
|
||||||
cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc=
|
cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc=
|
||||||
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20230701194111-c6b3d482cca6 h1:FTk0ZoaV5N8Tkps5Da5RrDMZZXSHZIuD67Hy1Y4fsos=
|
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20240626132607-e1db6466a926 h1:gSTTuW2lqH66cGVrhplrVrqos62BY1/GxR3KYh2TElk=
|
||||||
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20230701194111-c6b3d482cca6/go.mod h1:o8ZK5v/3J1dRmklFVn1l6WHAyQ3LgegyHjRIT8KLAFw=
|
forge.cadoles.com/Cadoles/go-proxy v0.0.0-20240626132607-e1db6466a926/go.mod h1:o8ZK5v/3J1dRmklFVn1l6WHAyQ3LgegyHjRIT8KLAFw=
|
||||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
|
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
|
||||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
@ -121,7 +121,7 @@ func (s *Server) deleteProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
type CreateProxyRequest struct {
|
type CreateProxyRequest struct {
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
To string `json:"to" validate:"required"`
|
To string `json:"to"`
|
||||||
From []string `json:"from" validate:"required"`
|
From []string `json:"from" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package layer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
|
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
|
||||||
@ -52,14 +53,16 @@ func QueryCommand() *cli.Command {
|
|||||||
|
|
||||||
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
||||||
|
|
||||||
proxies, err := client.QueryLayer(ctx.Context, proxyName, options...)
|
layers, err := client.QueryLayer(ctx.Context, proxyName, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(apierr.Wrap(err))
|
return errors.WithStack(apierr.Wrap(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(layers, sortLayerssByWeight)
|
||||||
|
|
||||||
hints := layerHeaderHints(baseFlags.OutputMode)
|
hints := layerHeaderHints(baseFlags.OutputMode)
|
||||||
|
|
||||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
|
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(layers)...); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,3 +70,13 @@ func QueryCommand() *cli.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sortLayerssByWeight(a *store.LayerHeader, b *store.LayerHeader) int {
|
||||||
|
if a.Weight < b.Weight {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if a.Weight > b.Weight {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
@ -19,7 +19,7 @@ func CreateCommand() *cli.Command {
|
|||||||
Name: "create",
|
Name: "create",
|
||||||
Usage: "Create proxy",
|
Usage: "Create proxy",
|
||||||
Flags: proxyFlag.WithProxyFlags(
|
Flags: proxyFlag.WithProxyFlags(
|
||||||
flag.ProxyTo(true),
|
flag.ProxyTo(),
|
||||||
flag.ProxyFrom(),
|
flag.ProxyFrom(),
|
||||||
),
|
),
|
||||||
Action: func(ctx *cli.Context) error {
|
Action: func(ctx *cli.Context) error {
|
||||||
|
@ -30,12 +30,11 @@ func ProxyName() cli.Flag {
|
|||||||
|
|
||||||
const KeyProxyTo = "proxy-to"
|
const KeyProxyTo = "proxy-to"
|
||||||
|
|
||||||
func ProxyTo(required bool) cli.Flag {
|
func ProxyTo() cli.Flag {
|
||||||
return &cli.StringFlag{
|
return &cli.StringFlag{
|
||||||
Name: KeyProxyTo,
|
Name: KeyProxyTo,
|
||||||
Usage: "Set `PROXY_TO` as proxy's destination url",
|
Usage: "Set `PROXY_TO` as proxy's destination url",
|
||||||
Value: "",
|
Value: "",
|
||||||
Required: required,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package proxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
"forge.cadoles.com/cadoles/bouncer/internal/client"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
|
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
|
||||||
@ -51,6 +52,8 @@ func QueryCommand() *cli.Command {
|
|||||||
return errors.WithStack(apierr.Wrap(err))
|
return errors.WithStack(apierr.Wrap(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(proxies, sortProxiesByWeight)
|
||||||
|
|
||||||
hints := proxyHeaderHints(baseFlags.OutputMode)
|
hints := proxyHeaderHints(baseFlags.OutputMode)
|
||||||
|
|
||||||
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
|
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
|
||||||
@ -61,3 +64,13 @@ func QueryCommand() *cli.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sortProxiesByWeight(a *store.ProxyHeader, b *store.ProxyHeader) int {
|
||||||
|
if a.Weight < b.Weight {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if a.Weight > b.Weight {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
@ -19,7 +19,7 @@ func UpdateCommand() *cli.Command {
|
|||||||
Name: "update",
|
Name: "update",
|
||||||
Usage: "Update proxy",
|
Usage: "Update proxy",
|
||||||
Flags: proxyFlag.WithProxyFlags(
|
Flags: proxyFlag.WithProxyFlags(
|
||||||
flag.ProxyTo(false),
|
flag.ProxyTo(),
|
||||||
flag.ProxyFrom(),
|
flag.ProxyFrom(),
|
||||||
flag.ProxyEnabled(),
|
flag.ProxyEnabled(),
|
||||||
flag.ProxyWeight(),
|
flag.ProxyWeight(),
|
||||||
|
@ -7,22 +7,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ProxyServerConfig struct {
|
type ProxyServerConfig struct {
|
||||||
|
Debug InterpolatedBool `yaml:"debug"`
|
||||||
HTTP HTTPConfig `yaml:"http"`
|
HTTP HTTPConfig `yaml:"http"`
|
||||||
Metrics MetricsConfig `yaml:"metrics"`
|
Metrics MetricsConfig `yaml:"metrics"`
|
||||||
Transport TransportConfig `yaml:"transport"`
|
Transport TransportConfig `yaml:"transport"`
|
||||||
Dial DialConfig `yaml:"dial"`
|
Dial DialConfig `yaml:"dial"`
|
||||||
Sentry SentryConfig `yaml:"sentry"`
|
Sentry SentryConfig `yaml:"sentry"`
|
||||||
Cache CacheConfig `yaml:"cache"`
|
Cache CacheConfig `yaml:"cache"`
|
||||||
|
Templates TemplatesConfig `yaml:"templates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultProxyServerConfig() ProxyServerConfig {
|
func NewDefaultProxyServerConfig() ProxyServerConfig {
|
||||||
return ProxyServerConfig{
|
return ProxyServerConfig{
|
||||||
|
Debug: false,
|
||||||
HTTP: NewHTTPConfig("0.0.0.0", 8080),
|
HTTP: NewHTTPConfig("0.0.0.0", 8080),
|
||||||
Metrics: NewDefaultMetricsConfig(),
|
Metrics: NewDefaultMetricsConfig(),
|
||||||
Transport: NewDefaultTransportConfig(),
|
Transport: NewDefaultTransportConfig(),
|
||||||
Dial: NewDefaultDialConfig(),
|
Dial: NewDefaultDialConfig(),
|
||||||
Sentry: NewDefaultSentryConfig(),
|
Sentry: NewDefaultSentryConfig(),
|
||||||
Cache: NewDefaultCacheConfig(),
|
Cache: NewDefaultCacheConfig(),
|
||||||
|
Templates: NewDefaultTemplatesConfig(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,3 +119,13 @@ func NewDefaultCacheConfig() CacheConfig {
|
|||||||
TTL: *NewInterpolatedDuration(time.Second * 30),
|
TTL: *NewInterpolatedDuration(time.Second * 30),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TemplatesConfig struct {
|
||||||
|
Dir InterpolatedString `yaml:"dir"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDefaultTemplatesConfig() TemplatesConfig {
|
||||||
|
return TemplatesConfig{
|
||||||
|
Dir: "./templates",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -34,19 +34,6 @@ func OriginalURL(ctx context.Context) (*url.URL, error) {
|
|||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func withProxy(ctx context.Context, proxy *store.Proxy) context.Context {
|
|
||||||
return context.WithValue(ctx, contextKeyProxy, proxy)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ctxProxy(ctx context.Context) (*store.Proxy, error) {
|
|
||||||
proxy, err := ctxValue[*store.Proxy](ctx, contextKeyProxy)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.WithStack(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return proxy, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func withLayers(ctx context.Context, layers []*store.Layer) context.Context {
|
func withLayers(ctx context.Context, layers []*store.Layer) context.Context {
|
||||||
return context.WithValue(ctx, contextKeyLayers, layers)
|
return context.WithValue(ctx, contextKeyLayers, layers)
|
||||||
}
|
}
|
||||||
|
@ -36,15 +36,15 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
|||||||
ctx = withOriginalURL(ctx, url)
|
ctx = withOriginalURL(ctx, url)
|
||||||
ctx = logger.With(ctx, logger.F("url", url.String()))
|
ctx = logger.With(ctx, logger.F("url", url.String()))
|
||||||
|
|
||||||
var match *store.Proxy
|
layers := make([]*store.Layer, 0)
|
||||||
|
|
||||||
MAIN:
|
|
||||||
for _, p := range proxies {
|
for _, p := range proxies {
|
||||||
for _, from := range p.From {
|
for _, from := range p.From {
|
||||||
logger.Debug(
|
logger.Debug(
|
||||||
ctx, "matching request with proxy's from",
|
ctx, "matching request with proxy's from",
|
||||||
logger.F("from", from),
|
logger.F("from", from),
|
||||||
)
|
)
|
||||||
|
|
||||||
if matches := wildcard.Match(url.String(), from); !matches {
|
if matches := wildcard.Match(url.String(), from); !matches {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -54,16 +54,26 @@ MAIN:
|
|||||||
logger.F("from", from),
|
logger.F("from", from),
|
||||||
)
|
)
|
||||||
|
|
||||||
match = p
|
ctx = logger.With(ctx,
|
||||||
break MAIN
|
logger.F("proxy", p.Name),
|
||||||
}
|
logger.F("host", r.Host),
|
||||||
|
logger.F("remoteAddr", r.RemoteAddr),
|
||||||
|
)
|
||||||
|
|
||||||
|
metricProxyRequestsTotal.With(prometheus.Labels{metricLabelProxy: string(p.Name)}).Add(1)
|
||||||
|
|
||||||
|
proxyLayers, err := d.getLayers(ctx, p.Name)
|
||||||
|
if err != nil {
|
||||||
|
return r, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if match == nil {
|
layers = append(layers, proxyLayers...)
|
||||||
return r, nil
|
|
||||||
|
if p.To == "" {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
toURL, err := url.Parse(match.To)
|
toURL, err := url.Parse(p.To)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, errors.WithStack(err)
|
return r, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
@ -72,24 +82,11 @@ MAIN:
|
|||||||
r.URL.Scheme = toURL.Scheme
|
r.URL.Scheme = toURL.Scheme
|
||||||
r.URL.Path = toURL.JoinPath(r.URL.Path).Path
|
r.URL.Path = toURL.JoinPath(r.URL.Path).Path
|
||||||
|
|
||||||
ctx = logger.With(ctx,
|
ctx = withLayers(ctx, layers)
|
||||||
logger.F("proxy", match.Name),
|
r = r.WithContext(ctx)
|
||||||
logger.F("host", r.Host),
|
|
||||||
logger.F("remoteAddr", r.RemoteAddr),
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.Debug(
|
return r, nil
|
||||||
ctx, "rewritten url",
|
}
|
||||||
logger.F("rewrittenURL", r.URL.String()),
|
|
||||||
)
|
|
||||||
|
|
||||||
metricProxyRequestsTotal.With(prometheus.Labels{metricLabelProxy: string(match.Name)}).Add(1)
|
|
||||||
|
|
||||||
ctx = withProxy(ctx, match)
|
|
||||||
|
|
||||||
layers, err := d.getLayers(ctx, match.Name)
|
|
||||||
if err != nil {
|
|
||||||
return r, errors.WithStack(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = withLayers(ctx, layers)
|
ctx = withLayers(ctx, layers)
|
||||||
|
@ -3,11 +3,14 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Cadoles/go-proxy"
|
"forge.cadoles.com/Cadoles/go-proxy"
|
||||||
@ -17,6 +20,8 @@ import (
|
|||||||
"forge.cadoles.com/cadoles/bouncer/internal/config"
|
"forge.cadoles.com/cadoles/bouncer/internal/config"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
|
|
||||||
|
"github.com/Masterminds/sprig/v3"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
@ -152,6 +157,7 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
|
|||||||
director.ResponseTransformer(),
|
director.ResponseTransformer(),
|
||||||
),
|
),
|
||||||
proxy.WithReverseProxyFactory(s.createReverseProxy),
|
proxy.WithReverseProxyFactory(s.createReverseProxy),
|
||||||
|
proxy.WithDefaultHandler(http.HandlerFunc(s.handleDefault)),
|
||||||
)
|
)
|
||||||
|
|
||||||
r.Handle("/*", handler)
|
r.Handle("/*", handler)
|
||||||
@ -180,18 +186,83 @@ func (s *Server) createReverseProxy(ctx context.Context, target *url.URL) *httpu
|
|||||||
httpTransport.DialContext = dialer.DialContext
|
httpTransport.DialContext = dialer.DialContext
|
||||||
|
|
||||||
reverseProxy.Transport = httpTransport
|
reverseProxy.Transport = httpTransport
|
||||||
reverseProxy.ErrorHandler = s.errorHandler
|
reverseProxy.ErrorHandler = s.handleError
|
||||||
|
|
||||||
return reverseProxy
|
return reverseProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
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) {
|
||||||
err = errors.WithStack(err)
|
err = errors.WithStack(err)
|
||||||
|
|
||||||
logger.Error(r.Context(), "proxy error", logger.E(err))
|
logger.Error(r.Context(), "proxy error", logger.E(err))
|
||||||
sentry.CaptureException(err)
|
sentry.CaptureException(err)
|
||||||
|
|
||||||
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(funcs ...OptionFunc) *Server {
|
func NewServer(funcs ...OptionFunc) *Server {
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
FROM golang:1.20 AS BUILD
|
|
||||||
|
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y make
|
|
||||||
|
|
||||||
ARG YQ_VERSION=4.34.1
|
|
||||||
|
|
||||||
RUN mkdir -p /usr/local/bin \
|
|
||||||
&& wget -O /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64 \
|
|
||||||
&& chmod +x /usr/local/bin/yq
|
|
||||||
|
|
||||||
COPY . /src
|
|
||||||
|
|
||||||
WORKDIR /src
|
|
||||||
|
|
||||||
RUN make GORELEASER_ARGS='build --rm-dist --single-target --snapshot' goreleaser
|
|
||||||
|
|
||||||
# Patch config
|
|
||||||
RUN /src/dist/bouncer_linux_amd64_v1/bouncer -c '' config dump > /src/dist/bouncer_linux_amd64_v1/config.yml \
|
|
||||||
&& yq -i '.layers.queue.templateDir = "/usr/share/bouncer/layers/queue/templates"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
|
||||||
&& yq -i '.admin.auth.privateKey = "/etc/bouncer/admin-key.json"' /src/dist/bouncer_linux_amd64_v1/config.yml \
|
|
||||||
&& yq -i '.redis.adresses = ["redis:6379"]' /src/dist/bouncer_linux_amd64_v1/config.yml
|
|
||||||
|
|
||||||
FROM alpine:3.18 AS RUNTIME
|
|
||||||
|
|
||||||
ARG DUMB_INIT_VERSION=1.2.5
|
|
||||||
|
|
||||||
RUN apk add --no-cache ca-certificates
|
|
||||||
|
|
||||||
RUN mkdir -p /usr/local/bin \
|
|
||||||
&& wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_x86_64 \
|
|
||||||
&& chmod +x /usr/local/bin/dumb-init
|
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
|
|
||||||
|
|
||||||
RUN mkdir -p /usr/local/bin /usr/share/bouncer/bin /etc/bouncer
|
|
||||||
|
|
||||||
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/bouncer /usr/share/bouncer/bin/bouncer
|
|
||||||
COPY --from=BUILD /src/layers /usr/share/bouncer/layers
|
|
||||||
COPY --from=BUILD /src/dist/bouncer_linux_amd64_v1/config.yml /etc/bouncer/config.yml
|
|
||||||
|
|
||||||
RUN ln -s /usr/share/bouncer/bin/bouncer /usr/local/bin/bouncer
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
EXPOSE 8081
|
|
||||||
|
|
||||||
ENV BOUNCER_CONFIG=/etc/bouncer/config.yml
|
|
||||||
|
|
||||||
CMD ["bouncer"]
|
|
@ -70,6 +70,12 @@ admin:
|
|||||||
|
|
||||||
# Configuration du service "proxy"
|
# Configuration du service "proxy"
|
||||||
proxy:
|
proxy:
|
||||||
|
# Activer/désactiver le mode debug (affichage ou non des messages d'erreur)
|
||||||
|
debug: false
|
||||||
|
# Configuration des templates utilisés par le proxy
|
||||||
|
templates:
|
||||||
|
# Répertoire contenant les templates
|
||||||
|
dir: /etc/bouncer/templates
|
||||||
http:
|
http:
|
||||||
# Hôte d'écoute du service,
|
# Hôte d'écoute du service,
|
||||||
# 0.0.0.0 pour écouter sur toutes les interfaces
|
# 0.0.0.0 pour écouter sur toutes les interfaces
|
||||||
|
96
templates/error.gohtml
Normal file
96
templates/error.gohtml
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
{{ define "default" }}
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||||
|
{{ $title := print .StatusCode " - " .Status }}
|
||||||
|
<title>{{ $title }}</title>
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
box-sizing: border-box;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
*,
|
||||||
|
*:before,
|
||||||
|
*:after {
|
||||||
|
box-sizing: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
body,
|
||||||
|
h1,
|
||||||
|
h2,
|
||||||
|
h3,
|
||||||
|
h4,
|
||||||
|
h5,
|
||||||
|
h6,
|
||||||
|
p,
|
||||||
|
ol,
|
||||||
|
ul {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
html,
|
||||||
|
body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
background-color: #ffe4e4;
|
||||||
|
}
|
||||||
|
|
||||||
|
#container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100%;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
#card {
|
||||||
|
padding: 1.5em 1em;
|
||||||
|
border: 1px solid #d90202;
|
||||||
|
background-color: #feb0b0;
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: 2px 2px #cccccc1c;
|
||||||
|
color: #810000 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
margin-bottom: 1.2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
margin-bottom: 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
code,
|
||||||
|
pre {
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.footer {
|
||||||
|
font-size: 0.7em;
|
||||||
|
margin-top: 2em;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container">
|
||||||
|
<div id="card">
|
||||||
|
<h2 class="title">{{ $title }}</h2>
|
||||||
|
{{ if .Debug }}
|
||||||
|
<pre>{{ .Err }}</pre>
|
||||||
|
{{ end }}
|
||||||
|
<p class="footer">
|
||||||
|
Propulsé par
|
||||||
|
<a href="https://forge.cadoles.com/Cadoles/bouncer">Bouncer</a>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{ end }}
|
Reference in New Issue
Block a user