fix loop

This commit is contained in:
Max Lerebourg
2022-10-01 19:09:22 +02:00
parent 1ebde35693
commit 68adb31065
4 changed files with 49 additions and 34 deletions
+3
View File
@@ -1,2 +1,5 @@
.idea/
.DS_Store
config
db
logs
+1 -1
View File
@@ -1,4 +1,4 @@
filenames:
- /var/log/traefik/*.log
- /var/log/traefik/traefik.log
labels:
type: traefik
+15 -15
View File
@@ -58,8 +58,8 @@ func CreateConfig() *Config {
CrowdsecCapiLogin: "",
CrowdsecCapiPwd: "",
CrowdsecCapiScenarios: []string{},
UpdateIntervalSeconds: 60,
DefaultDecisionSeconds: 60,
UpdateIntervalSeconds: 10,
DefaultDecisionSeconds: 10,
}
}
@@ -71,6 +71,7 @@ type Bouncer struct {
enabled bool
crowdsecStreamHealthy bool
needInit bool
crowdsecScheme string
crowdsecHost string
crowdsecKey string
@@ -116,14 +117,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
},
cache: ttl_map.New(),
}
// if we are on a stream mode, we fetch in a go routine every minute the new decisions.
if config.CrowdsecMode == streamMode {
go handleStreamCache(bouncer, true)
} else if config.CrowdsecMode == aloneMode {
getToken(bouncer)
time.AfterFunc(10*time.Second, func() {
handleStreamCache(bouncer, false)
})
if config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode {
if config.CrowdsecMode == aloneMode {
getToken(bouncer)
}
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
go func() {
for range ticker.C {
go handleStreamCache(bouncer)
}
}()
}
return bouncer, nil
}
@@ -265,18 +268,15 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
setDecision(a.cache, remoteHost, true, int64(duration.Seconds()))
}
func handleStreamCache(a *Bouncer, initialized bool) {
func handleStreamCache(a *Bouncer) {
// TODO clean properly on exit.
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
handleStreamCache(a, false)
})
var rawQuery string
var path string
if a.crowdsecMode == aloneMode {
rawQuery = ""
path = crowdsecCapiDecisions
} else {
rawQuery = fmt.Sprintf("startup=%t", initialized)
rawQuery = fmt.Sprintf("startup=%t", !a.crowdsecStreamHealthy)
path = crowdsecLapiStreamRoute
}
streamRouteURL := url.URL{
+29 -17
View File
@@ -15,28 +15,40 @@ services:
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "logs:/var/log/traefik"
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./logs:/var/log/traefik
- ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin
ports:
- 8000:80
- 8080:8080
depends_on:
- 'crowdsec'
whoami:
image: traefik/whoami
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.enabled=true"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=3829a6c9870e4726a377d8951ebb64a1m8psGvMaq1ykJ3zX"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=Q5pgN8bRNInHGdx6QCksdPOJVLeLQ7ipJntSeuP3r8088zXzRVs4G8liXAKfI1k6"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapiscenarios=crowdsecurity/http-backdoors-attempts,baudneo/zoneminder-bf"
ports:
- 8000:80
- 8080:8080
depends_on:
- crowdsec
whoami1:
image: traefik/whoami
container_name: "simple-service1"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
whoami2:
image: traefik/whoami
container_name: "simple-service2"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=crowdsec@docker"
- "traefik.http.services.whoami.loadbalancer.server.port=80"
crowdsec:
image: crowdsecurity/crowdsec:v1.4.1
@@ -48,9 +60,9 @@ services:
BOUNCER_KEY_TRAEFIK: 40796d93c2958f9e58345514e67740e5
volumes:
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
- logs:/var/log/traefik:ro
- crowdsec-db:/var/lib/crowdsec/data/
- crowdsec-config:/etc/crowdsec/
- ./logs:/var/log/traefik:ro
- ./db:/var/lib/crowdsec/data/
- ./config:/etc/crowdsec/
volumes:
logs: