# crowdsecurity/cs-firewall-bouncer architecture

> Architecture diagram of the GitHub repository crowdsecurity/cs-firewall-bouncer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/crowdsecurity/cs-firewall-bouncer
- Repository: https://github.com/crowdsecurity/cs-firewall-bouncer

## Overview

This Go service synchronizes CrowdSec API decisions into firewall blocklists. The main loop loads configuration, initializes a selected backend, and adds or removes supported decisions; backend implementations cover iptables/ipset, nftables, and pf, with a dry-run option. Prometheus metrics are exposed alongside backend-specific counters. The README documents API synchronization and firewall support; detailed API polling and some backend internals were not included in the excerpts, so those links are limited to documented or directly evidenced relationships.

Main entry points: [`pkg/backend/backend.go`](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/backend/backend.go) (Backend dispatcher), [`cmd/root.go`](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/cmd/root.go) (Command and sync loop), [`pkg/metrics/metrics.go`](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/metrics/metrics.go) (Metrics definitions), [`pkg/iptables/iptables.go`](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/iptables.go) (iptables backend)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and sync"]
  node_entry["Program entry<br/>[main.go]"]
  node_control["Command and sync loop<br/>[root.go]"]
  node_backend["Backend dispatcher<br/>[backend.go]"]
end

subgraph group_backends["Firewall backends"]
  node_iptables["iptables backend<br/>[iptables.go]"]
  node_iptablesctx["iptables rules and sets"]
  node_ipset["ipset command helper<br/>[ipset.go]"]
  node_nftables["nftables backend<br/>[nftables.go]"]
  node_nftctx["nftables contexts"]
  node_pf["pf backend<br/>[pf.go]"]
  node_pfctx["pf contexts<br/>[pf_context.go]"]
  node_dryrun["Dry-run backend<br/>[dryrun.go]"]
end

subgraph group_observability["Metrics"]
  node_metricsapi["Metrics definitions<br/>[metrics.go]"]
  node_prometheus["Prometheus endpoint<br/>[root.go]"]
  node_ipmetrics["iptables metrics<br/>[metrics.go]"]
  node_nftmetrics["nftables metrics<br/>[metrics.go]"]
  node_pfmetrics["pf metrics<br/>[metrics.go]"]
end

subgraph group_shared["Shared support"]
  node_config["Configuration<br/>[config.go]"]
  node_types["Backend interface<br/>[types.go]"]
end

node_operator(("Operator"))
node_crowdsec["CrowdSec API"]

node_operator -->|"runs"| node_entry
node_entry -->|"executes"| node_control
node_control -->|"loads"| node_config
node_crowdsec -->|"provides decisions"| node_control
node_control -->|"dispatches decisions"| node_backend
node_backend -->|"selects"| node_iptables
node_backend -->|"selects"| node_nftables
node_backend -->|"selects"| node_pf
node_backend -->|"selects"| node_dryrun
node_backend -->|"implements contract"| node_types
node_iptables -->|"delegates rules"| node_iptablesctx
node_iptablesctx -->|"manages sets"| node_ipset
node_nftables -->|"uses contexts"| node_nftctx
node_pf -->|"uses contexts"| node_pfctx
node_control -->|"starts endpoint"| node_prometheus
node_control -->|"registers metrics"| node_metricsapi
node_iptables -->|"collects counters"| node_ipmetrics
node_ipmetrics -->|"updates gauges"| node_metricsapi
node_nftmetrics -->|"updates gauges"| node_metricsapi
node_pfmetrics -->|"updates gauges"| node_metricsapi

