feat(core): secure events

This commit is contained in:
Kasey FItton
2024-11-02 20:04:45 +00:00
parent 87d55a2b0f
commit 2bc9f83ffb
11 changed files with 65 additions and 47 deletions
+21 -3
View File
@@ -1,11 +1,29 @@
---@return boolean
function ESX.IsPlayerLoaded() function ESX.IsPlayerLoaded()
return ESX.PlayerLoaded return ESX.PlayerLoaded
end end
---@return table
function ESX.GetPlayerData() function ESX.GetPlayerData()
return ESX.PlayerData return ESX.PlayerData
end end
---@param name string
---@param func function
---@return nil
function ESX.SecureNetEvent(name, func)
RegisterNetEvent(name, function()
if source == '' then
return
end
local success, result = pcall(func)
if not success then
error(("%s"):format(result))
end
end)
end
local addonResourcesState = { local addonResourcesState = {
['esx_progressbar'] = GetResourceState('esx_progressbar') ~= 'missing', ['esx_progressbar'] = GetResourceState('esx_progressbar') ~= 'missing',
['esx_notify'] = GetResourceState('esx_notify') ~= 'missing', ['esx_notify'] = GetResourceState('esx_notify') ~= 'missing',
@@ -1485,11 +1503,11 @@ function ESX.ShowInventory()
end) end)
end end
RegisterNetEvent('esx:showNotification', ESX.ShowNotification) ESX.SecureNetEvent('esx:showNotification', ESX.ShowNotification)
RegisterNetEvent('esx:showAdvancedNotification', ESX.ShowAdvancedNotification) ESX.SecureNetEvent('esx:showAdvancedNotification', ESX.ShowAdvancedNotification)
RegisterNetEvent('esx:showHelpNotification', ESX.ShowHelpNotification) ESX.SecureNetEvent('esx:showHelpNotification', ESX.ShowHelpNotification)
AddEventHandler("onResourceStop", function(resourceName) AddEventHandler("onResourceStop", function(resourceName)
for i = 1, #ESX.UI.Menu.Opened, 1 do for i = 1, #ESX.UI.Menu.Opened, 1 do
+22 -22
View File
@@ -1,6 +1,6 @@
local pickups = {} local pickups = {}
RegisterNetEvent("esx:requestModel", function(model) ESX.SecureNetEvent("esx:requestModel", function(model)
ESX.Streaming.RequestModel(model) ESX.Streaming.RequestModel(model)
end) end)
@@ -148,7 +148,7 @@ local function ApplyConfig()
DisableNPCs() DisableNPCs()
end end
RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin) ESX.SecureNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
ESX.PlayerData = xPlayer ESX.PlayerData = xPlayer
if not Config.Multichar then if not Config.Multichar then
@@ -192,11 +192,11 @@ RegisterNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
StartServerSyncLoops() StartServerSyncLoops()
end) end)
RegisterNetEvent("esx:onPlayerLogout", function() ESX.SecureNetEvent("esx:onPlayerLogout", function()
ESX.PlayerLoaded = false ESX.PlayerLoaded = false
end) end)
RegisterNetEvent("esx:setMaxWeight", function(newMaxWeight) ESX.SecureNetEvent("esx:setMaxWeight", function(newMaxWeight)
ESX.SetPlayerData("maxWeight", newMaxWeight) ESX.SetPlayerData("maxWeight", newMaxWeight)
end) end)
@@ -273,7 +273,7 @@ AddStateBagChangeHandler("VehicleProperties", nil, function(bagName, _, value)
ESX.Game.SetVehicleProperties(vehicle, value) ESX.Game.SetVehicleProperties(vehicle, value)
end) end)
RegisterNetEvent("esx:setAccountMoney", function(account) ESX.SecureNetEvent("esx:setAccountMoney", function(account)
for i = 1, #ESX.PlayerData.accounts do for i = 1, #ESX.PlayerData.accounts do
if ESX.PlayerData.accounts[i].name == account.name then if ESX.PlayerData.accounts[i].name == account.name then
ESX.PlayerData.accounts[i] = account ESX.PlayerData.accounts[i] = account
@@ -285,7 +285,7 @@ RegisterNetEvent("esx:setAccountMoney", function(account)
end) end)
if not Config.CustomInventory then if not Config.CustomInventory then
RegisterNetEvent("esx:addInventoryItem", function(item, count, showNotification) ESX.SecureNetEvent("esx:addInventoryItem", function(item, count, showNotification)
for k, v in ipairs(ESX.PlayerData.inventory) do for k, v in ipairs(ESX.PlayerData.inventory) do
if v.name == item then if v.name == item then
ESX.UI.ShowInventoryItemNotification(true, v.label, count - v.count) ESX.UI.ShowInventoryItemNotification(true, v.label, count - v.count)
@@ -299,7 +299,7 @@ if not Config.CustomInventory then
end end
end) end)
RegisterNetEvent("esx:removeInventoryItem", function(item, count, showNotification) ESX.SecureNetEvent("esx:removeInventoryItem", function(item, count, showNotification)
for i = 1, #ESX.PlayerData.inventory do for i = 1, #ESX.PlayerData.inventory do
if ESX.PlayerData.inventory[i].name == item then if ESX.PlayerData.inventory[i].name == item then
ESX.UI.ShowInventoryItemNotification(false, ESX.PlayerData.inventory[i].label, ESX.PlayerData.inventory[i].count - count) ESX.UI.ShowInventoryItemNotification(false, ESX.PlayerData.inventory[i].label, ESX.PlayerData.inventory[i].count - count)
@@ -326,7 +326,7 @@ if not Config.CustomInventory then
error("event ^5'esx:setWeaponAmmo'^1 Has Been Removed. Please use ^5xPlayer.addWeaponAmmo^1 Instead!") error("event ^5'esx:setWeaponAmmo'^1 Has Been Removed. Please use ^5xPlayer.addWeaponAmmo^1 Instead!")
end) end)
RegisterNetEvent("esx:setWeaponTint", function(weapon, weaponTintIndex) ESX.SecureNetEvent("esx:setWeaponTint", function(weapon, weaponTintIndex)
SetPedWeaponTintIndex(ESX.PlayerData.ped, joaat(weapon), weaponTintIndex) SetPedWeaponTintIndex(ESX.PlayerData.ped, joaat(weapon), weaponTintIndex)
end) end)
@@ -334,22 +334,22 @@ if not Config.CustomInventory then
error("event ^5'esx:removeWeapon'^1 Has Been Removed. Please use ^5xPlayer.removeWeapon^1 Instead!") error("event ^5'esx:removeWeapon'^1 Has Been Removed. Please use ^5xPlayer.removeWeapon^1 Instead!")
end) end)
RegisterNetEvent("esx:removeWeaponComponent", function(weapon, weaponComponent) ESX.SecureNetEvent("esx:removeWeaponComponent", function(weapon, weaponComponent)
local componentHash = ESX.GetWeaponComponent(weapon, weaponComponent).hash local componentHash = ESX.GetWeaponComponent(weapon, weaponComponent).hash
RemoveWeaponComponentFromPed(ESX.PlayerData.ped, joaat(weapon), componentHash) RemoveWeaponComponentFromPed(ESX.PlayerData.ped, joaat(weapon), componentHash)
end) end)
end end
RegisterNetEvent("esx:setJob", function(Job) ESX.SecureNetEvent("esx:setJob", function(Job)
ESX.SetPlayerData("job", Job) ESX.SetPlayerData("job", Job)
end) end)
RegisterNetEvent("esx:setGroup", function(group) ESX.SecureNetEvent("esx:setGroup", function(group)
ESX.SetPlayerData("group", group) ESX.SetPlayerData("group", group)
end) end)
if not Config.CustomInventory then if not Config.CustomInventory then
RegisterNetEvent("esx:createPickup", function(pickupId, label, coords, itemType, name, components, tintIndex) ESX.SecureNetEvent("esx:createPickup", function(pickupId, label, coords, itemType, name, components, tintIndex)
local function setObjectProperties(object) local function setObjectProperties(object)
SetEntityAsMissionEntity(object, true, false) SetEntityAsMissionEntity(object, true, false)
PlaceObjectOnGroundProperly(object) PlaceObjectOnGroundProperly(object)
@@ -383,14 +383,14 @@ if not Config.CustomInventory then
end end
end) end)
RegisterNetEvent("esx:createMissingPickups", function(missingPickups) ESX.SecureNetEvent("esx:createMissingPickups", function(missingPickups)
for pickupId, pickup in pairs(missingPickups) do for pickupId, pickup in pairs(missingPickups) do
TriggerEvent("esx:createPickup", pickupId, pickup.label, vector3(pickup.coords.x, pickup.coords.y, pickup.coords.z - 1.0), pickup.type, pickup.name, pickup.components, pickup.tintIndex) TriggerEvent("esx:createPickup", pickupId, pickup.label, vector3(pickup.coords.x, pickup.coords.y, pickup.coords.z - 1.0), pickup.type, pickup.name, pickup.components, pickup.tintIndex)
end end
end) end)
end end
RegisterNetEvent("esx:registerSuggestions", function(registeredCommands) ESX.SecureNetEvent("esx:registerSuggestions", function(registeredCommands)
for name, command in pairs(registeredCommands) do for name, command in pairs(registeredCommands) do
if command.suggestion then if command.suggestion then
TriggerEvent("chat:addSuggestion", ("/%s"):format(name), command.suggestion.help, command.suggestion.arguments) TriggerEvent("chat:addSuggestion", ("/%s"):format(name), command.suggestion.help, command.suggestion.arguments)
@@ -399,7 +399,7 @@ RegisterNetEvent("esx:registerSuggestions", function(registeredCommands)
end) end)
if not Config.CustomInventory then if not Config.CustomInventory then
RegisterNetEvent("esx:removePickup", function(pickupId) ESX.SecureNetEvent("esx:removePickup", function(pickupId)
if pickups[pickupId] and pickups[pickupId].obj then if pickups[pickupId] and pickups[pickupId].obj then
ESX.Game.DeleteObject(pickups[pickupId].obj) ESX.Game.DeleteObject(pickups[pickupId].obj)
pickups[pickupId] = nil pickups[pickupId] = nil
@@ -408,7 +408,7 @@ if not Config.CustomInventory then
end end
function StartServerSyncLoops() function StartServerSyncLoops()
if Config.CustomInventory then return end if Config.CustomInventory then return end
-- keep track of ammo -- keep track of ammo
CreateThread(function() CreateThread(function()
local currentWeapon = { Ammo = 0 } local currentWeapon = { Ammo = 0 }
@@ -495,7 +495,7 @@ if not Config.CustomInventory then
end end
----- Admin commands from esx_adminplus ----- Admin commands from esx_adminplus
RegisterNetEvent("esx:tpm", function() ESX.SecureNetEvent("esx:tpm", function()
local GetEntityCoords = GetEntityCoords local GetEntityCoords = GetEntityCoords
local GetGroundZFor_3dCoord = GetGroundZFor_3dCoord local GetGroundZFor_3dCoord = GetGroundZFor_3dCoord
local GetFirstBlipInfoId = GetFirstBlipInfoId local GetFirstBlipInfoId = GetFirstBlipInfoId
@@ -627,7 +627,7 @@ local function noclipThread()
end end
end end
RegisterNetEvent("esx:noclip", function() ESX.SecureNetEvent("esx:noclip", function()
ESX.TriggerServerCallback("esx:isUserAdmin", function(admin) ESX.TriggerServerCallback("esx:isUserAdmin", function(admin)
if not admin then if not admin then
return return
@@ -651,11 +651,11 @@ RegisterNetEvent("esx:noclip", function()
end) end)
end) end)
RegisterNetEvent("esx:killPlayer", function() ESX.SecureNetEvent("esx:killPlayer", function()
SetEntityHealth(ESX.PlayerData.ped, 0) SetEntityHealth(ESX.PlayerData.ped, 0)
end) end)
RegisterNetEvent("esx:repairPedVehicle", function() ESX.SecureNetEvent("esx:repairPedVehicle", function()
local ped = ESX.PlayerData.ped local ped = ESX.PlayerData.ped
local vehicle = GetVehiclePedIsIn(ped, false) local vehicle = GetVehiclePedIsIn(ped, false)
SetVehicleEngineHealth(vehicle, 1000) SetVehicleEngineHealth(vehicle, 1000)
@@ -664,7 +664,7 @@ RegisterNetEvent("esx:repairPedVehicle", function()
SetVehicleDirtLevel(vehicle, 0) SetVehicleDirtLevel(vehicle, 0)
end) end)
RegisterNetEvent("esx:freezePlayer", function(input) ESX.SecureNetEvent("esx:freezePlayer", function(input)
if input == "freeze" then if input == "freeze" then
SetEntityCollision(ESX.PlayerData.ped, false, false) SetEntityCollision(ESX.PlayerData.ped, false, false)
FreezeEntityPosition(ESX.PlayerData.ped, true) FreezeEntityPosition(ESX.PlayerData.ped, true)
@@ -680,6 +680,6 @@ ESX.RegisterClientCallback("esx:GetVehicleType", function(cb, model)
cb(ESX.GetVehicleTypeClient(model)) cb(ESX.GetVehicleTypeClient(model))
end) end)
RegisterNetEvent('esx:updatePlayerData', function(key, val) ESX.SecureNetEvent('esx:updatePlayerData', function(key, val)
ESX.SetPlayerData(key, val) ESX.SetPlayerData(key, val)
end) end)
@@ -66,7 +66,7 @@ ESX.TriggerServerCallback = function(eventName, callback, ...)
Callbacks:Trigger(eventName, callback, invoker, ...) Callbacks:Trigger(eventName, callback, invoker, ...)
end end
RegisterNetEvent("esx:serverCallback", function(...) ESX.SecureNetEvent("esx:serverCallback", function(...)
Callbacks:ServerRecieve(...) Callbacks:ServerRecieve(...)
end) end)
@@ -77,6 +77,6 @@ ESX.RegisterClientCallback = function(eventName, callback)
Callbacks:Register(eventName, callback) Callbacks:Register(eventName, callback)
end end
RegisterNetEvent("esx:triggerClientCallback", function(...) ESX.SecureNetEvent("esx:triggerClientCallback", function(...)
Callbacks:ClientRecieve(...) Callbacks:ClientRecieve(...)
end) end)
+4 -4
View File
@@ -9,7 +9,7 @@ local function checkPhone()
npwd:setPhoneDisabled((phoneItem and phoneItem.count or 0) <= 0) npwd:setPhoneDisabled((phoneItem and phoneItem.count or 0) <= 0)
end end
RegisterNetEvent("esx:playerLoaded", checkPhone) ESX.SecureNetEvent("esx:playerLoaded", checkPhone)
AddEventHandler("onClientResourceStart", function(resource) AddEventHandler("onClientResourceStart", function(resource)
if resource ~= "npwd" then if resource ~= "npwd" then
@@ -29,7 +29,7 @@ AddEventHandler("onClientResourceStop", function(resource)
end end
end) end)
RegisterNetEvent("esx:onPlayerLogout", function() ESX.SecureNetEvent("esx:onPlayerLogout", function()
if not npwd then if not npwd then
return return
end end
@@ -38,7 +38,7 @@ RegisterNetEvent("esx:onPlayerLogout", function()
npwd:setPhoneDisabled(true) npwd:setPhoneDisabled(true)
end) end)
RegisterNetEvent("esx:removeInventoryItem", function(item, count) ESX.SecureNetEvent("esx:removeInventoryItem", function(item, count)
if not npwd then if not npwd then
return return
end end
@@ -48,7 +48,7 @@ RegisterNetEvent("esx:removeInventoryItem", function(item, count)
end end
end) end)
RegisterNetEvent("esx:addInventoryItem", function(item) ESX.SecureNetEvent("esx:addInventoryItem", function(item)
if not npwd or item ~= "phone" then if not npwd or item ~= "phone" then
return return
end end
+2 -2
View File
@@ -12,12 +12,12 @@ if not IsDuplicityVersion() then -- Only register this event for the client
end end
end) end)
RegisterNetEvent("esx:playerLoaded", function(xPlayer) ESX.SecureNetEvent("esx:playerLoaded", function(xPlayer)
ESX.PlayerData = xPlayer ESX.PlayerData = xPlayer
ESX.PlayerLoaded = true ESX.PlayerLoaded = true
end) end)
RegisterNetEvent("esx:onPlayerLogout", function() ESX.SecureNetEvent("esx:onPlayerLogout", function()
ESX.PlayerLoaded = false ESX.PlayerLoaded = false
ESX.PlayerData = {} ESX.PlayerData = {}
end) end)
+3 -3
View File
@@ -3,14 +3,14 @@ local ready = false
local guiEnabled = false local guiEnabled = false
local timecycleModifier = "hud_def_blur" local timecycleModifier = "hud_def_blur"
RegisterNetEvent("esx_identity:alreadyRegistered", function() ESX.SecureNetEvent("esx_identity:alreadyRegistered", function()
while not loadingScreenFinished do while not loadingScreenFinished do
Wait(100) Wait(100)
end end
TriggerEvent("esx_skin:playerRegistered") TriggerEvent("esx_skin:playerRegistered")
end) end)
RegisterNetEvent("esx_identity:setPlayerData", function(data) ESX.SecureNetEvent("esx_identity:setPlayerData", function(data)
SetTimeout(1, function() SetTimeout(1, function()
ESX.SetPlayerData("name", ("%s %s"):format(data.firstName, data.lastName)) ESX.SetPlayerData("name", ("%s %s"):format(data.firstName, data.lastName))
ESX.SetPlayerData("firstName", data.firstName) ESX.SetPlayerData("firstName", data.firstName)
@@ -44,7 +44,7 @@ if not Config.UseDeferrals then
SendNUIMessage({ type = "enableui", enable = state }) SendNUIMessage({ type = "enableui", enable = state })
end end
RegisterNetEvent("esx_identity:showRegisterIdentity", function() ESX.SecureNetEvent("esx_identity:showRegisterIdentity", function()
TriggerEvent("esx_skin:resetFirstSpawn") TriggerEvent("esx_skin:resetFirstSpawn")
while not (ready and loadingScreenFinished) do while not (ready and loadingScreenFinished) do
print("Waiting for esx_identity NUI..") print("Waiting for esx_identity NUI..")
+3 -3
View File
@@ -29,15 +29,15 @@ end)
-- Events -- Events
RegisterNetEvent("esx_multicharacter:SetupUI", function(data, slots) ESX.SecureNetEvent("esx_multicharacter:SetupUI", function(data, slots)
Multicharacter:SetupUI(data, slots) Multicharacter:SetupUI(data, slots)
end) end)
RegisterNetEvent('esx:playerLoaded', function(playerData, isNew, skin) ESX.SecureNetEvent('esx:playerLoaded', function(playerData, isNew, skin)
Multicharacter:PlayerLoaded(playerData, isNew, skin) Multicharacter:PlayerLoaded(playerData, isNew, skin)
end) end)
RegisterNetEvent('esx:onPlayerLogout', function() ESX.SecureNetEvent('esx:onPlayerLogout', function()
DoScreenFadeOut(500) DoScreenFadeOut(500)
Wait(5000) Wait(5000)
+1 -1
View File
@@ -32,7 +32,7 @@ local function Notify(notificatonType, length, message)
end end
exports("Notify", Notify) exports("Notify", Notify)
RegisterNetEvent("ESX:Notify", Notify) ESX.SecureNetEvent("ESX:Notify", Notify)
if Debug then if Debug then
RegisterCommand("oldnotify", function() RegisterCommand("oldnotify", function()
+3 -3
View File
@@ -31,7 +31,7 @@ function Skin:CalcuatePosition(coords)
} }
local angleToLook = self:CalcualteHeading(self.heading - 140.0) local angleToLook = self:CalcualteHeading(self.heading - 140.0)
local thetaToLook = { local thetaToLook = {
x = math.cos(angleToLook), x = math.cos(angleToLook),
y = math.sin(angleToLook), y = math.sin(angleToLook),
@@ -70,7 +70,7 @@ AddEventHandler("esx_skin:playerRegistered", function()
end) end)
end) end)
RegisterNetEvent("esx:playerLoaded", function(_, _, skin) ESX.SecureNetEvent("esx:playerLoaded", function(_, _, skin)
ESX.PlayerLoaded = true ESX.PlayerLoaded = true
TriggerServerEvent("esx_skin:setWeight", skin) TriggerServerEvent("esx_skin:setWeight", skin)
end) end)
@@ -97,4 +97,4 @@ end)
AddEventHandler("esx_skin:setLastSkin", function(skin) AddEventHandler("esx_skin:setLastSkin", function(skin)
Skin.Last = skin Skin.Last = skin
end) end)
+2 -2
View File
@@ -23,8 +23,8 @@ end
exports("TextUI", TextUI) exports("TextUI", TextUI)
exports("HideUI", HideUI) exports("HideUI", HideUI)
RegisterNetEvent("ESX:TextUI", TextUI) ESX.SecureNetEvent("ESX:TextUI", TextUI)
RegisterNetEvent("ESX:HideUI", HideUI) ESX.SecureNetEvent("ESX:HideUI", HideUI)
if Debug then if Debug then
RegisterCommand("textui:error", function() RegisterCommand("textui:error", function()
+2 -2
View File
@@ -116,7 +116,7 @@ function SkinChanger:SetHead()
if not self.character["grandparents"] then if not self.character["grandparents"] then
self.character["grandparents"] = 0 self.character["grandparents"] = 0
end end
SetPedHeadBlendData(self.playerPed, SetPedHeadBlendData(self.playerPed,
self.character["mom"], self.character["dad"], self.character["grandparents"] , self.character["mom"], self.character["mom"], self.character["dad"], self.character["grandparents"] , self.character["mom"],
self.character["dad"], self.character["grandparents"], face_weight, skin_weight, third_weight, false) self.character["dad"], self.character["grandparents"], face_weight, skin_weight, third_weight, false)
@@ -304,4 +304,4 @@ AddEventHandler("skinchanger:getData", function(cb)
cb(components, SkinChanger:MaxValues()) cb(components, SkinChanger:MaxValues())
end) end)
SkinChanger:Init() SkinChanger:Init()