8 Commits

2 changed files with 39 additions and 80 deletions

View File

@@ -1,5 +1,8 @@
# Extended Ban Mod for Minetest # Extended Ban Mod for Minetest
This mod attempts to be an improvement to Minetest's ban system. This mod attempts to be an improvement to Minetest's ban system.
* It supports normal bans and temporary bans (from 60 seconds up to the end of * It supports normal bans and temporary bans (from 60 seconds up to the end of
time, with 1 second granularity). time, with 1 second granularity).
* Records and joins all accounts using the same IP address and several IP * Records and joins all accounts using the same IP address and several IP
@@ -12,70 +15,26 @@ This mod attempts to be an improvement to Minetest's ban system.
* Has an API to ban and check the ban database to allows other mods to manage * Has an API to ban and check the ban database to allows other mods to manage
users (for example, anticheat mods). users (for example, anticheat mods).
## Wildcard/Subnet Ban Feature
The mod now supports wildcard (subnet) bans for IP addresses using trailing `*` notation. This allows you to ban entire IP ranges or subnets with a single command.
### How It Works
* **IPv4 Wildcard Bans**: Use a trailing `*` to match any IP address that starts with the specified prefix.
* Example: `192.168.1.*` will match all IPs from `192.168.1.0` to `192.168.1.255`
* Example: `10.0.*` will match all IPs from `10.0.0.0` to `10.0.255.255`
* Example: `172.*` will match all IPs from `172.0.0.0` to `172.255.255.255`
* **IPv6 Wildcard Bans**: Use a trailing `*` to match any IPv6 address that starts with the specified prefix.
* Example: `2001:db8:*` will match all IPv6 addresses starting with `2001:db8:`
* Example: `fe80:*` will match all link-local IPv6 addresses
### Usage Examples
**Ban an entire IPv4 subnet:**
```
/xban 192.168.1.* Banning entire subnet due to spam
```
**Temporarily ban an IPv4 range:**
```
/xtempban 10.0.* 24h Temporary subnet ban for suspected bot activity
```
**Ban an IPv6 prefix:**
```
/xban 2001:db8:* Banning IPv6 prefix
```
**Unban a wildcard entry:**
```
/xunban 192.168.1.*
```
### Notes
* Wildcard bans are checked when a player attempts to connect.
* Individual IP addresses can still be whitelisted even if they match a wildcard ban.
* The wildcard character `*` must be at the end of the IP address.
* For IPv4, you can use wildcards at any octet boundary (e.g., `192.*`, `192.168.*`, `192.168.1.*`).
* For IPv6, the wildcard matches the remaining part of the address after the specified prefix.
## Chat commands ## Chat commands
The mod provides the following chat commands. All commands require the `ban` The mod provides the following chat commands. All commands require the `ban`
privilege. privilege.
### `xban` ### `xban`
Bans a player permanently. Bans a player permanently.
**Usage:** `/xban <player_or_ip> <reason>` **Usage:** `/xban <player_or_ip> <reason>`
**Example:** `/xban 127.0.0.1 Some reason.` **Example:** `/xban 127.0.0.1 Some reason.`
**Wildcard Example:** `/xban 192.168.1.* Subnet ban`
### `xtempban` ### `xtempban`
Bans a player temporarily. Bans a player temporarily.
**Usage:** `/xtempban <player_or_ip> <time> <reason>` **Usage:** `/xtempban <player_or_ip> <time> <reason>`
The `time` parameter is a string in the format `<number><unit>` where `<unit>` The `time` parameter is a string in the format `<count><unit>` where `<unit>`
is one of `s` for seconds, `m` for minutes, `h` for hours, `D` for days, `W` is one of `s` for seconds, `m` for minutes, `h` for hours, `D` for days, `W`
for weeks, `M` for months, or `Y` for years. If the unit is omitted, it is for weeks, `M` for months, or `Y` for years. If the unit is omitted, it is
assumed to mean seconds. For example, `42s` means 42 seconds, `1337m` 1337 assumed to mean seconds. For example, `42s` means 42 seconds, `1337m` 1337
@@ -84,23 +43,28 @@ up. For example, `1Y3M3D7h` will ban for 1 year, 3 months, 3 days and 7 hours.
**Example:** `/xtempban Joe 3600 Some reason.` **Example:** `/xtempban Joe 3600 Some reason.`
**Wildcard Example:** `/xtempban 10.0.* 7D Temporary subnet ban`
### `xunban` ### `xunban`
Unbans a player. Unbans a player.
**Usage:** `/xunban <player_or_ip>` **Usage:** `/xunban <player_or_ip>`
**Example:** `/xunban Joe` **Example:** `/xunban Joe`
**Wildcard Example:** `/xunban 192.168.1.*`
### `xban_record` ### `xban_record`
Shows the ban record on chat. Shows the ban record on chat.
**Usage:** `/xban_record <player_or_ip>` **Usage:** `/xban_record <player_or_ip>`
This prints one ban entry per line, with the time the ban came into effect,
the expiration time (if applicable), the reason, and the source of the ban.
The record is printed to chat with one entry per line.
**Example:** `/xban_record Joe`
### `xban_wl` ### `xban_wl`
Manages the whitelist. Manages the whitelist.
**Usage:** `/xban_wl (add|del|get) <player_or_ip>` **Usage:** `/xban_wl (add|del|get) <player_or_ip>`
@@ -116,15 +80,18 @@ player is in the whitelist, and prints the status to chat.
**Example:** `/xban_record add Jane` **Example:** `/xban_record add Jane`
### `xban_gui` ### `xban_gui`
Shows a form to consult the database interactively. Shows a form to consult the database interactively.
**Usage:** `/xban_gui` **Usage:** `/xban_gui`
## Administrator commands ## Administrator commands
The following commands require the `server` privilege, so they are only The following commands require the `server` privilege, so they are only
available to server administrators. available to server administrators.
### `xban_dbi` ### `xban_dbi`
Imports ban entries from other database formats. Imports ban entries from other database formats.
**Usage:** `/xban_dbi <importer>` **Usage:** `/xban_dbi <importer>`
@@ -139,6 +106,7 @@ the supported import plugins at the time of writing:
**Example:** `/xban_dbi minetest` **Example:** `/xban_dbi minetest`
### `xban_cleanup` ### `xban_cleanup`
Removes all non-banned entries from the xban db. Removes all non-banned entries from the xban db.
**Usage:** `/xban_cleanup` **Usage:** `/xban_cleanup`

