Skip to content

Commit

Permalink
Allow to configure max number of elements in ipset set (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Dec 13, 2022
1 parent bb8370a commit e72308d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ LD_OPTS_VARS=\
-X github.com/crowdsecurity/cs-firewall-bouncer/pkg/version.Tag=$(BUILD_TAG)

ifdef BUILD_STATIC
export LD_OPTS=-ldflags "-a -v -s -w -extldflags '-static' $(LD_OPTS_VARS)" -tags netgo
export LD_OPTS=-ldflags "-a -s -w -extldflags '-static' $(LD_OPTS_VARS)" -tags netgo
else
export LD_OPTS=-ldflags "-a -v -s -w $(LD_OPTS_VARS)"
export LD_OPTS=-ldflags "-a -s -w $(LD_OPTS_VARS)"
endif

PREFIX?="/"
Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type bouncerConfig struct {
BlacklistsIpv4 string `yaml:"blacklists_ipv4"`
BlacklistsIpv6 string `yaml:"blacklists_ipv6"`
SetType string `yaml:"ipset_type"`
SetSize int `yaml:"ipset_size"`

// specific to iptables, following https://github.com/crowdsecurity/cs-firewall-bouncer/issues/19
IptablesChains []string `yaml:"iptables_chains"`
Expand Down Expand Up @@ -121,6 +122,10 @@ func newConfig(reader io.Reader) (*bouncerConfig, error) {
config.SetType = "nethash"
}

if config.SetSize == 0 {
config.SetSize = 65536
}

switch config.Mode {
case NftablesMode:
err := nftablesConfig(config)
Expand Down
2 changes: 2 additions & 0 deletions iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newIPTables(config *bouncerConfig) (backend, error) {
version: "v4",
SetName: config.BlacklistsIpv4,
SetType: config.SetType,
SetSize: config.SetSize,
StartupCmds: [][]string{},
ShutdownCmds: [][]string{},
CheckIptableCmds: [][]string{},
Expand All @@ -44,6 +45,7 @@ func newIPTables(config *bouncerConfig) (backend, error) {
version: "v6",
SetName: config.BlacklistsIpv6,
SetType: config.SetType,
SetSize: config.SetSize,
StartupCmds: [][]string{},
ShutdownCmds: [][]string{},
CheckIptableCmds: [][]string{},
Expand Down
7 changes: 5 additions & 2 deletions iptables_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ipTablesContext struct {
iptablesBin string
SetName string // crowdsec-netfilter
SetType string
SetSize int
StartupCmds [][]string // -I INPUT -m set --match-set myset src -j DROP
ShutdownCmds [][]string // -D INPUT -m set --match-set myset src -j DROP
CheckIptableCmds [][]string
Expand All @@ -40,9 +41,11 @@ func (ctx *ipTablesContext) CheckAndCreate() error {
return fmt.Errorf("set %s doesn't exist: %w", ctx.SetName, err)
}
if ctx.version == "v6" {
cmd = exec.Command(ctx.ipsetBin, "-exist", "create", ctx.SetName, ctx.SetType, "timeout", "300", "family", "inet6")
cmd = exec.Command(ctx.ipsetBin, "-exist", "create", ctx.SetName, ctx.SetType, "timeout", "300", "family",
"inet6", "maxelem", fmt.Sprintf("%d", ctx.SetSize))
} else {
cmd = exec.Command(ctx.ipsetBin, "-exist", "create", ctx.SetName, ctx.SetType, "timeout", "300")
cmd = exec.Command(ctx.ipsetBin, "-exist", "create", ctx.SetName, ctx.SetType, "timeout", "300",
"maxelem", fmt.Sprintf("%d", ctx.SetSize))
}
log.Infof("ipset set-up : %s", cmd.String())
if out, err := cmd.CombinedOutput(); err != nil {
Expand Down

0 comments on commit e72308d

Please sign in to comment.