Jump to content

Whitelist System (WEB API)


DaveHUN

Recommended Posts

local serial = getPlayerSerial (getLocalPlayer())
local allowedSerials = { -- List of allowed serials
    "12345678901234567890123456789012",
    "23456789012345678901234567890123",
    "34567890123456789012345678901234"
}

function isSerialAllowed(serial)
    for i, allowedSerial in ipairs(allowedSerials) do
        if serial == allowedSerial then
            return true
        end
    end
    return false
end

addEventHandler("onClientResourceStart", resourceRoot,
    function()
        if not isSerialAllowed(serial) then
            -- Kick the player if their serial is not allowed
            kickPlayer("Your serial is not on the whitelist.")
        else
            -- Connect to the server if the serial is allowed
            -- Replace "server.example.com" with the address of your server
            triggerServerEvent("connectToServer", resourceRoot, "server.example.com")
        end
    end
)

 

addEvent("connectToServer", true)
addEventHandler("connectToServer", root,
    function(serverAddress)
        -- Connect the player to the specified server
        -- Replace "serverPassword" with the password for your server
        connect(serverAddress, "serverPassword")
    end
)

 

Link to comment
1 hour ago, FLUSHBICEPS said:
local serial = getPlayerSerial (getLocalPlayer())
local allowedSerials = { -- List of allowed serials
    "12345678901234567890123456789012",
    "23456789012345678901234567890123",
    "34567890123456789012345678901234"
}

function isSerialAllowed(serial)
    for i, allowedSerial in ipairs(allowedSerials) do
        if serial == allowedSerial then
            return true
        end
    end
    return false
end

addEventHandler("onClientResourceStart", resourceRoot,
    function()
        if not isSerialAllowed(serial) then
            -- Kick the player if their serial is not allowed
            kickPlayer("Your serial is not on the whitelist.")
        else
            -- Connect to the server if the serial is allowed
            -- Replace "server.example.com" with the address of your server
            triggerServerEvent("connectToServer", resourceRoot, "server.example.com")
        end
    end
)

 

addEvent("connectToServer", true)
addEventHandler("connectToServer", root,
    function(serverAddress)
        -- Connect the player to the specified server
        -- Replace "serverPassword" with the password for your server
        connect(serverAddress, "serverPassword")
    end
)

 

not what I meant
I would like you to download the serials from a website
eg testpage.com/serials.txt
and if it is included, it will connect

 

Link to comment
23 hours ago, DaveHUN said:

not what I meant
I would like you to download the serials from a website
eg testpage.com/serials.txt
and if it is included, it will connect

 

hi welcome to forum, i have prepared and tested a code for you, it works,and add it to the whitelist.json one below the other like this,If you have more problems, you can open a new thread.

>> Lua <<

local WHITELIST_API = "http://localhost/whitelist.json" --change with your url

addEventHandler("onPlayerJoin", root,
   function()
      local player_ = source
      callRemote(WHITELIST_API, 
      function(response)
         if(not isElement(player_)) then return end
         if(response == "ERROR") then return end 
         local playerSerial = getPlayerSerial(player_)

         for _,serial in ipairs(response) do
            if(playerSerial == serial) then return end
         end

         kickPlayer(player_, "You are not authorized to access this server")
      end, player_)
   end
)

addEventHandler("onResourceStart", resourceRoot,
   function()
      local players = getElementsByType("player")
      callRemote(WHITELIST_API, 
      function(response)
         if(response == "ERROR") then return end

         for _,player in ipairs(players) do
            if(isElement(player)) then 
              local playerSerial = getPlayerSerial(player)

              for _,serial in ipairs(response) do
                 if(playerSerial == serial) then return end
              end

              kickPlayer(player, "You are not authorized to access this server")
            end
         end

      end, players)
   end
)

>> whitelist.json <<
 

[ [ 
"123", --add serial you want to access serial
"321",--add serial you want to access serial
"132" --add serial you want to access serial
] ]

 

Edited by Shady1
Link to comment
8 hours ago, Shady1 said:

hi welcome to forum, i have prepared and tested a code for you, it works,and add it to the whitelist.json one below the other like this,If you have more problems, you can open a new thread.

>> Lua <<

local WHITELIST_API = "http://localhost/whitelist.json" --change with your url

addEventHandler("onPlayerJoin", root,
   function()
      local player_ = source
      callRemote(WHITELIST_API, 
      function(response)
         if(not isElement(player_)) then return end
         if(response == "ERROR") then return end 
         local playerSerial = getPlayerSerial(player_)

         for _,serial in ipairs(response) do
            if(playerSerial == serial) then return end
         end

         kickPlayer(player_, "You are not authorized to access this server")
      end, player_)
   end
)

addEventHandler("onResourceStart", resourceRoot,
   function()
      local players = getElementsByType("player")
      callRemote(WHITELIST_API, 
      function(response)
         if(response == "ERROR") then return end

         for _,player in ipairs(players) do
            if(isElement(player)) then 
              local playerSerial = getPlayerSerial(player)

              for _,serial in ipairs(response) do
                 if(playerSerial == serial) then return end
              end

              kickPlayer(player, "You are not authorized to access this server")
            end
         end

      end, players)
   end
)

