mirror of
https://github.com/minetest-mods/xban2.git
synced 2025-11-24 18:35:37 +01:00
Compare commits
5 Commits
master
...
43acd1c620
| Author | SHA1 | Date | |
|---|---|---|---|
| 43acd1c620 | |||
|
|
58e77ad16c | ||
|
|
af26ae75bd | ||
|
|
8fde3c240f | ||
|
|
d200b342e9 |
24
LICENSE
24
LICENSE
@@ -1,24 +0,0 @@
|
|||||||
BSD 2-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2014-2023, Diego Martínez
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
76
README.md
76
README.md
@@ -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>`
|
||||||
@@ -137,8 +104,3 @@ the supported import plugins at the time of writing:
|
|||||||
* `v2`: Old format used by xban (`players.iplist.v2`).
|
* `v2`: Old format used by xban (`players.iplist.v2`).
|
||||||
|
|
||||||
**Example:** `/xban_dbi minetest`
|
**Example:** `/xban_dbi minetest`
|
||||||
|
|
||||||
### `xban_cleanup`
|
|
||||||
Removes all non-banned entries from the xban db.
|
|
||||||
|
|
||||||
**Usage:** `/xban_cleanup`
|
|
||||||
|
|||||||
5
gui.lua
5
gui.lua
@@ -61,8 +61,7 @@ local function make_fs(name)
|
|||||||
"size[16,12]",
|
"size[16,12]",
|
||||||
"label[0,-.1;Filter]",
|
"label[0,-.1;Filter]",
|
||||||
"field[1.5,0;12.8,1;filter;;"..ESC(filter).."]",
|
"field[1.5,0;12.8,1;filter;;"..ESC(filter).."]",
|
||||||
"field_close_on_enter[filter;false]",
|
"button[14,-.3;2,1;search;Search]",
|
||||||
"button[14,-.3;2,1;search_submit;Search]",
|
|
||||||
}
|
}
|
||||||
local fsn = #fs
|
local fsn = #fs
|
||||||
fsn=fsn+1 fs[fsn] = format("textlist[0,.8;4,9.3;player;%s;%d;0]",
|
fsn=fsn+1 fs[fsn] = format("textlist[0,.8;4,9.3;player;%s;%d;0]",
|
||||||
@@ -123,7 +122,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if fields.search_submit or fields.filter then
|
if fields.search then
|
||||||
local filter = fields.filter or ""
|
local filter = fields.filter or ""
|
||||||
state.filter = filter
|
state.filter = filter
|
||||||
state.list = make_list(filter)
|
state.list = make_list(filter)
|
||||||
|
|||||||
101
init.lua
101
init.lua
@@ -25,7 +25,6 @@ end
|
|||||||
|
|
||||||
local ACTION = make_logger("action")
|
local ACTION = make_logger("action")
|
||||||
local WARNING = make_logger("warning")
|
local WARNING = make_logger("warning")
|
||||||
local ERROR = make_logger("error")
|
|
||||||
|
|
||||||
local unit_to_secs = {
|
local unit_to_secs = {
|
||||||
s = 1, m = 60, h = 3600,
|
s = 1, m = 60, h = 3600,
|
||||||
@@ -41,39 +40,23 @@ local function parse_time(t) --> secs
|
|||||||
return secs
|
return secs
|
||||||
end
|
end
|
||||||
|
|
||||||
local function concat_keys(t, sep)
|
function xban.find_entry(player, create) --> entry, index
|
||||||
local keys = {}
|
for index, e in ipairs(db) do
|
||||||
for k, _ in pairs(t) do
|
for name in pairs(e.names) do
|
||||||
keys[#keys + 1] = k
|
if name == player then
|
||||||
end
|
return e, index
|
||||||
return table.concat(keys, sep)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- supports wildcard IP pattern (both IPv4 and IPv6)
|
|
||||||
function xban.find_entry(key, create)
|
|
||||||
-- 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
|
|
||||||
local wildcard_prefix = name:match("(.+[.:])%*$")
|
|
||||||
if wildcard_prefix and key:sub(1, #wildcard_prefix) == wildcard_prefix then
|
|
||||||
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
|
||||||
@@ -128,7 +111,7 @@ function xban.ban_player(player, source, expires, reason) --> bool, err
|
|||||||
end
|
end
|
||||||
ACTION("%s bans %s until %s for reason: %s", source, player,
|
ACTION("%s bans %s until %s for reason: %s", source, player,
|
||||||
date, reason)
|
date, reason)
|
||||||
ACTION("Banned Names/IPs: %s", concat_keys(e.names, ", "))
|
ACTION("Banned Names/IPs: %s", table.concat(e.names, ", "))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -148,7 +131,7 @@ function xban.unban_player(player, source) --> bool, err
|
|||||||
e.expires = nil
|
e.expires = nil
|
||||||
e.time = nil
|
e.time = nil
|
||||||
ACTION("%s unbans %s", source, player)
|
ACTION("%s unbans %s", source, player)
|
||||||
ACTION("Unbanned Names/IPs: %s", concat_keys(e.names, ", "))
|
ACTION("Unbanned Names/IPs: %s", table.concat(e.names, ", "))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -201,18 +184,15 @@ function xban.get_record(player)
|
|||||||
end
|
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)
|
||||||
|
|
||||||
@@ -334,7 +314,6 @@ minetest.register_chatcommand("xban_wl", {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
local function check_temp_bans()
|
local function check_temp_bans()
|
||||||
minetest.after(60, check_temp_bans)
|
minetest.after(60, check_temp_bans)
|
||||||
local to_rm = { }
|
local to_rm = { }
|
||||||
@@ -355,12 +334,18 @@ end
|
|||||||
|
|
||||||
local function save_db()
|
local function save_db()
|
||||||
minetest.after(SAVE_INTERVAL, save_db)
|
minetest.after(SAVE_INTERVAL, save_db)
|
||||||
|
local f, e = io.open(DB_FILENAME, "wt")
|
||||||
db.timestamp = os.time()
|
db.timestamp = os.time()
|
||||||
local contents = assert(xban.serialize_db(db))
|
if f then
|
||||||
local ok = minetest.safe_file_write(DB_FILENAME, contents)
|
local ok, err = f:write(xban.serialize(db))
|
||||||
if not ok then
|
if not ok then
|
||||||
ERROR("Unable to save database")
|
WARNING("Unable to save database: %s", err)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
WARNING("Unable to save database: %s", e)
|
||||||
end
|
end
|
||||||
|
if f then f:close() end
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local function load_db()
|
local function load_db()
|
||||||
@@ -374,7 +359,7 @@ local function load_db()
|
|||||||
WARNING("Unable to load database: %s", "Read failed")
|
WARNING("Unable to load database: %s", "Read failed")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local t, e2 = xban.deserialize_db(cont)
|
local t, e2 = minetest.deserialize(cont)
|
||||||
if not t then
|
if not t then
|
||||||
WARNING("Unable to load database: %s",
|
WARNING("Unable to load database: %s",
|
||||||
"Deserialization failed: "..(e2 or "unknown error"))
|
"Deserialization failed: "..(e2 or "unknown error"))
|
||||||
@@ -389,30 +374,6 @@ local function load_db()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_chatcommand("xban_cleanup", {
|
|
||||||
description = "Removes all non-banned entries from the xban db",
|
|
||||||
privs = { server=true },
|
|
||||||
func = function(name, params)
|
|
||||||
local old_count = #db
|
|
||||||
|
|
||||||
local i = 1
|
|
||||||
while i <= #db do
|
|
||||||
if not db[i].banned then
|
|
||||||
-- not banned, remove from db
|
|
||||||
table.remove(db, i)
|
|
||||||
else
|
|
||||||
-- banned, hold entry back
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- save immediately
|
|
||||||
save_db()
|
|
||||||
|
|
||||||
return true, "Removed " .. (old_count - #db) .. " entries, new db entry-count: " .. #db
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_on_shutdown(save_db)
|
minetest.register_on_shutdown(save_db)
|
||||||
minetest.after(SAVE_INTERVAL, save_db)
|
minetest.after(SAVE_INTERVAL, save_db)
|
||||||
load_db()
|
load_db()
|
||||||
@@ -422,3 +383,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.")
|
||||||
|
|||||||
@@ -27,44 +27,5 @@ local function my_serialize_2(t, level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function xban.serialize(t)
|
function xban.serialize(t)
|
||||||
minetest.log("warning", "[xban2] xban.serialize() is deprecated")
|
|
||||||
return "return {\n"..my_serialize_2(t, 1).."\n}"
|
return "return {\n"..my_serialize_2(t, 1).."\n}"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON doesn't allow combined string+number keys, this function moves any
|
|
||||||
-- number keys into an "entries" table
|
|
||||||
function xban.serialize_db(t)
|
|
||||||
local res = {}
|
|
||||||
local entries = {}
|
|
||||||
for k, v in pairs(t) do
|
|
||||||
if type(k) == "number" then
|
|
||||||
entries[k] = v
|
|
||||||
else
|
|
||||||
res[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
res.entries = entries
|
|
||||||
return minetest.write_json(res, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
function xban.deserialize_db(s)
|
|
||||||
if s:sub(1, 1) ~= "{" then
|
|
||||||
-- Load legacy databases
|
|
||||||
return minetest.deserialize(s)
|
|
||||||
end
|
|
||||||
|
|
||||||
local res, err = minetest.parse_json(s)
|
|
||||||
if not res then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Remove all "null"s added by empty tables
|
|
||||||
for i, entry in ipairs(res.entries or {}) do
|
|
||||||
entry.names = entry.names or {}
|
|
||||||
entry.record = entry.record or {}
|
|
||||||
res[i] = entry
|
|
||||||
end
|
|
||||||
res.entries = nil
|
|
||||||
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
|||||||
Reference in New Issue
Block a user