Skip to content

Instantly share code, notes, and snippets.

@NanoAi
Last active March 15, 2018 06:16
Show Gist options
  • Save NanoAi/08f85fe84e0018ddabaa50998219086b to your computer and use it in GitHub Desktop.
Save NanoAi/08f85fe84e0018ddabaa50998219086b to your computer and use it in GitHub Desktop.
Garry's Mod SQLBans
BanSys = {}
local META = FindMetaTable("Player")
META.OldBan = META.OldBan or META.Ban
local banCache = {}
local function AddBan(id, ip, minutes)
local ip = ip or "0.0.0.0"
local minutes = ( minutes > 0 ) and ( os.time() + (minutes * 60) ) or 0
local key = (id or ip)
banCache[key] = {response = true, time = minutes, when = os.time()}
local query = {
{
str = "CREATE TABLE IF NOT EXISTS `banned_players`( `Index` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `SteamID` VARCHAR(30) NOT NULL, `UserIP` TEXT, `Time` INTEGER);",
args = {}
},
{
str = "DELETE FROM `banned_players` WHERE `SteamID` = '%s' OR `UserIP` = '%s';",
args = {id, ip}
},
{
str = "INSERT INTO `banned_players`( `SteamID`, `UserIP`, `Time`) VALUES( '%s', '%s', %s );",
args = {id, ip, minutes}
}
}
for _, data in ipairs(query) do
if ( sql.Query( string.format( data.str, unpack(data.args) ) ) ) == false then
ErrorNoHalt("SQL: "..data.str.."\n\t"..sql.LastError())
end
end
end
local function RemoveBan(id, ip)
local query = sql.Query( string.format("DELETE FROM `banned_players` WHERE `SteamID` = '%s' OR `UserIP` = '%s';", id, ip) )
local key = (id or ip)
banCache[key] = {response = false, time = 0, when = os.time()}
if ip then
RunConsoleCommand("removeip", ip)
end
if query == false then
error("SQL: "..query.."\n\t"..sql.LastError())
end
end
local function UpdateBan(id, ip, minutes)
local minutes = minutes
local key = (id or ip)
banCache[key] = {response = true, time = minutes, when = os.time()}
if minutes then
minutes = ( minutes > 0 ) and ( os.time() + (minutes * 60) ) or 0
else
minutes = "`Time`"
end
local query = sql.Query( string.format("UPDATE `banned_players` SET `UserIP`='%s', `Time`=%s WHERE SteamID='%s';", ip, minutes, id) )
if query == false then
error("SQL: "..query.."\n\t"..sql.LastError())
end
end
local function IsBanned(id, ip)
local key = (id or ip)
local query = banCache[key] and banCache[key].time
if query == nil then
query = sql.Query( string.format("SELECT `Time` FROM `banned_players` WHERE `SteamID`='%s' OR `UserIP`='%s';", id, ip) )
if query == false then
ErrorNoHalt("SQL: "..query.."\n\t"..sql.LastError())
return false
end
query = query[1].Time
end
local timeLeft = query - os.time()
local response = (timeLeft > 0)
banCache[key] = {response = response, time = query, when = os.time()}
if response then
return true, timeLeft
else
RemoveBan(id, ip)
return false, 0
end
end
function META:Ban(minutes, kick)
local id = self:SteamID64()
local ip = self:IPAddress()
AddBan(id, ip, minutes)
return self:OldBan(minutes, kick)
end
timer.Create("CheckBans", 120, 0, function()
local now = os.time()
for k, data in next, banCache do
if (data.when + 300) < now then
banCache[k] = nil
end
end
end)
hook.Add("CheckPassword", "CheckBans", function(id, ip, _, _, name)
local banned, time = IsBanned(id, ip)
local msg = "You are currently on this servers ban list, try joining again later or find us on https://discord.gg/utpR3gJ for more info!"
if banned then
Msg( string.format("%s (%s)<%s> was kicked because they are on the ban list!\n", name, id, ip) )
timer.Simple(0.01, function()
game.KickID(id, msg)
RunConsoleCommand("addip", time, ip)
end)
return false, msg
end
end)
BanSys.AddBan = AddBan
BanSys.RemoveBan = RemoveBan
BanSys.UpdateBan = UpdateBan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment