Skip to content

Commit dd06a0f

Browse files
committed
Update
1 parent 7873197 commit dd06a0f

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

blocked_ips.csv

Whitespace-only changes.

def.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function def() {
1313
1,
1414
),
1515
);
16+
17+
new File().ForceExistsFile(join("blocked_ips.csv"), "");
1618
}
1719

1820
module.exports = def;

ipban.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/bash
22

3-
declare -A blocked_ips
4-
53
BLOCK_TIME_MINUTES=$2
64
BLOCK_TIME_SECONDS=$((BLOCK_TIME_MINUTES * 60))
75

6+
CSV_FILE="blocked_ips.csv"
7+
88
function block_ip() {
99
local ip=$1
1010
local end_time=$(( $(date +%s) + BLOCK_TIME_SECONDS ))
@@ -13,7 +13,7 @@ function block_ip() {
1313
iptables -D INPUT -s $ip -j DROP
1414
fi
1515

16-
blocked_ips[$ip]=$end_time
16+
echo "$ip,$end_time" >> "$CSV_FILE"
1717

1818
iptables -A INPUT -s $ip -j DROP
1919
}

ipunban.sh

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#!/bin/bash
22

3-
declare -A blocked_ips
4-
5-
source ./ipban.sh
3+
CSV_FILE="blocked_ips.csv"
64

75
function check_ip() {
86
local ip=$1
97
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))
1312

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
1618

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"
2020
fi
2121
fi
2222
}
2323

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

0 commit comments

Comments
 (0)