aboutsummaryrefslogtreecommitdiff
path: root/net/crowdsec-firewall-bouncer/files/crowdsec-firewall-bouncer.initd
blob: f720e366ae759d94b39958224f8e38fd13bdd858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh /etc/rc.common
# (C) 2021 Gerald Kerma

START=99
USE_PROCD=1
NAME=crowdsec-firewall-bouncer
PROG=/usr/bin/crowdsec-firewall-bouncer
CONFIG=/etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
BACKEND=iptables
VARCONFIGDIR=/var/etc/crowdsec/bouncers
VARCONFIG=/var/etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
FW_BACKEND="iptables"

service_triggers() {
	procd_add_reload_trigger crowdsec-firewall-bouncer
}

init_config() {
	## CheckFirewall
	iptables="true"
	which iptables > /dev/null
	FW_BACKEND=""
	if [[ $? != 0 ]]; then
	  echo "iptables is not present"
	  iptables="false"
	else
	  FW_BACKEND="iptables"
	  echo "iptables found"
	fi

	nftables="true"
	which nft > /dev/null
	if [[ $? != 0 ]]; then
	  echo "nftables is not present"
	  nftables="false"
	else
	  FW_BACKEND="nftables"
	  echo "nftables found"
	fi

	if [ "$nftables" = "true" -a "$iptables" = "true" ]; then
	  echo "Found nftables(default) and iptables..."
	fi

	if [ "$FW_BACKEND" = "iptables" ]; then
	  which ipset > /dev/null
	  if [[ $? != 0 ]]; then
	    echo "ipset not found, install it !"
	  fi
	fi
	BACKEND=$FW_BACKEND

	# Create tmp dir & permissions if needed
	if [ ! -d "${VARCONFIGDIR}" ]; then
		mkdir -m 0755 -p "${VARCONFIGDIR}"
	fi;

	cp $CONFIG $VARCONFIG

	sed -i "s,^\(\s*mode\s*:\s*\).*\$,\1$BACKEND," $VARCONFIG
}

start_service() {
	init_config

	procd_open_instance
	procd_set_param command "$PROG" -c "$VARCONFIG"
	procd_close_instance
}