View File

@@ -49,31 +49,23 @@ local function concat_keys(t, sep)
return table.concat(keys, sep) return table.concat(keys, sep)
end end
-- supports wildcard IP pattern (both IPv4 and IPv6) function xban.find_entry(player, create) --> entry, index
function xban.find_entry(key, create) for index, e in ipairs(db) do
-- exact match (player or IP)
for i, e in ipairs(xban.db) do
if e.names[key] then return e, i end
end
-- wildcard pattern match for IPs
if key and key:find("[.:]") then
for i, e in ipairs(xban.db) do
for name in pairs(e.names) do for name in pairs(e.names) do
local wildcard_prefix = name:match("(.+[.:])%*$") if name == player then
if wildcard_prefix and key:sub(1, #wildcard_prefix) == wildcard_prefix then return e, index
return e, i
end
end end
end end
end end
if create then if create then
print(("Created new entry for `%s'"):format(player))
local e = { local e = {
names = { [key]=true }, names = { [player]=true },
banned = false, banned = false,
record = { }, record = { },
} }
table.insert(xban.db, e) table.insert(db, e)
return e, #xban.db return e, #db
end end
return nil return nil
end end
@@ -203,16 +195,13 @@ end
minetest.register_on_prejoinplayer(function(name, ip) minetest.register_on_prejoinplayer(function(name, ip)
local wl = db.whitelist or { } local wl = db.whitelist or { }
if wl[name] or wl[ip] then return end if wl[name] or wl[ip] then return end
local e = xban.find_entry(name) or xban.find_entry(ip)
local e = xban.find_entry(name) if not e then return end
if not e or not e.banned then if e.banned then
e = ip and xban.find_entry(ip) local date = (e.expires and os.date("%c", e.expires)
end or "the end of time")
return ("Banned: Expires: %s, Reason: %s"):format(
if e and e.banned then date, e.reason)
local date = e.expires and os.date("%c", e.expires) or "the end of time"
local reason = e.reason or "No reason given"
return ("Banned: Expires: %s, Reason: %s"):format(date, reason)
end end
end) end)
@@ -422,3 +411,5 @@ minetest.after(1, check_temp_bans)
dofile(xban.MP.."/dbimport.lua") dofile(xban.MP.."/dbimport.lua")
dofile(xban.MP.."/gui.lua") dofile(xban.MP.."/gui.lua")
minetest.log("action", "[xban2] loaded.")