HR_Shaft

IP Banning for Phasor v2+ Updated 1/3/2015

Dec 15th, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.71 KB | None | 0 0
  1. -- IP Banning, by H® Shaft
  2. -- Original Creators: AelitePrime and Wizard
  3. -- sendconsoletext overloaded by Nugget
  4. -- From Wizards Commands 2.3 and AelitePrimes Commands 4.3 scripts
  5. -- Extracted & Re-Written by H® Shaft 12/14/2014
  6. -- Updated 1/3/2015 - resolved ban and logging error, resolved issue with new phasor
  7.  
  8. -- Console Commands:    Description:                                      Text Commands:
  9. -- sv_ipban [player#]   IP Bans the specified player                      /ipban [player#]
  10. -- sv_ipbanlist         Shows a list of all IP Bans                       /ipbanlist
  11. -- sv_ipunban [ID#]     Unbans the specified player ip                    /ipunban [ID#]
  12. -- sv_banthisip [ip#]   Bans the specified IP address [xxx.xxx.xxx.xxx]   /banthisip [xxx.xxx.xxx.xxx]
  13. -- (none)               Shows a list of all players and ips               /list
  14.  
  15. -- All IP Bans are saved to your servers cg directory in a file named ipbanlist.txt in
  16. -- format: [Name], [IP Address], [Ban Date]   Example: H® Shaft,101.168.101.101,12/14/2014
  17. -- All console and text actions (and attempts) are recorded in the game log.
  18. -- Note: All IP Bans done by this script result in a permanent ban, until ip-unbanned.
  19.    
  20. cur_players = 0
  21. ip_banid = 1
  22. players_list = {}
  23. ip_banlist = {}
  24.  
  25. function GetRequiredVersion()
  26.     return 200
  27. end
  28.  
  29. function OnScriptLoad(process, game, persistent)
  30.     profilepath = getprofilepath()
  31.    
  32.     for i=0,15 do
  33.         if getplayer(i) then
  34.             cur_players = cur_players + 1
  35.             players_list[i] = {}
  36.         end
  37.     end
  38.  
  39.     local file = io.open(string.format("%s\\ipbanlist.txt", profilepath), "r")
  40.     if file then
  41.         for line in file:lines() do
  42.             local words = tokenizestring(line, ",")
  43.             ip_banlist[ip_banid] = {}
  44.             ip_banlist[ip_banid].name = words[1]
  45.             ip_banlist[ip_banid].ip = words[2]
  46.             ip_banlist[ip_banid].bdate = words[3]
  47.             ip_banid = ip_banid + 1
  48.         end
  49.         file:close()
  50.     end
  51.  
  52. end
  53.  
  54. function OnPlayerJoin(player)
  55.     if getplayer(player) then
  56.         cur_players = cur_players + 1
  57.         players_list[player] = {}
  58.     end
  59. end
  60.  
  61. function OnPlayerLeave(player)
  62.     if getplayer(player) then
  63.         cur_players = cur_players - 1
  64.         players_list[player] = nil
  65.     end
  66. end
  67.  
  68. function OnScriptUnload()
  69.     local file = io.open(profilepath .. "\\ipbanlist.txt", "w")
  70.     for k = 0,ip_banid do
  71.         if ip_banlist[k] then
  72.             file:write(string.format("%s,%s,%s\n", ip_banlist[k].name, ip_banlist[k].ip, ip_banlist[k].bdate))
  73.         end
  74.     end
  75.     file:close()
  76. end
  77.  
  78. function OnServerChat(player, type, message)
  79.     local response = nil
  80.     if player then
  81.         name = getname(player)
  82.         hash = gethash(player)
  83.    
  84.         local t = tokenizecmdstring(message)
  85.         local count = #t
  86.         if t[1] == nil then
  87.             return true
  88.         end
  89.  
  90.         if string.lower(t[1]) == "/ipban" then
  91.             if isadmin(player) then
  92.                 response = false
  93.                 if t[2] and rresolveplayer(t[2]) then
  94.                     local name = getname(rresolveplayer(t[2]))
  95.                     Command_Ipban(player, {t[1], t[2], 2})
  96.                 else
  97.                     sendresponse("Invalid Syntax: /ipban [player #]", "/", player)
  98.                 end                    
  99.             else
  100.                 response = true
  101.                 sendresponse("You're not an admin. Logging attempt.", "/", player)
  102.                 log_msg(1, getname(player) .. " attempted use of the /ipban command.")
  103.             end
  104.         end
  105.        
  106.         if string.lower(t[1]) == "/ipunban" then
  107.             if isadmin(player) then
  108.                 response = false
  109.                 if t[2] then
  110.                     if tonumber(t[2]) < ip_banid then
  111.                         Command_Ipunban(player, t[1], t[2], 2)
  112.                     end
  113.                 else
  114.                     sendresponse("Invalid Syntax: /ipunban [player #]", "/", player)
  115.                 end                    
  116.             else
  117.                 response = true
  118.                 sendresponse("You're not an admin. Logging attempt.", "/", player)
  119.                 log_msg(1, getname(player) .. " attempted use of the /ipunban command.")
  120.             end
  121.         end
  122.  
  123.         if string.lower(t[1]) == "/ipbanlist" then
  124.             if isadmin(player) then
  125.                 sendconsoletext(player, "[ID |   Name   |   IP    |  Banned On ]", 10, 0)
  126.                 local bool = true
  127.                 for i = 0, ip_banid-1 do
  128.                     if ip_banlist[i] then
  129.                         bool = false
  130.                         sendconsoletext(player, i ..  " | " .. ip_banlist[i].name .. " | " .. ip_banlist[i].ip .. " | " .. ip_banlist[i].bdate, 10, 0)
  131.                     end
  132.                 end
  133.                 if bool then sendconsoletext(player, "IP Ban list is empty", 5, 0) end
  134.                 log_msg(1, getname(player) .. " used the ipbanlist command.")
  135.             else
  136.                 response = true
  137.                 sendresponse("You're not an admin. Logging attempt.", "/", player)
  138.                 log_msg(1, getname(player) .. " attempted use of the /ipunban command.")
  139.             end
  140.         end        
  141.        
  142.         if string.lower(t[1]) == "/banthisip" then
  143.             if isadmin(player) then
  144.                 response = false
  145.                 if t[2] then
  146.                     Command_BanThisIP(player, t[1], t[2], 2)
  147.                     log_msg(1, getname(player) .. " Banned the IP: " .. t[2])
  148.                 else
  149.                     sendresponse("Invalid Syntax: /banthisip [xxx.xxx.xxx.xxx]", "/", player)
  150.                 end                    
  151.             else
  152.                 response = true
  153.                 sendresponse("You're not an admin. Logging attempt.", "/", player)
  154.                 log_msg(1, getname(player) .. " attempted use of the /banthisip command.")
  155.             end
  156.         end    
  157.        
  158.         if string.lower(t[1]) == "/list" then
  159.             if isadmin(player) then
  160.                 response = false
  161.                 sendconsoletext(player, "[ID# - Name - IP]", 10, 0)
  162.                 for i = 0,15 do
  163.                     if getplayer(i) then
  164.                         local name = getname(i)
  165.                         local id = resolveplayer(i)
  166.                         local ip = getip(i)
  167.                         sendconsoletext(player, id .. " - " .. name .. " - " .. ip, 10, 0) 
  168.                     end
  169.                 end
  170.                 log_msg(1, getname(player) .. " used the list command.")   
  171.             else
  172.                 response = true
  173.                 sendresponse("You're not an admin. Logging attempt.", "/", player)
  174.                 log_msg(1, getname(player) .. " attempted use of the /list command.")
  175.             end
  176.         end    
  177.    
  178.     end
  179.     return response
  180. end
  181.  
  182. function OnBanCheck(hash, ip)
  183.     local response = nil
  184.     for k,v in pairs(ip_banlist) do
  185.         if ip_banlist[k] and ip_banlist[k].ip == ip then
  186.             response = false
  187.         end
  188.     end
  189.     return response
  190. end
  191.  
  192. function OnServerCommand(player, command)
  193.     local response = nil
  194.     local temp = tokenizecmdstring(command)
  195.     local cmd = temp[1]
  196.     t = tokenizecmdstring(command)
  197.     count = #t
  198.     if t[1] == "sv_ipban" then
  199.         response = false
  200.         Command_Ipban(player, t[1], t[2], count)
  201.     elseif t[1] == "sv_ipbanlist" then
  202.         response = false
  203.         Command_Ipbanlist(player, t[1], count)
  204.     elseif t[1] == "sv_ipunban" then
  205.         response = false
  206.         Command_Ipunban(player, t[1], t[2], count)
  207.     elseif t[1] == "sv_banthisip" then
  208.         response = false
  209.         Command_BanThisIP(player, t[1], t[2], count)   
  210.     end
  211.     return response
  212. end
  213.  
  214. function Command_Ipban(executor, arg)
  215.     if arg[1] ~= nil and arg[2] ~= nil and arg[1] and arg[2] then
  216.         local command = arg[1]
  217.         local player = tonumber(arg[2])
  218.         local count = arg[3]   
  219.         if count == 2 then
  220.             if player then
  221.                 local ipbanlist = io.open(string.format("%s\\ipbanlist.txt", profilepath), "a")
  222.                 local id = rresolveplayer(player)
  223.                 local bandate = os.date("%m/%d/%Y")
  224.                 ip_banlist[player] = {}
  225.                 ip_banlist[player].name = getname(id)
  226.                 ip_banlist[player].ip = getip(id)
  227.                 ip_banlist[player].bdate = bandate     
  228.                 ipbanlist:write(string.format("%s,%s,%s\n", getname(id), getip(id), bandate))
  229.                 ipbanlist:close()
  230.                 Ipban(rresolveplayer(player))
  231.                 say(getname(id) .. " was IP banned from the server!")
  232.                 sendresponse(getname(id) .. " has been IP banned from the server!", "/", executor)
  233.                 log_msg(1, getname(executor) .. " IP Banned " .. getname(id) .. " (player id#" .. resolveplayer(id) .. ")")
  234.             else
  235.                 sendresponse("Invalid Player!", command, executor)
  236.             end
  237.         else
  238.             sendresponse("Invalid Syntax: " .. command .. " [player]", command, executor)
  239.         end
  240.     end
  241. end
  242.  
  243. function Command_BanThisIP(executor, command, ip, count)
  244.     if count == 2 then 
  245.         for i = 1, 1 do
  246.             local name = "UNKNOWN"
  247.             local bandate = os.date("%m/%d/%Y")
  248.             ip_banid = ip_banid + 1
  249.             ip_banlist[ip_banid] = {}
  250.             ip_banlist[ip_banid].name = name
  251.             ip_banlist[ip_banid].ip = ip
  252.             ip_banlist[ip_banid].bdate = bandate       
  253.             registertimer(0, "RefreshIPBanlist")
  254.         end
  255.         sendresponse(ip .. " was added to the IP ban list, name listed as UNKNOWN!", "/", executor)
  256.         log_msg(1, getname(executor) .. " used the banthisip command.")
  257.         registertimer(0, "RefreshIPBanlist")       
  258.     else
  259.         sendresponse("Invalid Syntax: " .. command .. " [xxx.xxx.xxx.xxx]", command, executor)
  260.     end    
  261. end
  262.  
  263. function Command_Ipbanlist(executor, command, count)
  264.     if count == 1 then
  265.         local response = "[ID |   Name   |   IP    |  Banned On ]\n"
  266.         local bool = true
  267.         for i = 0, ip_banid-1 do
  268.             if ip_banlist[i] then
  269.                 bool = false
  270.                 response = response .. i ..  " | " .. ip_banlist[i].name .. " | " .. ip_banlist[i].ip .. " | " .. ip_banlist[i].bdate .. "\n"
  271.             end
  272.         end
  273.         if bool then response = "IP Ban list is empty" end
  274.         log_msg(1, getname(executor) .. " used the ipbanlist command.")
  275.         sendresponse(response, command, executor)
  276.     else
  277.         sendresponse("Invalid Syntax: " .. command, command, executor)
  278.     end
  279. end
  280.  
  281. function Command_Ipunban(executor, command, ID, count)
  282.     if count == 2 and tonumber(ID) then
  283.         ID = tonumber(ID)
  284.         local response = "Invalid ID"
  285.         if ID < ip_banid then
  286.             response = ip_banlist[ID].ip .. " has been un-ipbanned."
  287.             table.remove(ip_banlist, ID)
  288.             log_msg(1, getname(executor) .. " IP Un-Banned player id# " .. ID)
  289.             sendresponse(response, command, executor)
  290.             registertimer(0, "RefreshIPBanlist")
  291.         else
  292.             sendresponse(response, command, executor)
  293.         end
  294.     else
  295.         sendresponse("Invalid Syntax: " .. command .. " [ID]", command, executor)
  296.     end
  297. end
  298.  
  299. function Ipban(player)
  300.     svcmd("sv_ban " .. resolveplayer(player))
  301.     local file = io.open(profilepath .. "\\banned.txt", "r")
  302.     if file then
  303.         local Lines = {}
  304.         for line in file:lines() do
  305.             if line and line ~= "" then
  306.                 table.insert(Lines, line)
  307.             end
  308.         end
  309.         svcmd("sv_unban " .. #Lines - 2)
  310.         file:close()
  311.     end
  312. end
  313.  
  314. function RefreshIPBanlist(id, count)
  315.     local writefile = io.open(profilepath .. "\\ipbanlist.txt", "w")
  316.     if writefile then
  317.         for k = 0,ip_banid do
  318.             if ip_banlist[k] then
  319.                 writefile:write(string.format("%s,%s,%s\n", ip_banlist[k].name, ip_banlist[k].ip, ip_banlist[k].bdate))
  320.             end
  321.         end
  322.         writefile:close()
  323.     end
  324.     ip_banlist = {}
  325.     ip_banid = 1
  326.     local readfile = io.open(string.format("%s\\ipbanlist.txt", profilepath), "r")
  327.     if readfile then
  328.         for line in readfile:lines() do
  329.             local words = tokenizestring(line, ",")
  330.             ip_banlist[ip_banid] = {}
  331.             ip_banlist[ip_banid].name = words[1]
  332.             ip_banlist[ip_banid].ip = words[2]
  333.             ip_banlist[ip_banid].bdate = words[3]
  334.             ip_banid = ip_banid + 1
  335.         end
  336.         readfile:close()
  337.     end    
  338.     return false   
  339. end
  340.  
  341. function getvalidplayers()
  342.     if cur_players ~= 0 then
  343.         local players = {}
  344.         for i = 0,15 do
  345.             if getplayer(i) then
  346.                 table.insert(players, i)
  347.             end
  348.         end    
  349.         if players[1] then
  350.             return players
  351.         end
  352.     end
  353.     return false
  354. end
  355.  
  356. function delayMsg(id, count, arguments)
  357.     local msg = arguments[1]
  358.     local command = tostring(arguments[2])
  359.     local player = arguments[3]
  360.     if string.sub(command, 1, 1) == "/" then
  361.         say(msg)
  362.     elseif string.sub(command, 1, 1) == "\\" then
  363.         privatesay(player, msg)
  364.     elseif player ~= nil and player ~= -1 then
  365.         sendconsoletext(player, msg, 5, 0)
  366.     end
  367.     return false
  368. end
  369.  
  370. function sendresponse(message, command, player, log)
  371.     local function sendmessage(msg, cmd, pl, logBool)
  372.         local playerCheck = ( tonumber(pl) and pl ~= nil and pl ~= -1 and pl >= 0 and pl < 16 )
  373.         if string.sub(cmd, 1, 1) == "/" then
  374.             registertimer(0, "delayMsg", {msg, cmd, pl} )
  375.         elseif string.sub(cmd, 1, 1) == "\\" then
  376.             registertimer(0, "delayMsg", {msg, cmd, pl} )
  377.         elseif playerCheck then
  378.             sendconsoletext(pl, msg, 5, 0)
  379.         end
  380.         hprintf(msg)
  381.         if logBool ~= false and playerCheck then
  382.             cmdlog("Response to " .. getname(pl) .. ": " .. msg)
  383.         end
  384.     end
  385.     if message == nil then
  386.         hprintf("Internal Error Occured Check the Command Script Errors log")
  387.         cmderrors("Error Occured at sendresponse: No message ")
  388.         return
  389.     end
  390.     if command == nil then
  391.         hprintf("Internal Error Occured Check the Command Script Errors log")
  392.         cmderrors("Error Occured at sendresponse: No Command Helpful info: " .. message)
  393.         return
  394.     end
  395.     if type(message) == "table" then
  396.         for i = 0, #message do
  397.             if message[i] then
  398.                 sendmessage(message[i] , command, player , log)
  399.             end
  400.         end
  401.     elseif message ~= "" then
  402.         sendmessage(message, command, player, log)
  403.     end
  404. end
  405.  
  406. function cmderrors(message)
  407.     WriteLog(profilepath .. "\\logs\\IP_Ban_Errors.log", tostring(message))
  408. end
  409.  
  410. function cmdlog(message)
  411.     WriteLog(profilepath .. "\\logs\\IP_Ban_commands.log",tostring(message))
  412. end
  413.  
  414. function WriteLog(filename, value)
  415.     local file = io.open(filename, "a")
  416.     if file then
  417.         file:write(string.format("%s\t%s\n", os.date("%Y:%m:%d:%H:%M:%S"), tostring(value)))
  418.         file:close()
  419.     end
  420. end
  421.  
  422. -- Start sendconsoletext overloaded by Nugget
  423. console = {}
  424. console.__index = console
  425. consoletimer = registertimer(100, "ConsoleTimer")
  426. phasor_sendconsoletext = sendconsoletext
  427.  
  428. function sendconsoletext(player, message, time, order, align, height, func)
  429.     if player then
  430.         console[player] = console[player] or {}
  431.         local temp = {}
  432.         temp.player = player
  433.         temp.id = nextid(player, order)
  434.         temp.message = message or ""
  435.         temp.time = time or 0.7
  436.         temp.remain = temp.time
  437.         temp.align = align or "left"
  438.         temp.height = height or 0
  439.         if type(func) == "function" then
  440.             temp.func = func
  441.         elseif type(func) == "string" then
  442.             temp.func = _G[func]
  443.         end
  444.         console[player][temp.id] = temp
  445.         setmetatable(console[player][temp.id], console)
  446.         return console[player][temp.id]
  447.     end
  448. end
  449.  
  450. function nextid(player, order)
  451.     if not order then
  452.         local x = 0
  453.         for k,v in pairs(console[player]) do
  454.             if k > x + 1 then
  455.                 return x + 1
  456.             end
  457.             x = x + 1
  458.         end
  459.         return x + 1
  460.     else
  461.         local original = order
  462.         while console[player][order] do
  463.             order = order + 0.001
  464.             if order == original + 0.999 then break end
  465.         end
  466.         return order
  467.     end
  468. end
  469.  
  470. function getmessage(player, order)
  471.     if console[player] then
  472.         if order then
  473.             return console[player][order]
  474.         end
  475.     end
  476. end
  477.  
  478. function getmessages(player)
  479.     return console[player]
  480. end
  481.  
  482. function getmessageblock(player, order)
  483.     local temp = {}
  484.     for k,v in opairs(console[player]) do
  485.         if k >= order and k < order + 1 then
  486.             table.insert(temp, console[player][k])
  487.         end
  488.     end
  489.     return temp
  490. end
  491.  
  492. function console:getmessage()
  493.     return self.message
  494. end
  495.  
  496. function console:append(message, reset)
  497.     if console[self.player] then
  498.         if console[self.player][self.id] then
  499.             if getplayer(self.player) then
  500.                 if reset then
  501.                     if reset == true then
  502.                         console[self.player][self.id].remain = console[self.player][self.id].time
  503.                     elseif tonumber(reset) then
  504.                         console[self.player][self.id].time = tonumber(reset)
  505.                         console[self.player][self.id].remain = tonumber(reset)
  506.                     end
  507.                 end
  508.                 console[self.player][self.id].message = message or ""
  509.                 return true
  510.             end
  511.         end
  512.     end
  513. end
  514.  
  515. function console:shift(order)
  516.     local temp = console[self.player][self.id]
  517.     console[self.player][self.id] = console[self.player][order]
  518.     console[self.player][order] = temp
  519. end
  520.  
  521. function console:pause(time)
  522.     console[self.player][self.id].pausetime = time or 5
  523. end
  524.  
  525. function console:delete()
  526.     console[self.player][self.id] = nil
  527. end
  528.  
  529. function ConsoleTimer(id, count)
  530.     for i,_ in opairs(console) do
  531.         if tonumber(i) then
  532.             if getplayer(i) then
  533.                 for k,v in opairs(console[i]) do
  534.                     if console[i][k].pausetime then
  535.                         console[i][k].pausetime = console[i][k].pausetime - 0.1
  536.                         if console[i][k].pausetime <= 0 then
  537.                             console[i][k].pausetime = nil
  538.                         end
  539.                     else
  540.                         if console[i][k].func then
  541.                             if not console[i][k].func(i) then
  542.                                 console[i][k] = nil
  543.                             end
  544.                         end
  545.                         if console[i][k] then
  546.                             console[i][k].remain = console[i][k].remain - 0.1
  547.                             if console[i][k].remain <= 0 then
  548.                                 console[i][k] = nil
  549.                             end
  550.                         end
  551.                     end
  552.                 end
  553.                 if table.len(console[i]) > 0 then
  554.                     local paused = 0
  555.                     for k,v in pairs(console[i]) do
  556.                         if console[i][k].pausetime then
  557.                             paused = paused + 1
  558.                         end
  559.                     end
  560.                     if paused < table.len(console[i]) then
  561.                         local str = ""
  562.                         for i = 0,30 do
  563.                             str = str .. " \n"
  564.                         end
  565.                         phasor_sendconsoletext(i, str)
  566.                         for k,v in opairs(console[i]) do
  567.                             if not console[i][k].pausetime then
  568.                                 if console[i][k].align == "right" or console[i][k].align == "center" then
  569.                                     phasor_sendconsoletext(i, consolecenter(string.sub(console[i][k].message, 1, 78)))
  570.                                 else
  571.                                     phasor_sendconsoletext(i, string.sub(console[i][k].message, 1, 78))
  572.                                 end
  573.                             end
  574.                         end
  575.                     end
  576.                 end
  577.             else
  578.                 console[i] = nil
  579.             end
  580.         end
  581.     end
  582.    
  583.     return true
  584. end
  585.  
  586. function consolecenter(text)
  587.     if text then
  588.         local len = string.len(text)
  589.         for i = len + 1, 78 do
  590.             text = " " .. text
  591.         end
  592.         return text
  593.     end
  594. end
  595.  
  596. function opairs(t)
  597.     local keys = {}
  598.     for k,v in pairs(t) do
  599.         table.insert(keys, k)
  600.     end    
  601.     table.sort(keys,
  602.     function(a,b)
  603.         if type(a) == "number" and type(b) == "number" then
  604.             return a < b
  605.         end
  606.         an = string.lower(tostring(a))
  607.         bn = string.lower(tostring(b))
  608.         if an ~= bn then
  609.             return an < bn
  610.         else
  611.             return tostring(a) < tostring(b)
  612.         end
  613.     end)
  614.     local count = 1
  615.     return function()
  616.         if table.unpack(keys) then
  617.             local key = keys[count]
  618.             local value = t[key]
  619.             count = count + 1
  620.             return key,value
  621.         end
  622.     end
  623. end
  624.  
  625. function table.len(t)
  626.     local count = 0
  627.     for k,v in pairs(t) do
  628.         count = count + 1
  629.     end
  630.     return count
  631. end
  632. -- Stop sendconsoletext overloaded
  633.  
  634. -- Created by H® Shaft thank you to Oxide, AelitePrime, Nugget & Wizard.
  635. -- Visit http://halorace.org/forum/index.php?topic=514.0 or
  636. -- Visit http://pastebin.com/u/HR_Shaft for more phasor scripts
Advertisement
Add Comment
Please, Sign In to add comment