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/ .idea/
.DS_Store .DS_Store
config
db
logs
+1 -1
View File
@@ -1,4 +1,4 @@
filenames: filenames:
- /var/log/traefik/*.log - /var/log/traefik/traefik.log
labels: labels:
type: traefik type: traefik
+14 -14
View File
@@ -58,8 +58,8 @@ func CreateConfig() *Config {
CrowdsecCapiLogin: "", CrowdsecCapiLogin: "",
CrowdsecCapiPwd: "", CrowdsecCapiPwd: "",
CrowdsecCapiScenarios: []string{}, CrowdsecCapiScenarios: []string{},
UpdateIntervalSeconds: 60, UpdateIntervalSeconds: 10,
DefaultDecisionSeconds: 60, DefaultDecisionSeconds: 10,
} }
} }
@@ -71,6 +71,7 @@ type Bouncer struct {
enabled bool enabled bool
crowdsecStreamHealthy bool crowdsecStreamHealthy bool
needInit bool
crowdsecScheme string crowdsecScheme string
crowdsecHost string crowdsecHost string
crowdsecKey string crowdsecKey string
@@ -116,14 +117,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
}, },
cache: ttl_map.New(), 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 || config.CrowdsecMode == aloneMode {
if config.CrowdsecMode == streamMode { if config.CrowdsecMode == aloneMode {
go handleStreamCache(bouncer, true)
} else if config.CrowdsecMode == aloneMode {
getToken(bouncer) getToken(bouncer)
time.AfterFunc(10*time.Second, func() { }
handleStreamCache(bouncer, false) ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
}) go func() {
for range ticker.C {
go handleStreamCache(bouncer)
}
}()
} }
return bouncer, nil 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())) setDecision(a.cache, remoteHost, true, int64(duration.Seconds()))
} }
func handleStreamCache(a *Bouncer, initialized bool) { func handleStreamCache(a *Bouncer) {
// TODO clean properly on exit. // TODO clean properly on exit.
time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
handleStreamCache(a, false)
})
var rawQuery string var rawQuery string
var path string var path string
if a.crowdsecMode == aloneMode { if a.crowdsecMode == aloneMode {
rawQuery = "" rawQuery = ""
path = crowdsecCapiDecisions path = crowdsecCapiDecisions
} else { } else {
rawQuery = fmt.Sprintf("startup=%t", initialized) rawQuery = fmt.Sprintf("startup=%t", !a.crowdsecStreamHealthy)
path = crowdsecLapiStreamRoute path = crowdsecLapiStreamRoute
} }
streamRouteURL := url.URL{ streamRouteURL := url.URL{
+29 -17
View File
@@ -15,28 +15,40 @@ services:
- "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin" - "--experimental.localplugins.bouncer.modulename=github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin"
volumes: volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro" - /var/run/docker.sock:/var/run/docker.sock:ro
- "logs:/var/log/traefik" - ./logs:/var/log/traefik
- ./:/plugins-local/src/github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin - ./:/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: labels:
- "traefik.enable=true" - "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.enabled=true"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseclapikey=40796d93c2958f9e58345514e67740e5"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=3829a6c9870e4726a377d8951ebb64a1m8psGvMaq1ykJ3zX" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapilogin=3829a6c9870e4726a377d8951ebb64a1m8psGvMaq1ykJ3zX"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=Q5pgN8bRNInHGdx6QCksdPOJVLeLQ7ipJntSeuP3r8088zXzRVs4G8liXAKfI1k6" - "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapipwd=Q5pgN8bRNInHGdx6QCksdPOJVLeLQ7ipJntSeuP3r8088zXzRVs4G8liXAKfI1k6"
- "traefik.http.middlewares.crowdsec.plugin.bouncer.crowdseccapiscenarios=crowdsecurity/http-backdoors-attempts,baudneo/zoneminder-bf" - "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: crowdsec:
image: crowdsecurity/crowdsec:v1.4.1 image: crowdsecurity/crowdsec:v1.4.1
@@ -48,9 +60,9 @@ services:
BOUNCER_KEY_TRAEFIK: 40796d93c2958f9e58345514e67740e5 BOUNCER_KEY_TRAEFIK: 40796d93c2958f9e58345514e67740e5
volumes: volumes:
- ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro - ./acquis.yaml:/etc/crowdsec/acquis.yaml:ro
- logs:/var/log/traefik:ro - ./logs:/var/log/traefik:ro
- crowdsec-db:/var/lib/crowdsec/data/ - ./db:/var/lib/crowdsec/data/
- crowdsec-config:/etc/crowdsec/ - ./config:/etc/crowdsec/
volumes: volumes:
logs: logs: