Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Oxide Rust plugin for a server whitelist. Has not been tested, may crash.
PLUGIN.Title = "User Whitelist"
PLUGIN.Description = "Allows admin to grant access to specific users"
PLUGIN.Author = "Monstrado"
function PLUGIN:Init()
self.ServerWhitelistDataFile = datafile("server_whitelist")
self:RefreshWhitelist()
self:AddChatCommand("whitelist", self.cmdWhitelist)
self:AddChatCommand("blacklist", self.cmdBlacklist)
end
-- Check for user in whitelist
--- Returns position in array
function PLUGIN:inWhitelist(whitelistUserId)
-- Refresh whitelist, to make sure admin hasn't added users manually
-- to the whitelist file
self:RefreshWhitelist()
for pos, userId in pairs(self.ServerWhitelistData) do
if userId == whitelistUserId then return pos end
end
return false
end
-- Check the size of the whitelist
function PLUGIN:getWhitelistSize()
return table.getn(self.ServerWhitelistData)
end
-- Add user to whitelist
function PLUGIN:addToWhitelist(whitelistUserId)
if self:inWhitelist(whitelistUserId) then
return false
end
table.insert(self.ServerWhitelistData, tostring(whitelistUserId))
return true
end
-- Remove user from whitelist
function PLUGIN:removeFromWhitelist(whitelistUserId)
local userIdPos = self:inWhitelist(whitelistUserId)
if not userIdPos then
return false
end
table.remove(self.ServerWhitelistData, userIdPos)
return true
end
--
-- Save whitelist state
function PLUGIN:Save()
self.ServerWhitelistDataFile:SetText(json.encode(self.ServerWhitelistData))
self.ServerWhitelistDataFile:Save()
end
--
-- Reread whitelist state
function PLUGIN:RefreshWhitelist()
local txt = self.ServerWhitelistDataFile:GetText()
if (txt ~= "") then
self.ServerWhitelistData = json.decode(txt)
else
self.ServerWhitelistData = {}
end
end
-- Performs Whitelist/Blacklist Operations
-- Takes an action argument
function PLUGIN:performAction(netuser, args, action)
-- TODO: Actually check the validity of the supplied steam id, I don't know the APIs so
-- I'm leaving that out for now
local targetUserId = args[1]
-- Whitelist
if action == "whitelist" then
if self:addToWhitelist(targetUserId) then
rust. Notice(netuser, "User added to the server whitelist")
else
rust.Notice(netuser, "User already in whitelist")
return
end
-- Blacklist
elseif action == "blacklist" then
if self:removeFromWhitelist(targetUserId) then
rust.Notice(netuser, "User removed from the server whitelist")
else
rust.Notice(netuser, "User not found in whitelist")
return
end
-- Discard all other actions
else
rust.Notice(netuser, "Internal server error, no action supplied")
return
end
self:Save()
end
-- Whitelist command plugin
-- usage: /whitelist <steam_id>
function PLUGIN:cmdWhitelist(netuser, cmd, args)
if (not args[1]) then
rust.Notice(netuser, "Syntax: /whitelist <steam_id>")
return
end
self:performAction(netuser, args, "whitelist")
end
-- Blacklist command plugin
-- usage: /blacklist <steam_id>
function PLUGIN:cmdBlacklist(netuser, cmd, args)
if (not args[1]) then
rust.Notice(netuser, "Syntax: /blacklist <steam_id>")
return
end
self:performAction(netuser, args, "blacklist")
end
-- Enforce whitelist
local SteamIDField = field_get(RustFirstPass.SteamLogin, "SteamID", true)
function PLUGIN:CanClientLogin(login)
-- I need the steamID64, not sure how to get that.
local steamlogin = login.SteamLogin
local steamId = tostring(SteamIDField(steamlogin))
--
-- Debug shit for now
print("User with steamId: " .. steamId .. " is joining the server...")
rust.BroadcastChat("User with steamId: " .. steamId .. " is joining the server...")
--
-- Check if the steamId is in the whitelist
if not self:inWhitelist(tostring(steamId)) then
return NetError.Facepunch_Kick_RCON
end
end
-- Help text
function PLUGIN:SendHelpText(netuser)
rust.SendChatToUser(netuser, "Use /whitelist and /blacklist to add and remove users from the whitelist")
end
-- Generic split function found on the internet
-- http://coronalabs.com/blog/2013/04/16/lua-string-magic/
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
-- Convert SteamID to Community ID (aka: steamid64)
-- TODO: Make this shit work with the way Rust is providing the SteamID
-- (STEAM_1:<ID>:0) <-- still not sure how to determine the auth code!
function PLUGIN:SteamIdtoCommunityId(sid)
local matchData = sid:split(":")
if not matchData[3] then
return
end
local id = sid:split(":") id = (((id[3] * 2) + id[2]) + 1197960265728) id = "7656" .. id
return id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.