From 12d0f0badc1736385f1802887fccd7bd6cde2df3 Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Thu, 26 Sep 2024 19:50:35 +0200 Subject: [PATCH] =?UTF-8?q?chore:=20[system]\runcode=20=F0=9F=8E=A8=20Run?= =?UTF-8?q?=20formatter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/[system]/runcode/fxmanifest.lua | 32 +- .../resources/[system]/runcode/runcode.js | 17 +- .../resources/[system]/runcode/runcode_cl.lua | 24 +- .../[system]/runcode/runcode_shared.lua | 36 +-- .../resources/[system]/runcode/runcode_sv.lua | 56 ++-- .../resources/[system]/runcode/runcode_ui.lua | 36 +-- .../[system]/runcode/runcode_web.lua | 289 +++++++++--------- .../resources/[system]/runcode/web/index.html | 260 ++++++++-------- .../resources/[system]/runcode/web/nui.html | 106 +++---- 9 files changed, 433 insertions(+), 423 deletions(-) diff --git a/server-data/resources/[system]/runcode/fxmanifest.lua b/server-data/resources/[system]/runcode/fxmanifest.lua index 3ff01e759..317214b75 100644 --- a/server-data/resources/[system]/runcode/fxmanifest.lua +++ b/server-data/resources/[system]/runcode/fxmanifest.lua @@ -1,24 +1,24 @@ -- This resource is part of the default Cfx.re asset pack (cfx-server-data) -- Altering or recreating for local use only is strongly discouraged. -version '1.0.0' -author 'Cfx.re ' -description 'Allows server owners to execute arbitrary server-side or client-side JavaScript/Lua code. *Consider only using this on development servers.' -repository 'https://github.com/citizenfx/cfx-server-data' +version("1.0.0") +author("Cfx.re ") +description("Allows server owners to execute arbitrary server-side or client-side JavaScript/Lua code. *Consider only using this on development servers.") +repository("https://github.com/citizenfx/cfx-server-data") -game 'common' -fx_version 'bodacious' +game("common") +fx_version("bodacious") -client_script 'runcode_cl.lua' -server_script 'runcode_sv.lua' -server_script 'runcode_web.lua' +client_script("runcode_cl.lua") +server_script("runcode_sv.lua") +server_script("runcode_web.lua") -shared_script 'runcode_shared.lua' -shared_script 'runcode.js' +shared_script("runcode_shared.lua") +shared_script("runcode.js") -client_script 'runcode_ui.lua' +client_script("runcode_ui.lua") -ui_page 'web/nui.html' -files { - 'web/nui.html' -} +ui_page("web/nui.html") +files({ + "web/nui.html", +}) diff --git a/server-data/resources/[system]/runcode/runcode.js b/server-data/resources/[system]/runcode/runcode.js index 76c878ddd..0d98fa364 100644 --- a/server-data/resources/[system]/runcode/runcode.js +++ b/server-data/resources/[system]/runcode/runcode.js @@ -1,11 +1,12 @@ exports('runJS', (snippet) => { - if (IsDuplicityVersion() && GetInvokingResource() !== GetCurrentResourceName()) { - return [ 'Invalid caller.', false ]; - } + if (IsDuplicityVersion() && GetInvokingResource() !== GetCurrentResourceName()) { + return [ 'Invalid caller.', false ]; + } - try { - return [ new Function(snippet)(), false ]; - } catch (e) { - return [ false, e.toString() ]; - } + try { + return [ new Function(snippet)(), false ]; + } + catch (e) { + return [ false, e.toString() ]; + } }); \ No newline at end of file diff --git a/server-data/resources/[system]/runcode/runcode_cl.lua b/server-data/resources/[system]/runcode/runcode_cl.lua index 29e68064f..dfba7b05f 100644 --- a/server-data/resources/[system]/runcode/runcode_cl.lua +++ b/server-data/resources/[system]/runcode/runcode_cl.lua @@ -1,15 +1,15 @@ -RegisterNetEvent('runcode:gotSnippet') +RegisterNetEvent("runcode:gotSnippet") -AddEventHandler('runcode:gotSnippet', function(id, lang, code) - local res, err = RunCode(lang, code) +AddEventHandler("runcode:gotSnippet", function(id, lang, code) + local res, err = RunCode(lang, code) - if not err then - if type(res) == 'vector3' then - res = json.encode({ table.unpack(res) }) - elseif type(res) == 'table' then - res = json.encode(res) - end - end + if not err then + if type(res) == "vector3" then + res = json.encode({ table.unpack(res) }) + elseif type(res) == "table" then + res = json.encode(res) + end + end - TriggerServerEvent('runcode:gotResult', id, res, err) -end) \ No newline at end of file + TriggerServerEvent("runcode:gotResult", id, res, err) +end) diff --git a/server-data/resources/[system]/runcode/runcode_shared.lua b/server-data/resources/[system]/runcode/runcode_shared.lua index e1d8111d3..05059bbb0 100644 --- a/server-data/resources/[system]/runcode/runcode_shared.lua +++ b/server-data/resources/[system]/runcode/runcode_shared.lua @@ -1,32 +1,32 @@ local runners = {} function runners.lua(arg) - local code, err = load('return ' .. arg, '@runcode') + local code, err = load("return " .. arg, "@runcode") - -- if failed, try without return - if err then - code, err = load(arg, '@runcode') - end + -- if failed, try without return + if err then + code, err = load(arg, "@runcode") + end - if err then - print(err) - return nil, err - end + if err then + print(err) + return nil, err + end - local status, result = pcall(code) - print(result) + local status, result = pcall(code) + print(result) - if status then - return result - end + if status then + return result + end - return nil, result + return nil, result end function runners.js(arg) - return table.unpack(exports[GetCurrentResourceName()]:runJS(arg)) + return table.unpack(exports[GetCurrentResourceName()]:runJS(arg)) end function RunCode(lang, str) - return runners[lang](str) -end \ No newline at end of file + return runners[lang](str) +end diff --git a/server-data/resources/[system]/runcode/runcode_sv.lua b/server-data/resources/[system]/runcode/runcode_sv.lua index f2d52c36d..8614e25ae 100644 --- a/server-data/resources/[system]/runcode/runcode_sv.lua +++ b/server-data/resources/[system]/runcode/runcode_sv.lua @@ -1,42 +1,42 @@ function GetPrivs(source) - return { - canServer = IsPlayerAceAllowed(source, 'command.run'), - canClient = IsPlayerAceAllowed(source, 'command.crun'), - canSelf = IsPlayerAceAllowed(source, 'runcode.self'), - } + return { + canServer = IsPlayerAceAllowed(source, "command.run"), + canClient = IsPlayerAceAllowed(source, "command.crun"), + canSelf = IsPlayerAceAllowed(source, "runcode.self"), + } end -RegisterCommand('run', function(_, _, rawCommand) - local _, _ = RunCode('lua', rawCommand:sub(4)) +RegisterCommand("run", function(_, _, rawCommand) + local _, _ = RunCode("lua", rawCommand:sub(4)) end, true) -RegisterCommand('crun', function(source, _, rawCommand) - if not source then - return - end +RegisterCommand("crun", function(source, _, rawCommand) + if not source then + return + end - TriggerClientEvent('runcode:gotSnippet', source, -1, 'lua', rawCommand:sub(5)) + TriggerClientEvent("runcode:gotSnippet", source, -1, "lua", rawCommand:sub(5)) end, true) -RegisterCommand('runcode', function(source) - if not source then - return - end +RegisterCommand("runcode", function(source) + if not source then + return + end - local df = LoadResourceFile(GetCurrentResourceName(), 'data.json') - local saveData = {} + local df = LoadResourceFile(GetCurrentResourceName(), "data.json") + local saveData = {} - if df then - saveData = json.decode(df) - end + if df then + saveData = json.decode(df) + end - local p = GetPrivs(source) + local p = GetPrivs(source) - if not p.canServer and not p.canClient and not p.canSelf then - return - end + if not p.canServer and not p.canClient and not p.canSelf then + return + end - p.saveData = saveData + p.saveData = saveData - TriggerClientEvent('runcode:openUi', source, p) -end, true) \ No newline at end of file + TriggerClientEvent("runcode:openUi", source, p) +end, true) diff --git a/server-data/resources/[system]/runcode/runcode_ui.lua b/server-data/resources/[system]/runcode/runcode_ui.lua index 65f93649f..7f8b58000 100644 --- a/server-data/resources/[system]/runcode/runcode_ui.lua +++ b/server-data/resources/[system]/runcode/runcode_ui.lua @@ -1,56 +1,56 @@ local openData -RegisterNetEvent('runcode:openUi') +RegisterNetEvent("runcode:openUi") -AddEventHandler('runcode:openUi', function(options) +AddEventHandler("runcode:openUi", function(options) openData = { - type = 'open', + type = "open", options = options, - url = 'http://' .. GetCurrentServerEndpoint() .. '/' .. GetCurrentResourceName() .. '/', - res = GetCurrentResourceName() + url = "http://" .. GetCurrentServerEndpoint() .. "/" .. GetCurrentResourceName() .. "/", + res = GetCurrentResourceName(), } SendNuiMessage(json.encode(openData)) end) -RegisterNUICallback('getOpenData', function(_, cb) +RegisterNUICallback("getOpenData", function(_, cb) cb(openData) end) -RegisterNUICallback('doOk', function(_, cb) +RegisterNUICallback("doOk", function(_, cb) SendNuiMessage(json.encode({ - type = 'ok' + type = "ok", })) SetNuiFocus(true, true) - cb('ok') + cb("ok") end) -RegisterNUICallback('doClose', function(_, cb) +RegisterNUICallback("doClose", function(_, cb) SendNuiMessage(json.encode({ - type = 'close' + type = "close", })) SetNuiFocus(false, false) - cb('ok') + cb("ok") end) local rcCbs = {} local id = 1 -RegisterNUICallback('runCodeInBand', function(args, cb) +RegisterNUICallback("runCodeInBand", function(args, cb) id = id + 1 rcCbs[id] = cb - TriggerServerEvent('runcode:runInBand', id, args) + TriggerServerEvent("runcode:runInBand", id, args) end) -RegisterNetEvent('runcode:inBandResult') +RegisterNetEvent("runcode:inBandResult") -AddEventHandler('runcode:inBandResult', function(id, result) +AddEventHandler("runcode:inBandResult", function(id, result) if rcCbs[id] then local cb = rcCbs[id] rcCbs[id] = nil @@ -59,8 +59,8 @@ AddEventHandler('runcode:inBandResult', function(id, result) end end) -AddEventHandler('onResourceStop', function(resourceName) +AddEventHandler("onResourceStop", function(resourceName) if resourceName == GetCurrentResourceName() then SetNuiFocus(false, false) end -end) \ No newline at end of file +end) diff --git a/server-data/resources/[system]/runcode/runcode_web.lua b/server-data/resources/[system]/runcode/runcode_web.lua index 9744dbab6..65704258a 100644 --- a/server-data/resources/[system]/runcode/runcode_web.lua +++ b/server-data/resources/[system]/runcode/runcode_web.lua @@ -1,21 +1,21 @@ local cachedFiles = {} local function sendFile(res, fileName) - if cachedFiles[fileName] then - res.send(cachedFiles[fileName]) - return - end + if cachedFiles[fileName] then + res.send(cachedFiles[fileName]) + return + end - local fileData = LoadResourceFile(GetCurrentResourceName(), 'web/' .. fileName) + local fileData = LoadResourceFile(GetCurrentResourceName(), "web/" .. fileName) - if not fileData then - res.writeHead(404) - res.send('Not found.') - return - end + if not fileData then + res.writeHead(404) + res.send("Not found.") + return + end - cachedFiles[fileName] = fileData - res.send(fileData) + cachedFiles[fileName] = fileData + res.send(fileData) end local codeId = 1 @@ -25,168 +25,173 @@ local attempts = 0 local lastAttempt local function handleRunCode(data, res) - if not data.lang then - data.lang = 'lua' - end - - if not data.client or data.client == '' then - CreateThread(function() - local result, err = RunCode(data.lang, data.code) - - res.send(json.encode({ - result = result, - error = err - })) - end) - else - codes[codeId] = { - timeout = GetGameTimer() + 1000, - res = res - } - - TriggerClientEvent('runcode:gotSnippet', tonumber(data.client), codeId, data.lang, data.code) - - codeId = codeId + 1 - end + if not data.lang then + data.lang = "lua" + end + + if not data.client or data.client == "" then + CreateThread(function() + local result, err = RunCode(data.lang, data.code) + + res.send(json.encode({ + result = result, + error = err, + })) + end) + else + codes[codeId] = { + timeout = GetGameTimer() + 1000, + res = res, + } + + TriggerClientEvent("runcode:gotSnippet", tonumber(data.client), codeId, data.lang, data.code) + + codeId = codeId + 1 + end end -RegisterNetEvent('runcode:runInBand') - -AddEventHandler('runcode:runInBand', function(id, data) - local s = source - local privs = GetPrivs(s) - - local res = { - send = function(str) - TriggerClientEvent('runcode:inBandResult', s, id, str) - end - } - - if (not data.client or data.client == '') and not privs.canServer then - res.send(json.encode({ error = 'Insufficient permissions.'})) - return - end - - if (data.client and data.client ~= '') and not privs.canClient then - if privs.canSelf then - data.client = s - else - res.send(json.encode({ error = 'Insufficient permissions.'})) - return - end - end - - SaveResourceFile(GetCurrentResourceName(), 'data.json', json.encode({ - lastSnippet = data.code, - lastLang = data.lang or 'lua' - }), -1) - - handleRunCode(data, res) +RegisterNetEvent("runcode:runInBand") + +AddEventHandler("runcode:runInBand", function(id, data) + local s = source + local privs = GetPrivs(s) + + local res = { + send = function(str) + TriggerClientEvent("runcode:inBandResult", s, id, str) + end, + } + + if (not data.client or data.client == "") and not privs.canServer then + res.send(json.encode({ error = "Insufficient permissions." })) + return + end + + if (data.client and data.client ~= "") and not privs.canClient then + if privs.canSelf then + data.client = s + else + res.send(json.encode({ error = "Insufficient permissions." })) + return + end + end + + SaveResourceFile( + GetCurrentResourceName(), + "data.json", + json.encode({ + lastSnippet = data.code, + lastLang = data.lang or "lua", + }), + -1 + ) + + handleRunCode(data, res) end) local function handlePost(req, res) - req.setDataHandler(function(body) - local data = json.decode(body) + req.setDataHandler(function(body) + local data = json.decode(body) - if not data or not data.password or not data.code then - res.send(json.encode({ error = 'Bad request.'})) - return - end + if not data or not data.password or not data.code then + res.send(json.encode({ error = "Bad request." })) + return + end - if GetConvar('rcon_password', '') == '' then - res.send(json.encode({ error = 'The server has an empty rcon_password.'})) - return - end + if GetConvar("rcon_password", "") == "" then + res.send(json.encode({ error = "The server has an empty rcon_password." })) + return + end - if attempts > 5 or data.password ~= GetConvar('rcon_password', '') then - attempts = attempts + 1 - lastAttempt = GetGameTimer() + if attempts > 5 or data.password ~= GetConvar("rcon_password", "") then + attempts = attempts + 1 + lastAttempt = GetGameTimer() - res.send(json.encode({ error = 'Bad password.'})) - return - end + res.send(json.encode({ error = "Bad password." })) + return + end - handleRunCode(data, res) - end) + handleRunCode(data, res) + end) end CreateThread(function() - while true do - Wait(1000) - - if attempts > 0 and (GetGameTimer() - lastAttempt) > 5000 then - attempts = 0 - lastAttempt = 0 - end - end + while true do + Wait(1000) + + if attempts > 0 and (GetGameTimer() - lastAttempt) > 5000 then + attempts = 0 + lastAttempt = 0 + end + end end) local function returnCode(id, res, err) - if not codes[id] then - return - end + if not codes[id] then + return + end - local code = codes[id] - codes[id] = nil + local code = codes[id] + codes[id] = nil - local gotFrom + local gotFrom - if source then - gotFrom = GetPlayerName(source) .. ' [' .. tostring(source) .. ']' - end + if source then + gotFrom = GetPlayerName(source) .. " [" .. tostring(source) .. "]" + end - code.res.send(json.encode({ - result = res, - error = err, - from = gotFrom - })) + code.res.send(json.encode({ + result = res, + error = err, + from = gotFrom, + })) end CreateThread(function() - while true do - Wait(100) - - for k, v in ipairs(codes) do - if GetGameTimer() > v.timeout then - source = nil - returnCode(k, '', 'Timed out waiting on the target client.') - end - end - end + while true do + Wait(100) + + for k, v in ipairs(codes) do + if GetGameTimer() > v.timeout then + source = nil + returnCode(k, "", "Timed out waiting on the target client.") + end + end + end end) -RegisterNetEvent('runcode:gotResult') -AddEventHandler('runcode:gotResult', returnCode) +RegisterNetEvent("runcode:gotResult") +AddEventHandler("runcode:gotResult", returnCode) SetHttpHandler(function(req, res) - local path = req.path + local path = req.path - if req.method == 'POST' then - return handlePost(req, res) - end + if req.method == "POST" then + return handlePost(req, res) + end - -- client shortcuts - if req.path == '/clients' then - local clientList = {} + -- client shortcuts + if req.path == "/clients" then + local clientList = {} - for _, id in ipairs(GetPlayers()) do - table.insert(clientList, { GetPlayerName(id), id }) - end + for _, id in ipairs(GetPlayers()) do + table.insert(clientList, { GetPlayerName(id), id }) + end - res.send(json.encode({ - clients = clientList - })) + res.send(json.encode({ + clients = clientList, + })) - return - end + return + end - -- should this be the index? - if req.path == '/' then - path = 'index.html' - end + -- should this be the index? + if req.path == "/" then + path = "index.html" + end - -- remove any '..' from the path - path = path:gsub("%.%.", "") + -- remove any '..' from the path + path = path:gsub("%.%.", "") - return sendFile(res, path) -end) \ No newline at end of file + return sendFile(res, path) +end) diff --git a/server-data/resources/[system]/runcode/web/index.html b/server-data/resources/[system]/runcode/web/index.html index c615fcaac..43a53e9d6 100644 --- a/server-data/resources/[system]/runcode/web/index.html +++ b/server-data/resources/[system]/runcode/web/index.html @@ -1,5 +1,6 @@ + fivem runcode @@ -10,67 +11,68 @@ +
@@ -83,7 +85,8 @@ runcode  in-game -