Add Custom header usage to find the user IP

This commit is contained in:
MathieuHa
2022-10-15 18:49:41 +02:00
parent 552b30a9ef
commit 8eec1c5656
3 changed files with 14 additions and 11 deletions
+6 -1
View File
@@ -28,6 +28,7 @@ const (
crowdsecLapiStreamRoute = "v1/decisions/stream"
cacheBannedValue = "t"
cacheNoBannedValue = "f"
xForwardedFor = "X-Forwarded-For"
)
// Config the plugin configuration.
@@ -40,6 +41,7 @@ type Config struct {
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
ForwardedHeadersTrustedIPs []string `json:"forwardedheaderstrustedips,omitempty"`
ForwardedHeadersCustomName string `json:"forwardedheaderscustomheader,omitempty"`
}
// CreateConfig creates the default plugin configuration.
@@ -53,6 +55,7 @@ func CreateConfig() *Config {
UpdateIntervalSeconds: 60,
DefaultDecisionSeconds: 60,
ForwardedHeadersTrustedIPs: []string{},
ForwardedHeadersCustomName: xForwardedFor,
}
}
@@ -70,6 +73,7 @@ type Bouncer struct {
crowdsecMode string
updateInterval int64
defaultDecisionTimeout int64
customHeader string
poolStrategy *ip.PoolStrategy
client *http.Client
cache *ttl_map.Heap
@@ -96,6 +100,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
crowdsecHost: config.CrowdsecLapiHost,
crowdsecKey: config.CrowdsecLapiKey,
updateInterval: config.UpdateIntervalSeconds,
customHeader: config.ForwardedHeadersCustomName,
defaultDecisionTimeout: config.DefaultDecisionSeconds,
poolStrategy: &ip.PoolStrategy{
Checker: checker,
@@ -198,7 +203,7 @@ func contains(source []string, target string) bool {
// It returns the first IP that is not in the pool, or the empty string otherwise.
func getRemoteIP(bouncer *Bouncer, req *http.Request) (string, error) {
remoteIP := bouncer.poolStrategy.GetIP(req)
remoteIP := bouncer.poolStrategy.GetIP(req, bouncer.customHeader)
if len(remoteIP) != 0 {
return remoteIP, nil
}