3
# This script can ban and kick players based on more flexible
4
# criteria than the game can. It reads ban rules from the configuration
5
# script flexban_cfg.sh, which really is just a shell script in which you can
6
# execute the following commands. All commands operate on the player that last
7
# entered the server. You can use all the usual bash flow control, for example
8
# grep_geoip Australia && ban 60 "No Aussies!"
9
# bans all australians, while
10
# grep_geoip Australia || ban 60 "Aussies only!"
11
# bans all but Australians, and
12
# grep_geoip Australia || grep_whois Bruce && ban 60 "Aussie Bruces only!"
13
# bans everyone not from australia with Bruce appearing in their whois entry.
14
# (Sorry for the silly examples.)
16
# greps geoip for pattern, returns true on match. All arguments are passed to grep.
19
test ${BANME} = true && return 1
22
echo ${GEOIP} | grep "$*" 2>&1 > /dev/null
25
# greps whois for pattern, returns true on match. All arguments are passed to grep.
28
test ${BANME} = true && return 1
31
echo ${WHOIS} | grep "$*" 2>&1 > /dev/null
34
# checks for IP ranges, returns true if the IP is in the range.
35
# Range syntax: base_ip/significant bits, i.e.
37
# matches all IPs of the form 192.168.x.x.
38
function check_range()
40
if ! echo $1 | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\/[0-9]\+$" > /dev/null 2>&1; then
41
echo "Not an IP range: $1" 1>&2
45
if ! echo ${IP} | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$" > /dev/null 2>&1; then
46
echo "Internal error: Not a valid IP: ${IP}" 1>&2
50
check_range_core $(echo $IP | sed -e 's,\., ,g') $(echo $1 | sed -e 's,\., ,g' -e 's,/, ,')
53
# bans the player currently in investigation. Usage:
54
# ban <time> "<reason>"
57
test ${BANME} = true && return
61
test -z "${TIME}" && TIME=60
62
test -z "${REASON}" && REASON="Flexban rule match"
63
echo BAN_IP ${IP} ${TIME} ${REASON}
64
echo KICK ${PLAYER} ${REASON}
69
# whitelists the player currently in investigation; further ban commands are ignored.
72
# just set the flag that cancels all checks
76
#############################################
78
# End of commands to use in the configuration
79
# script, the rest are for internal use only.
81
#############################################
83
# IP range check core, syntax
84
# check_range_core CHECK_IP1 CHECK_IP2 CHECK_IP3 CHECK_IP4 RANGE_IP1 RANGE_IP2 RANGE_IP3 RANGE_IP4 BITS
85
function check_range_core()
87
IP_NUM=$(( $4 + 256*($3 + 256* ($2 + 256 * $1 ) ) ))
88
CHECK_NUM=$(( $8 + 256*($7 + 256* ($6 + 256 * $5 ) ) ))
89
RANGE=$(( (1 << $9) - 1 ))
90
CHECK_NUM=$(( ${CHECK_NUM} & (~${RANGE}) ))
91
UPPER_END=$(( ${CHECK_NUM} | ${RANGE} ))
92
# echo ${CHECK_NUM} ${UPPER_END} ${RANGE} ${IP_NUM}
93
test ${IP_NUM} -ge ${CHECK_NUM} && test ${IP_NUM} -lt ${UPPER_END}
96
# sets GEOIP to the output of geoiplookup
99
test -z "${GEOIP}" && GEOIP=$(geoiplookup ${IP})
102
# sets WHOIS to the output of whois
105
test -z "${WHOIS}" && WHOIS=$(whois ${IP})
108
# processes the player_entered messages individually
109
function process_core()
123
# processes the player_entered messages
126
while LINE=$(line); do
131
# for debugging, set the path like it would be when run from the
133
export PATH="${PATH}:$(dirname $(dirname $(dirname $0)))/config/"
137
grep ^PLAYER_ENTERED | process