Here's a functional variation of CS2D's subnet banning "technique"
function string.split(t, b)
local cmd = {}
local match = "[^%s]+"
if b then
match = "%w+"
end
if type(b) == "string" then match = "[^"..b.."]+" end
if not t then return invalid() end
for word in string.gmatch(t, match) do
table.insert(cmd, word)
end
return cmd
end
function is(t, tab)
for _,v in ipairs(tab) do if t==v then return true end end
return false
end
local __parse = parse
io.open("p.banlist.lst","a"):close()
_f = io.open("p.banlist.lst","r")
proxy_banlist = _f:read("*all"):split("\n")
_f:close()
_f = nil
io.open("p.whitelist.lst","a"):close()
_f = io.open("p.banlist.lst","r")
whitelist = _f:read("*all"):split("\n")
_f:close()
_f = nil
function ban_ip(ip)
if ip then ip = {ip} else ip = proxy_banlist end
for _ip in ipairs(ip) do
for p in player(0, "table") do
if check_ip(player(p, "ip"), _ip) then
if is(player(p, "usgn"), whitelist) then return end
return __parse("kick "..ip)
end
end
end
end
function check_ip(a, b)
local isip = "[%d*]+[.][%d*]+[.][%d*]+[.][%d*]+"
if not a:find(isip) or not b:find(isip) then return end
bsplit = b:split(".")
for _,part in ipairs(a:split(".")) do
if not (bsplit[_] == part or bsplit[_] == "*" or part == "*") then return false end
end
return true
end
function parse(cmd)
cmds = cmd:split(" ")
if not is (cmds[1],{"banip","unban", "unbanall"}) then return __parse(cmd) end
if cmds[1] == "banip" then
local ip = cmds[2]
local _f = io.open("p.banlist.lst","a")
_f:write(ip.."\n")
table.insert(proxy_banlist, ip)
_f:close()
ban_ip(ip)
elseif cmds[1] == "unbanall" then
io.open("p.banlist.lst","w"):close()
proxy_banlist = {}
return __parse(cmd)
elseif cmds[1] == "unban" then
local key = cmds[2]
if not key:find("[%d*]+[.][%d*]+[.][%d*]+[.][%d*]+") then return __parse(cmd) end
local component = "[%d*]+"
local components = {}
for part in key:gmatch(component) do
table.insert(components, part)
end
retain = {}
for _, ip in ipairs(proxy_banlist) do
local index = 1
local this = true
for part in ip:gmatch(component) do
if (part ~= components[index] and components[index] ~= "*") or (components[index] == "*" and part == "*") then this = false end
index = index + 1
end
print(ip, this)
if not this then table.insert(retain, ip) end
end
_f = io.open("p.banlist.lst","w")
_f:write(table.concat(retain, "\n").."\n")
proxy_banlist = retain
_f:close()
return __parse(cmd)
end
end
addhook("join","proxy_join")
function proxy_join()
ban_ip()
end
Unbanall in server, run this script, close it and open up the newly created "p.whitelist.lst" and add in your whitelists. Note that this will never allow explicit "banip" unless you invoke the __parse function in place of the proxied parse.
Btw, I haven't extensively tested this code since I don't have easy access to CS2D (it's my laptop :P) so I can't be certain that this won't break down suddenly and format your entire computer, but it should hold for most cases. You may want to replace
return __parse("kick "..ip)
with
return __parse("banip "..ip)
in order to decrease the overhead of having to check every newjoiner's IP against the banlist, as now you can kill the
addhook("join","proxy_join")
function proxy_join()
ban_ip()
end
lines at the end, however this will mean that anyone not present at the time of the ban will never be banned...