click node_entry "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/main.go"
click node_control "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/cmd/root.go"
click node_config "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/cfg/config.go"
click node_backend "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/backend/backend.go"
click node_iptables "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/iptables.go"
click node_iptablesctx "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/iptables_context.go"
click node_ipset "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/ipsetcmd/ipset.go"
click node_nftables "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/nftables.go"
click node_nftctx "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/nftables_context.go"
click node_pf "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/pf.go"
click node_pfctx "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/pf_context.go"
click node_dryrun "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/dryrun/dryrun.go"
click node_metricsapi "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/metrics/metrics.go"
click node_prometheus "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/cmd/root.go"
click node_ipmetrics "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/metrics.go"
click node_nftmetrics "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/metrics.go"
click node_pfmetrics "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/metrics.go"
click node_types "https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/types/types.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_control,node_backend toneBlue
class node_iptables,node_iptablesctx,node_ipset,node_nftables,node_nftctx,node_pf,node_pfctx,node_dryrun toneAmber
class node_metricsapi,node_prometheus,node_ipmetrics,node_nftmetrics,node_pfmetrics,node_crowdsec toneMint
class node_config,node_types toneRose
class node_operator toneIndigo
```

## Components

### Runtime and sync

- [Program entry](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/main.go): `main.go`
- [Command and sync loop](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/cmd/root.go): `cmd/root.go`
- [Backend dispatcher](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/backend/backend.go): `pkg/backend/backend.go`

### Firewall backends

- [iptables backend](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/iptables.go): `pkg/iptables/iptables.go`
- [iptables rules and sets](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/iptables_context.go): `pkg/iptables/iptables_context.go`
- [ipset command helper](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/ipsetcmd/ipset.go): `pkg/ipsetcmd/ipset.go`
- [nftables backend](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/nftables.go): `pkg/nftables/nftables.go`
- [nftables contexts](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/nftables_context.go): `pkg/nftables/nftables_context.go`
- [pf backend](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/pf.go): `pkg/pf/pf.go`
- [pf contexts](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/pf_context.go): `pkg/pf/pf_context.go`
- [Dry-run backend](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/dryrun/dryrun.go): `pkg/dryrun/dryrun.go`

### Metrics

- [Metrics definitions](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/metrics/metrics.go): `pkg/metrics/metrics.go`
- [Prometheus endpoint](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/cmd/root.go): `cmd/root.go`
- [iptables metrics](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/iptables/metrics.go): `pkg/iptables/metrics.go`
- [nftables metrics](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/nftables/metrics.go): `pkg/nftables/metrics.go`
- [pf metrics](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/pf/metrics.go): `pkg/pf/metrics.go`

### Shared support

- [Configuration](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/cfg/config.go): `pkg/cfg/config.go`
- [Backend interface](https://github.com/crowdsecurity/cs-firewall-bouncer/blob/main/pkg/types/types.go): `pkg/types/types.go`

### Other components

- **Operator**
- **CrowdSec API**

## Connections

- Operator → Program entry: runs (evidence: `main.go`)
- Program entry → Command and sync loop: executes (evidence: `main.go`)
- Command and sync loop → Configuration: loads (evidence: `cmd/root.go`)
- CrowdSec API → Command and sync loop: provides decisions (evidence: `README.md`)
- Command and sync loop → Backend dispatcher: dispatches decisions (evidence: `cmd/root.go`)
- Backend dispatcher → iptables backend: selects (evidence: `pkg/backend/backend.go`)
- Backend dispatcher → nftables backend: selects (evidence: `pkg/backend/backend.go`)
- Backend dispatcher → pf backend: selects (evidence: `pkg/backend/backend.go`)
- Backend dispatcher → Dry-run backend: selects (evidence: `pkg/backend/backend.go`)
- Backend dispatcher → Backend interface: implements contract (evidence: `pkg/backend/backend.go`)
- iptables backend → iptables rules and sets: delegates rules (evidence: `pkg/iptables/iptables.go`)
- iptables rules and sets → ipset command helper: manages sets (evidence: `pkg/iptables/iptables_context.go`)
- nftables backend → nftables contexts: uses contexts (evidence: `pkg/nftables/nftables.go`)
- pf backend → pf contexts: uses contexts (evidence: `pkg/pf/pf.go`)
- Command and sync loop → Prometheus endpoint: starts endpoint (evidence: `cmd/root.go`)
- Command and sync loop → Metrics definitions: registers metrics (evidence: `cmd/root.go`)
- iptables backend → iptables metrics: collects counters (evidence: `pkg/iptables/metrics.go`)
- iptables metrics → Metrics definitions: updates gauges (evidence: `pkg/iptables/metrics.go`)
- nftables metrics → Metrics definitions: updates gauges (evidence: `pkg/nftables/metrics.go`)
- pf metrics → Metrics definitions: updates gauges (evidence: `pkg/pf/metrics.go`)
