File tree 4 files changed +21
-19
lines changed
4 files changed +21
-19
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ function def() {
13
13
1 ,
14
14
) ,
15
15
) ;
16
+
17
+ new File ( ) . ForceExistsFile ( join ( "blocked_ips.csv" ) , "" ) ;
16
18
}
17
19
18
20
module . exports = def ;
Original file line number Diff line number Diff line change 1
1
#! /bin/bash
2
2
3
- declare -A blocked_ips
4
-
5
3
BLOCK_TIME_MINUTES=$2
6
4
BLOCK_TIME_SECONDS=$(( BLOCK_TIME_MINUTES * 60 ))
7
5
6
+ CSV_FILE=" blocked_ips.csv"
7
+
8
8
function block_ip() {
9
9
local ip=$1
10
10
local end_time=$(( $(date +% s) + BLOCK_TIME_SECONDS ))
@@ -13,7 +13,7 @@ function block_ip() {
13
13
iptables -D INPUT -s $ip -j DROP
14
14
fi
15
15
16
- blocked_ips[ $ip ]= $end_time
16
+ echo " $ip , $end_time " >> " $CSV_FILE "
17
17
18
18
iptables -A INPUT -s $ip -j DROP
19
19
}
Original file line number Diff line number Diff line change 1
1
#! /bin/bash
2
2
3
- declare -A blocked_ips
4
-
5
- source ./ipban.sh
3
+ CSV_FILE=" blocked_ips.csv"
6
4
7
5
function check_ip() {
8
6
local ip=$1
9
7
local current_time=$( date +%s)
10
- local block_time_minutes=${blocked_ips[$ip]}
11
- local block_time_seconds=$(( block_time_minutes * 60 ))
12
- local end_time=$(( block_time_seconds + current_time))
8
+ local end_time=$( awk -F' ,' -v ip=" $ip " ' $1 == ip {print $2}' " $CSV_FILE " )
9
+
10
+ if [ -n " $end_time " ]; then
11
+ local block_time_seconds=$(( end_time - current_time))
13
12
14
- if (( current_time >= end_time)) ; then
15
- unset blocked_ips[$ip ]
13
+ if (( block_time_seconds <= 0 )) ; then
14
+ if iptables -C INPUT -s $ip -j DROP & > /dev/null; then
15
+ iptables -D INPUT -s $ip -j DROP
16
+ echo " Unbanned IP: $ip "
17
+ fi
16
18
17
- if iptables -C INPUT -s $ip -j DROP & > /dev/null; then
18
- iptables -D INPUT -s $ip -j DROP
19
- echo " Unbanned IP: $ip "
19
+ awk -F' ,' -v ip=" $ip " ' $1 != ip' " $CSV_FILE " > " $CSV_FILE .tmp" && mv " $CSV_FILE .tmp" " $CSV_FILE "
20
20
fi
21
21
fi
22
22
}
23
23
24
- local_blocked_ips=( " ${ ! blocked_ips[@]} " )
25
-
26
- for ip in " ${local_blocked_ips[@]} " ; do
27
- check_ip $ip
28
- done
24
+ if [ -f " $CSV_FILE " ] ; then
25
+ while IFS= ' , ' read -r ip _ ; do
26
+ check_ip " $ip "
27
+ done < " $CSV_FILE "
28
+ fi
You can’t perform that action at this time.
0 commit comments