>> whitelist.json <<
 

[ [ 
"123", --add serial you want to access serial
"321",--add serial you want to access serial
"132" --add serial you want to access serial
] ]

 

and it could be resolved that only the series would be included in the file

 

Link to comment

I'd point out @Shady1's solution does not scale well as the whitelist gets bigger.

It would be more ideal to, on start-up, and then periodically (or on command like refreshwhitelist) download the list, store it in a Lua table in a [serial] = bool structure and look-up that table onPlayerConnect, and cancelEvent(true) if serial not present (which also benefits the server process as a good part of player join sequence is aborted earlier, including the construction of a player element). This way you don't download the whole list every time a player joins, parse it every time a player joins, and iterate thought it every time a player joins. Lua table look-ups benefit from hashing, leading to much faster lookup speeds than iterating thought the list, provided the list is large enough (could be as little as 5 entries for benefits to show up but I haven't tested it) (otherwise hashing could be more expensive).

Edited by Addlibs
  • Like 1
Link to comment
21 minutes ago, Addlibs said:

I'd point out @Shady1's solution does not scale well as the whitelist gets bigger.

It would be more ideal to, on start-up, and then periodically (or on command like refreshwhitelist) download the list, store it in a Lua table in a [serial] = bool structure and look-up that table onPlayerConnect, and cancelEvent(true) if serial not present (which also benefits the server process as a good part of player join sequence is aborted earlier, including the construction of a player element). This way you don't download the whole list every time a player joins, parse it every time a player joins, and iterate thought it every time a player joins. Lua table look-ups benefit from hashing, leading to much faster lookup speeds than iterating thought the list, provided the list is large enough (could be as little as 5 entries for benefits to show up but I haven't tested it) (otherwise hashing could be more expensive).

Upvoting. This way you can indeed do cancelEvent (true, "You are not whitelisted") onPlayerConnect and this will effectively prevent the player from connecting with that custom message. This is prettier than kickPlayer which you are doing with a certain delay by getting the result of callRemote.

Link to comment
54 minutes ago, Addlibs said:

I'd point out @Shady1's solution does not scale well as the whitelist gets bigger.

It would be more ideal to, on start-up, and then periodically (or on command like refreshwhitelist) download the list, store it in a Lua table in a [serial] = bool structure and look-up that table onPlayerConnect, and cancelEvent(true) if serial not present (which also benefits the server process as a good part of player join sequence is aborted earlier, including the construction of a player element). This way you don't download the whole list every time a player joins, parse it every time a player joins, and iterate thought it every time a player joins. Lua table look-ups benefit from hashing, leading to much faster lookup speeds than iterating thought the list, provided the list is large enough (could be as little as 5 entries for benefits to show up but I haven't tested it) (otherwise hashing could be more expensive).


you're right, I thought about that, but I chose this way to keep the list updated.
Lua

local WHITELIST_API = "http://localhost/whitelist.json" --change with your url
local serialWhitelist = {}

function updateWhitelist()
   for serial,_ in pairs(serialWhitelist) do
      serialWhitelist[serial] = nil
   end
   callRemote(WHITELIST_API, 
      function(response)
      if(response == "ERROR") then outputDebugString("Unable to get to Whitelist File.", 1) return end
      for _,serial in ipairs(response) do
         serialWhitelist[serial] = true
      end
      outputDebugString("Whitelist Updated Succesfuly!")
      
      for _,player in ipairs(getElementsByType("player")) do
         if(isElement(player)) then
            local playerSerial = getPlayerSerial(player)
            if(not serialWhitelist[playerSerial]) then
               kickPlayer(player, "You are not authorized to access this server")
            end
         end
      end
     
   end)
end

addEventHandler("onResourceStart", resourceRoot,
   function()
      callRemote(WHITELIST_API, 
      function(response)
         if(response == "ERROR") then return outputDebugString("Unable to get to Whitelist File.", 1) end
         for _,serial in ipairs(response) do
            serialWhitelist[serial] = true
         end

         for _,player in ipairs(getElementsByType("player")) do
            if(isElement(player)) then
               local playerSerial = getPlayerSerial(player)
               if(not serialWhitelist[playerSerial]) then
                  kickPlayer(player, "You are not authorized to access this server")
               end
            end
         end

      end)
   end
)

addEventHandler("onPlayerConnect", root,
   function(_, _, _, playerSerial)
      if(not serialWhitelist[playerSerial]) then
         cancelEvent(true, "You are not authorized to access this server")
      end
   end
)


addCommandHandler("updatelist", function()
   updateWhitelist()
end) 

 

On 11/02/2023 at 18:50, DaveHUN said:

I want a script that queries the serial from a specific web, if the serial is there, it can connect to the server, essentially a whitelist system

if you like my help, you can like my comment

Edited by Shady1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...