From 9dd6ac81459a869d1bd4298e5ecb6f44527b551e Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Tue, 13 Jul 2021 01:16:46 +0200 Subject: [PATCH 1/9] Initial work on debugger --- ext/Server/Commands/Chat.lua | 13 ++++++-- ext/Server/Constants/Permissions.lua | 5 ++- ext/Server/Debug/Debugger.lua | 50 ++++++++++++++++++++++++++++ ext/Server/__init__.lua | 1 + 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 ext/Server/Debug/Debugger.lua diff --git a/ext/Server/Commands/Chat.lua b/ext/Server/Commands/Chat.lua index e30c5454..b3d5991a 100644 --- a/ext/Server/Commands/Chat.lua +++ b/ext/Server/Commands/Chat.lua @@ -5,6 +5,7 @@ local m_NodeCollection = require('__shared/NodeCollection') local m_BotManager = require('BotManager') local m_BotSpawner = require('BotSpawner') +local m_Debug = require('Debug/Debugger') function ChatCommands:Execute(p_Parts, p_Player) if p_Player == nil or Config.DisableChatCommands == true then @@ -173,9 +174,6 @@ function ChatCommands:Execute(p_Parts, p_Player) Config.BotAimWorsening = tonumber(p_Parts[2]) or 0.5 --self:_modifyWeapons(Config.BotAimWorsening) --causes lag. Instead restart round - if Debug.Server.COMMAND then - print('difficulty set to ' .. Config.BotAimWorsening .. '. Please restart round or level to take effect') - end elseif p_Parts[1] == '!shootback' then if PermissionManager:HasPermission(p_Player, 'ChatCommands.ShootBack') == false then ChatManager:SendMessage('You have no permissions for this action (ChatCommands.ShootBack).', p_Player) @@ -333,6 +331,15 @@ function ChatCommands:Execute(p_Parts, p_Player) local s_TraceIndex = tonumber(p_Parts[2]) or 0 NetEvents:SendToLocal('ClientNodeEditor:SaveTrace', p_Player, s_TraceIndex) + + -- Debug: Generate debug report + elseif p_Parts[1] == '!bugreport' then + if PermissionManager:HasPermission(p_Player, 'Debug.BugReport') == false then + ChatManager:SendMessage('You have no permissions for this action (Debug.BugReport).', p_Player) + return + end + + Debugger:GenerateReport(p_Player) end end diff --git a/ext/Server/Constants/Permissions.lua b/ext/Server/Constants/Permissions.lua index c471963b..651674a6 100644 --- a/ext/Server/Constants/Permissions.lua +++ b/ext/Server/Constants/Permissions.lua @@ -56,5 +56,8 @@ Permissions = { 'UserInterface.WaypointEditor.TraceReset', -- Settings - 'UserInterface.Settings' + 'UserInterface.Settings', + + -- Debugger + 'Debug.BugReport' } diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua new file mode 100644 index 00000000..6ccd1292 --- /dev/null +++ b/ext/Server/Debug/Debugger.lua @@ -0,0 +1,50 @@ +class('Debugger') + +-- Basic debugger +-- Author: @Firjen on 12/07/21 + +-- The URL to the fun-bots report site +local DEBUG_REPORT_URL = "https://report.funbots.dev" + +local DEBUG_ELIGIABLE_PATH = "/api/precheck" + +local DEBUG_SUBMIT_PATH = "/api/submit" + +-- Generate a bug report and send it to the fun-bots bug report server +function Debugger:GenerateReport(p_Player) + -- Before submitting a report, check if we are eligable to generate a bug report. + -- Uneligable servers are outdated servers or those who've made too many reports in the past hour. + + ChatManager:Yell("Generating a new bug report...", 10.0, p_Player) + print("[DEBUG] " .. p_Player.name .. " is generating a new bug report.") + + if RCON:GetServerGuid() == nil then + print("[DEBUG] Bug report failed: Server GUID is unknown or not set.") + ChatManager:Yell("Failed to create bug report. Server GUID unknown.", 5.0, p_Player) + do return end + end + + -- Net:GetHTTPAsync(DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid(), Debugger:GenerateReportCallback()) + -- Net:GetHTTPAsync("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", GenerateReportCallback) + + local s_content = Net:GetHTTP("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2") + GenerateReportCallback(s_content) +end + +function GenerateReportCallback(HttpRequest) + + -- @Todo: Broadcast a message to all users with permission + + print("[DEBUG] Code: " .. HttpRequest.status) +end + +function Debugger:__init() + local s_start = os.time(os.date("!*t")) + + print("Loaded debugger v2 in " .. os.time(os.date("!*t")) - s_start .. " ms") + + return true +end + + +return Debugger:__init() \ No newline at end of file diff --git a/ext/Server/__init__.lua b/ext/Server/__init__.lua index 86e1c1d0..c3edcd82 100644 --- a/ext/Server/__init__.lua +++ b/ext/Server/__init__.lua @@ -30,6 +30,7 @@ local m_SettingsManager = require('SettingsManager') local m_BotManager = require('BotManager') local m_BotSpawner = require('BotSpawner') local m_WeaponList = require('__shared/WeaponList') +local m_Debugger = require('Debug/Debugger') local m_ChatCommands = require('Commands/Chat') local m_Console = require('Commands/Console') local m_RCONCommands = require('Commands/RCON') From 13bf25be57f1af08df26e9d2001f6e3f28784cda Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Tue, 13 Jul 2021 14:39:30 +0200 Subject: [PATCH 2/9] Update Debugger.lua --- ext/Server/Debug/Debugger.lua | 55 ++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua index 6ccd1292..b72277bb 100644 --- a/ext/Server/Debug/Debugger.lua +++ b/ext/Server/Debug/Debugger.lua @@ -10,32 +10,71 @@ local DEBUG_ELIGIABLE_PATH = "/api/precheck" local DEBUG_SUBMIT_PATH = "/api/submit" +local pp = nil + -- Generate a bug report and send it to the fun-bots bug report server function Debugger:GenerateReport(p_Player) -- Before submitting a report, check if we are eligable to generate a bug report. -- Uneligable servers are outdated servers or those who've made too many reports in the past hour. - ChatManager:Yell("Generating a new bug report...", 10.0, p_Player) + ChatManager:Yell("Generating a new bug report...", 15.0, p_Player) print("[DEBUG] " .. p_Player.name .. " is generating a new bug report.") if RCON:GetServerGuid() == nil then print("[DEBUG] Bug report failed: Server GUID is unknown or not set.") - ChatManager:Yell("Failed to create bug report. Server GUID unknown.", 5.0, p_Player) + ChatManager:Yell("Failed to create bug report. Server GUID unknown", 4.0, p_Player) do return end end -- Net:GetHTTPAsync(DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid(), Debugger:GenerateReportCallback()) - -- Net:GetHTTPAsync("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", GenerateReportCallback) + pp = p_Player + Net:GetHTTPAsync("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", GenerateReportCallback) +end + +function GenerateReportCallback(httpRequest) + local p_Player = pp + + if httpRequest == nil then + print("[DEBUG] Bug report failed: HTTP request failure") + ChatManager:Yell("Failed to create bug report. HTTP request failure", 4.0, pp) + do return end + end + + -- Check code returned + if httpRequest.status == 429 then + print("[DEBUG] Bug report failed: too many bug reports created. You can only create 2 reports per 24 hr.") + ChatManager:Yell("Failed to create bug report. Report limit reached (max 2 per 24hr)", 4.0, p_Player) + do return end + elseif httpRequest.status ~= 429 and httpRequest.status ~= 200 then + print("[DEBUG] Bug report failed: Report server returned error " .. httpRequest.status) + ChatManager:Yell("Failed to create bug report. HTTP code " .. httpRequest.status, 4.0, p_Player) + do return end + end - local s_content = Net:GetHTTP("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2") - GenerateReportCallback(s_content) + -- If code 200, we can ask the server to create our report. + Debugger:CreateReport(p_Player) end -function GenerateReportCallback(HttpRequest) +-- Generate the report +function Debugger:CreateReport(p_Player) + -- Generate POST data + local postData = {} - -- @Todo: Broadcast a message to all users with permission + -- Add config to POST + postData.insert("config", json.encode(Config)) - print("[DEBUG] Code: " .. HttpRequest.status) + -- Send post data + Net:PostHTTPAsync("https://report.funbots.dev/api/submit?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", postData.tostring, CreateReportCallback) +end + +function CreateReportCallback(httpRequest) + if httpRequest == nil then + print("[DEBUG] Bug report failed: HTTP request failure") + ChatManager:Yell("Failed to create bug report. HTTP request failure 2", 4.0, pp) + do return end + end + + ChatManager:Yell("Bug report created?", 4.0, pp) end function Debugger:__init() From b80327f1d6463656530ec29abe539409dfea9ac1 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Tue, 13 Jul 2021 17:25:58 +0200 Subject: [PATCH 3/9] Update Debugger.lua --- ext/Server/Debug/Debugger.lua | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua index b72277bb..cc446d18 100644 --- a/ext/Server/Debug/Debugger.lua +++ b/ext/Server/Debug/Debugger.lua @@ -26,12 +26,17 @@ function Debugger:GenerateReport(p_Player) do return end end - -- Net:GetHTTPAsync(DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid(), Debugger:GenerateReportCallback()) + -- @Todo: Figure out how to pass this variable with GenerateReportCallback pp = p_Player - Net:GetHTTPAsync("https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", GenerateReportCallback) + + local s_UUID = RCON:GetServerGuid(). + print("UUID = " .. s_UUID) + + Net:GetHTTPAsync(DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid(), GenerateReportCallback) end function GenerateReportCallback(httpRequest) + -- @Todo: Figure out how to pass this variable with GenerateReportCallback local p_Player = pp if httpRequest == nil then @@ -58,13 +63,13 @@ end -- Generate the report function Debugger:CreateReport(p_Player) -- Generate POST data - local postData = {} + local postData = {config = json.encode(Config)} + print(postData) - -- Add config to POST - postData.insert("config", json.encode(Config)) + local s_String = "config=" .. json.encode(Config) .. "&version=2.2.0" .. "author=" .. p_Player.name -- Send post data - Net:PostHTTPAsync("https://report.funbots.dev/api/submit?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2", postData.tostring, CreateReportCallback) + Net:PostHTTPAsync(DEBUG_REPORT_URL .. DEBUG_REPORT_URL .. "?uuid=" .. RCON:GetServerGuid(), json.encode(Config), CreateReportCallback) end function CreateReportCallback(httpRequest) @@ -85,5 +90,4 @@ function Debugger:__init() return true end - return Debugger:__init() \ No newline at end of file From 38133625a87c84cb7c47526f297e7ecc4e55d1f4 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Tue, 13 Jul 2021 17:33:40 +0200 Subject: [PATCH 4/9] Update bug-report.yml --- .github/ISSUE_TEMPLATE/bug-report.yml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml index 8b354971..dd6c8ec0 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.yml +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -7,15 +7,16 @@ body: value: | Thanks for taking the time to fill out this bug report! Before posting be sure that it's not a duplicate of any previous posted issues by using the search function. Please note that we do not support outdated versions. Always use the latest stable release. - - type: dropdown - id: version + + - type: input + id: bug-report-token attributes: - label: Your fun-bots version - description: What version of fun-bots does this issue occur on? Please note that we do not support old versions. Select the correct version. - options: - - 2.2.0-RC (release candidates) - - 2.2.0 (stable release) - - 2.3.0-dev (development builds) + label: Bug report token + description: | + Generate a bug report using the `!bugreport` command and enter the token or URL here. + This is a **required** step to create the bug report. + Note: when reporting multiple bugs, use the same bug report token as long as you've made no changes to the server. + placeholder: https://report.funbots.dev/7e1..... validations: required: true - type: textarea @@ -57,6 +58,4 @@ body: - label: I have read the [contribution guidelines on reporting bugs](https://github.com/Joe91/fun-bots/blob/master/.github/CONTRIBUTING.md#reporting-a-bug) and the [code of conduct](https://github.com/Joe91/fun-bots/blob/master/.github/CODE_OF_CONDUCT.md). required: true - label: I have tested and reproduced this bug without any other mods besides fun-bots. - required: false - - label: I have tested and reproduced this bug with the default fun-bots configuration file. required: false \ No newline at end of file From 0b900f5c8e84b3e63e7720f8dc079bd7c4e15f48 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Tue, 13 Jul 2021 17:37:32 +0200 Subject: [PATCH 5/9] Update bug-report.yml --- .github/ISSUE_TEMPLATE/bug-report.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml index dd6c8ec0..31870df2 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.yml +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -7,7 +7,6 @@ body: value: | Thanks for taking the time to fill out this bug report! Before posting be sure that it's not a duplicate of any previous posted issues by using the search function. Please note that we do not support outdated versions. Always use the latest stable release. - - type: input id: bug-report-token attributes: @@ -15,7 +14,7 @@ body: description: | Generate a bug report using the `!bugreport` command and enter the token or URL here. This is a **required** step to create the bug report. - Note: when reporting multiple bugs, use the same bug report token as long as you've made no changes to the server. + When reporting multiple bugs, use the same bug report token as long as you've made no changes to the server or configuration. You can only create two bug reports every 24 hours. placeholder: https://report.funbots.dev/7e1..... validations: required: true From 111cb8d94198c3dc479d789ab86fc1fb89d13f33 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Sat, 7 Aug 2021 14:30:41 +0200 Subject: [PATCH 6/9] Testing debug --- ext/Server/Debug/BugReport.lua | 0 ext/Server/Debug/Debugger.lua | 33 +++++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 ext/Server/Debug/BugReport.lua diff --git a/ext/Server/Debug/BugReport.lua b/ext/Server/Debug/BugReport.lua new file mode 100644 index 00000000..e69de29b diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua index cc446d18..2cf113d1 100644 --- a/ext/Server/Debug/Debugger.lua +++ b/ext/Server/Debug/Debugger.lua @@ -12,27 +12,28 @@ local DEBUG_SUBMIT_PATH = "/api/submit" local pp = nil --- Generate a bug report and send it to the fun-bots bug report server -function Debugger:GenerateReport(p_Player) - -- Before submitting a report, check if we are eligable to generate a bug report. - -- Uneligable servers are outdated servers or those who've made too many reports in the past hour. +local s_CheckURL = "https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2" - ChatManager:Yell("Generating a new bug report...", 15.0, p_Player) - print("[DEBUG] " .. p_Player.name .. " is generating a new bug report.") +function TestGetHTTPAsync() + Net:GetHTTPAsync(s_CheckURL, function(p_HttpResponse) + if p_HttpResponse == nil then + print("[TestGetHTTPAsync] p_HttpResponse == nil") + end - if RCON:GetServerGuid() == nil then - print("[DEBUG] Bug report failed: Server GUID is unknown or not set.") - ChatManager:Yell("Failed to create bug report. Server GUID unknown", 4.0, p_Player) - do return end - end + if p_HttpResponse.status ~= 200 then + print("[TestGetHTTPAsync] p_HttpResponse.status ~= 200") + end - -- @Todo: Figure out how to pass this variable with GenerateReportCallback - pp = p_Player + local s_Json = json.decode(p_HttpResponse.body) - local s_UUID = RCON:GetServerGuid(). - print("UUID = " .. s_UUID) + print("[TestGetHTTPAsync]") + print(s_Json) + end) +end - Net:GetHTTPAsync(DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid(), GenerateReportCallback) +-- Generate a bug report and send it to the fun-bots bug report server +function Debugger:GenerateReport(p_Player) + TestGetHTTPAsync(); end function GenerateReportCallback(httpRequest) From 41e95304a4a45e329bfeb54f2c9533fadc130e69 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Fri, 20 Aug 2021 00:24:08 +0200 Subject: [PATCH 7/9] Added Timestamp and modified Debuger --- ext/Server/Debug/Debugger.lua | 149 +++++++++++++++++++-------------- ext/Shared/Utils/Timestamp.lua | 60 +++++++++++++ 2 files changed, 147 insertions(+), 62 deletions(-) diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua index 2cf113d1..65fc2d0e 100644 --- a/ext/Server/Debug/Debugger.lua +++ b/ext/Server/Debug/Debugger.lua @@ -1,92 +1,117 @@ class('Debugger') --- Basic debugger +-- Create a bug report using an in-game !bugreport command. +-- A bug report is required in order to create a new issue on the Github. -- Author: @Firjen on 12/07/21 +-- Debugger version: 1 -- The URL to the fun-bots report site local DEBUG_REPORT_URL = "https://report.funbots.dev" +-- Pre-check path (checks if a report can be made) local DEBUG_ELIGIABLE_PATH = "/api/precheck" +-- Path to submit a bug report local DEBUG_SUBMIT_PATH = "/api/submit" -local pp = nil +-- Timestamp of the latest report +local DEBUG_LAST_REPORT = 0 -local s_CheckURL = "https://report.funbots.dev/api/precheck?uuid=a5bb3716-1768-4367-b1ba-6b6ab0b7fbb2" +-- Cooldown between bug reports in milliesconds +local DEBUG_REPORT_COOLDOWN = 60*1000*2.5 -- 2.5 minutes cooldown -function TestGetHTTPAsync() - Net:GetHTTPAsync(s_CheckURL, function(p_HttpResponse) - if p_HttpResponse == nil then - print("[TestGetHTTPAsync] p_HttpResponse == nil") - end - - if p_HttpResponse.status ~= 200 then - print("[TestGetHTTPAsync] p_HttpResponse.status ~= 200") - end - - local s_Json = json.decode(p_HttpResponse.body) - - print("[TestGetHTTPAsync]") - print(s_Json) - end) -end - --- Generate a bug report and send it to the fun-bots bug report server +-- Ran with the !bugreport command. +-- p_Player - User who initiated the request function Debugger:GenerateReport(p_Player) - TestGetHTTPAsync(); -end - -function GenerateReportCallback(httpRequest) - -- @Todo: Figure out how to pass this variable with GenerateReportCallback - local p_Player = pp - - if httpRequest == nil then - print("[DEBUG] Bug report failed: HTTP request failure") - ChatManager:Yell("Failed to create bug report. HTTP request failure", 4.0, pp) + -- Check cooldown + if DEBUG_LAST_REPORT ~= nil and (DEBUG_LAST_REPORT - SharedUtils:GetTimeMS() > 0) then + ChatManager:Yell("A report was recently made, please wait " .. ReadableTimetamp((DEBUG_LAST_REPORT-SharedUtils:GetTimeMS()), TimeUnits.FIT, 1) .. " before creating a new report", 5.0, p_Player) do return end end - -- Check code returned - if httpRequest.status == 429 then - print("[DEBUG] Bug report failed: too many bug reports created. You can only create 2 reports per 24 hr.") - ChatManager:Yell("Failed to create bug report. Report limit reached (max 2 per 24hr)", 4.0, p_Player) - do return end - elseif httpRequest.status ~= 429 and httpRequest.status ~= 200 then - print("[DEBUG] Bug report failed: Report server returned error " .. httpRequest.status) - ChatManager:Yell("Failed to create bug report. HTTP code " .. httpRequest.status, 4.0, p_Player) + -- Set the cooldown + DEBUG_LAST_REPORT = SharedUtils:GetTimeMS() + DEBUG_REPORT_COOLDOWN + + -- Check that the server GUID is set (required) + if RCON:GetServerGuid() == nil then + ChatManager:Yell("Failed to create bug report. Server GUID is null", 5.0, p_Player) + print("[Debugger: Report] Failure to create a bug report. Server GUID is null."); + print("[Debugger: Report] This is not a fun-bots bug, please contact Venice Unleashed."); do return end end - -- If code 200, we can ask the server to create our report. - Debugger:CreateReport(p_Player) -end + -- Create a pre-check check. + print("[Debugger: Report] " .. p_Player.name .. " is creating a new bug report."); + ChatManager:Yell("Creating a new bug report...", 5.0, p_Player) -- Yell gets overwritten by a newer yell if available. --- Generate the report -function Debugger:CreateReport(p_Player) - -- Generate POST data - local postData = {config = json.encode(Config)} - print(postData) + local s_CheckURL = DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid():ToString("D"); + Net:GetHTTPAsync(s_CheckURL, function(p_HttpResponse) + if p_HttpResponse == nil then + ChatManager:Yell("Failed to create bug report. Unable to contact report server.", 5.0, p_Player) + print("[Debugger: Report] Failure to precheck for a bug report. Unable to contact report server. Please check your internet connection and try again later."); + do return end + end - local s_String = "config=" .. json.encode(Config) .. "&version=2.2.0" .. "author=" .. p_Player.name + local s_Json = json.decode(p_HttpResponse.body) - -- Send post data - Net:PostHTTPAsync(DEBUG_REPORT_URL .. DEBUG_REPORT_URL .. "?uuid=" .. RCON:GetServerGuid(), json.encode(Config), CreateReportCallback) -end + -- Too many requests, try again later + if p_HttpResponse.status == 429 then + ChatManager:Yell("Too many bug reports created, try again in " .. ReadableTimetamp(s_Json.try_again, TimeUnits.FIT, 1), 5.0, p_Player) + print("[Debugger: Report] Failure to precheck for a bug report. Too many bug reports created, try again in " .. ReadableTimetamp(s_Json.try_again, TimeUnits.FIT, 0)); + do return end + end -function CreateReportCallback(httpRequest) - if httpRequest == nil then - print("[DEBUG] Bug report failed: HTTP request failure") - ChatManager:Yell("Failed to create bug report. HTTP request failure 2", 4.0, pp) - do return end - end - - ChatManager:Yell("Bug report created?", 4.0, pp) -end + -- Not cooldown and not 200 + if p_HttpResponse.status ~= 200 then + ChatManager:Yell("Failed to create bug report. Returned HTTP code " .. p_HttpResponse.status, 5.0, p_Player) + print("[Debugger: Report] Failure to precheck for a bug report. Returned HTTP code " .. p_HttpResponse.status); + do return end + end + + + -- Report can be created as server returned error 200. + local s_SubmitURL = DEBUG_REPORT_URL .. DEBUG_SUBMIT_PATH .. "?uuid=" .. RCON:GetServerGuid():ToString("D"); + + -- Parse JSON data to POST + local s_ReportData = { + config = Config + } + + --[[ + + author_name = p_Player.name, + author_guid = p_Player.guid, + map_id = SharedUtils:GetLevelName(), + gamemode_id = SharedUtils:GetCurrentGameMode(), + tps = SharedUtils:GetTickrate(), + players_online = PlayerManager.GetPlayerCount() + ]] + + Net:PostHTTPAsync(s_SubmitURL, json.encode(s_ReportData), function(p_HttpResponse) + if p_HttpResponse == nil then + ChatManager:Yell("Failed to create bug report. Unable to contact report server.", 5.0, p_Player) + print("[Debugger: Report] Failure to post for a bug report. Unable to contact report server. Please check your internet connection and try again later."); + do return end + end + + -- Not cooldown and not 200 + if p_HttpResponse.status ~= 200 then + ChatManager:Yell("Failed to create bug report. Returned HTTP code " .. p_HttpResponse.status, 5.0, p_Player) + print("[Debugger: Report] Failure to post a bug report. Returned HTTP code " .. p_HttpResponse.status); + do return end + end + + print(p_HttpResponse.status) + print(p_HttpResponse.body) + end) + + end) + end function Debugger:__init() - local s_start = os.time(os.date("!*t")) + local s_start = SharedUtils:GetTimeMS() - print("Loaded debugger v2 in " .. os.time(os.date("!*t")) - s_start .. " ms") + print("Loaded debugger v2 in " .. ReadableTimetamp(os.time(os.date("!*t")) - s_start, TimeUnits.FIT, 1) .. " ms") return true end diff --git a/ext/Shared/Utils/Timestamp.lua b/ext/Shared/Utils/Timestamp.lua index 925616ab..e7e1923e 100644 --- a/ext/Shared/Utils/Timestamp.lua +++ b/ext/Shared/Utils/Timestamp.lua @@ -12,4 +12,64 @@ function ParseOffset(p_String) end return s_Timestamp + s_Offset +end + +-- Enums for different time units used to convert milliseconds into a readable format to the clients. +-- Author: Firjen (19/08/21) +TimeUnits = { + FIT = 99, + DAYS = 4, + HOURS = 3, + MINUTES = 2, + SECONDS = 1, +} + +-- Trim a large number to a smaller number with specified degree (numbers behind the comma) +-- Param: degree - Total numbers +-- Param: integer - number to trim +-- Author: Firjen (19/08/21) +-- Return: String [Double-like] (eg. 2.1, 1.6, 0.5) +function Trim(degree, integer) + return string.format("%.1f",integer) +end + +-- Convert to a readable format (eg. 5 Minutes, 12 Seconds, etc.) +-- Author: Firjen (19/08/21) +-- Return: String (Eg. Permanent, 12 Seconds, 5 Minutes) +function ReadableTimetamp(p_Time, p_TimeUnit, p_Trim) + -- When time is equal to 0 it's now + if p_Time == 0 then + return "Now" + end + + -- Anything less than 0 (eg. time is -1), it's considered permanent + if p_Time < 0 then + return "Permanent" + end + + -- If fit pick the best timeunit + if p_TimeUnit == TimeUnits.FIT then + if p_Time < 60000 then + p_TimeUnit = TimeUnits.SECONDS + elseif p_Time < 3600000 then + p_TimeUnit = TimeUnits.MINUTES + elseif p_Time < 86400000 then + p_TimeUnit = TimeUnits.HOURS + else + p_TimeUnit = TimeUnits.DAYS + end + end + + + if p_TimeUnit == TimeUnits.DAYS then + return Trim(p_Trim, p_Time / 8.64E7) .. " Days" + elseif p_TimeUnit == TimeUnits.HOURS then + return Trim(p_Trim, p_Time / 3600000) .. " Hours" + elseif p_TimeUnit == TimeUnits.MINUTES then + return Trim(p_Trim, p_Time / 86400000) .. " Minutes" + elseif p_TimeUnit == TimeUnits.SECONDS then + return Trim(p_Trim, p_Time / 1000) .. " Seconds" + else + return Trim(p_Trim, p_Time) .. " Milliseconds" + end end \ No newline at end of file From 71a6125ab652c72d1f447ef0d13dfa715cac55a6 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Fri, 20 Aug 2021 16:47:11 +0200 Subject: [PATCH 8/9] Finished up debugger Sorry for the bad commits :( --- ext/Server/Debug/Debugger.lua | 80 ++++++++++++++++++++-------------- ext/Shared/Utils/Timestamp.lua | 3 +- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/ext/Server/Debug/Debugger.lua b/ext/Server/Debug/Debugger.lua index 65fc2d0e..0c49913a 100644 --- a/ext/Server/Debug/Debugger.lua +++ b/ext/Server/Debug/Debugger.lua @@ -2,10 +2,14 @@ class('Debugger') -- Create a bug report using an in-game !bugreport command. -- A bug report is required in order to create a new issue on the Github. --- Author: @Firjen on 12/07/21 --- Debugger version: 1 +-- Abuse of the bug reporting system will lead into getting suspended from all fun-bots services, including support. +-- Do not change this without input from @Firjens (unless minor or text changes) to prevent unwanted behavior. +-- Author: @Firjen on 12/07/21 (finished on 20/08/21) for V2.3.0 --- The URL to the fun-bots report site +-- The version used for API related things. The moment new features are required for debugging the REST API end-point will know that this is a newer or older version. +local DEBUG_VERSION = 1 + +-- The URL to the official reporting website. local DEBUG_REPORT_URL = "https://report.funbots.dev" -- Pre-check path (checks if a report can be made) @@ -14,25 +18,25 @@ local DEBUG_ELIGIABLE_PATH = "/api/precheck" -- Path to submit a bug report local DEBUG_SUBMIT_PATH = "/api/submit" --- Timestamp of the latest report +-- Timestamp of the latest report created by anyone with permission. Uesd for reporting cooldown local DEBUG_LAST_REPORT = 0 --- Cooldown between bug reports in milliesconds -local DEBUG_REPORT_COOLDOWN = 60*1000*2.5 -- 2.5 minutes cooldown +-- Cooldown between bug reports in milliseconds. You are limited to a certain reports per 24 hours, and it's useless to create a new report when no major changes were made. +local DEBUG_REPORT_COOLDOWN = 60*1000*3 -- 3 minutes cooldown --- Ran with the !bugreport command. --- p_Player - User who initiated the request +-- This function is solely run by someone with permissions running the in-game !bugreport command. +-- Param: p_Player - User who initiated the request function Debugger:GenerateReport(p_Player) - -- Check cooldown + -- Check if there is a current cooldown active, if so, return and tell the user. if DEBUG_LAST_REPORT ~= nil and (DEBUG_LAST_REPORT - SharedUtils:GetTimeMS() > 0) then ChatManager:Yell("A report was recently made, please wait " .. ReadableTimetamp((DEBUG_LAST_REPORT-SharedUtils:GetTimeMS()), TimeUnits.FIT, 1) .. " before creating a new report", 5.0, p_Player) do return end end - -- Set the cooldown + -- Set the current cooldown. There is no point in spamming it if the servers are down. DEBUG_LAST_REPORT = SharedUtils:GetTimeMS() + DEBUG_REPORT_COOLDOWN - -- Check that the server GUID is set (required) + -- Check that the server GUID is correctly set. if RCON:GetServerGuid() == nil then ChatManager:Yell("Failed to create bug report. Server GUID is null", 5.0, p_Player) print("[Debugger: Report] Failure to create a bug report. Server GUID is null."); @@ -40,12 +44,15 @@ function Debugger:GenerateReport(p_Player) do return end end - -- Create a pre-check check. + -- Notify the user that we are creating a new bug report print("[Debugger: Report] " .. p_Player.name .. " is creating a new bug report."); - ChatManager:Yell("Creating a new bug report...", 5.0, p_Player) -- Yell gets overwritten by a newer yell if available. + ChatManager:Yell("Creating a new bug report...", 5.0, p_Player) + -- Contact the eligiable end point to see if we are allowed to even make a bug report. We may be rate limited? We may be suspended because we abused it? + -- No need to send the whole config to one end point as this can use precious server resources and networking. local s_CheckURL = DEBUG_REPORT_URL .. DEBUG_ELIGIABLE_PATH .. "?uuid=" .. RCON:GetServerGuid():ToString("D"); Net:GetHTTPAsync(s_CheckURL, function(p_HttpResponse) + -- Check if the server has answered, even upon an error a JSON payload is returned with a message, if this isn't the case it's most likely the server that failed to contact it. if p_HttpResponse == nil then ChatManager:Yell("Failed to create bug report. Unable to contact report server.", 5.0, p_Player) print("[Debugger: Report] Failure to precheck for a bug report. Unable to contact report server. Please check your internet connection and try again later."); @@ -54,39 +61,37 @@ function Debugger:GenerateReport(p_Player) local s_Json = json.decode(p_HttpResponse.body) - -- Too many requests, try again later + -- Rate limiting, a limited amount of reports are allowed to be made within 24 hours. if p_HttpResponse.status == 429 then ChatManager:Yell("Too many bug reports created, try again in " .. ReadableTimetamp(s_Json.try_again, TimeUnits.FIT, 1), 5.0, p_Player) print("[Debugger: Report] Failure to precheck for a bug report. Too many bug reports created, try again in " .. ReadableTimetamp(s_Json.try_again, TimeUnits.FIT, 0)); do return end end - -- Not cooldown and not 200 + -- Other than rate limiting or OK means that the server has some kind of issue. if p_HttpResponse.status ~= 200 then ChatManager:Yell("Failed to create bug report. Returned HTTP code " .. p_HttpResponse.status, 5.0, p_Player) print("[Debugger: Report] Failure to precheck for a bug report. Returned HTTP code " .. p_HttpResponse.status); do return end end - -- Report can be created as server returned error 200. local s_SubmitURL = DEBUG_REPORT_URL .. DEBUG_SUBMIT_PATH .. "?uuid=" .. RCON:GetServerGuid():ToString("D"); - -- Parse JSON data to POST + -- This should not be modified as the server checks everything, parses it and validates it. + -- When something misses or gets added the server will reject the request. local s_ReportData = { - config = Config - } - - --[[ - + debugversion = DEBUG_VERSION, + config = Config, + staticconfig = StaticConfig, + version = Config.Version.Tag, author_name = p_Player.name, author_guid = p_Player.guid, map_id = SharedUtils:GetLevelName(), - gamemode_id = SharedUtils:GetCurrentGameMode(), - tps = SharedUtils:GetTickrate(), - players_online = PlayerManager.GetPlayerCount() - ]] + gamemode_id = SharedUtils:GetCurrentGameMode() + } + -- Post the bug report information to the server Net:PostHTTPAsync(s_SubmitURL, json.encode(s_ReportData), function(p_HttpResponse) if p_HttpResponse == nil then ChatManager:Yell("Failed to create bug report. Unable to contact report server.", 5.0, p_Player) @@ -94,17 +99,26 @@ function Debugger:GenerateReport(p_Player) do return end end - -- Not cooldown and not 200 - if p_HttpResponse.status ~= 200 then - ChatManager:Yell("Failed to create bug report. Returned HTTP code " .. p_HttpResponse.status, 5.0, p_Player) - print("[Debugger: Report] Failure to post a bug report. Returned HTTP code " .. p_HttpResponse.status); + local s_Json = json.decode(p_HttpResponse.body) + + -- 403 forbidden contains a message why it's forbidden. Show to user. + if p_HttpResponse.status == 403 then + ChatManager:Yell("Failed to create bug report. Server returned: " .. s_Json.message, 5.0, p_Player) + print("[Debugger: Report] Failure to post a bug report. Returned HTTP code " .. p_HttpResponse.status .. " with message: " .. s_Json.message); do return end end - print(p_HttpResponse.status) - print(p_HttpResponse.body) - end) + -- If token fetch token and return token to user + if p_HttpResponse.status == 200 and s_Json.token ~= nil then + ChatManager:Yell("Success! Your report token is: " .. s_Json.token, 45.0, p_Player) + ChatManager:SendMessage("Bug report created: report.funbots.dev/" .. s_Json.token, p_Player) + print("[Debugger: Report] A new bug report is created and can be viewed on: report.funbots.dev/" .. s_Json.token); + do return end + end + ChatManager:Yell("Failed to create bug report. Returned HTTP code " .. p_HttpResponse.status, 5.0, p_Player) + print("[Debugger: Report] Failure to post a bug report. Returned HTTP code " .. p_HttpResponse.status .. " with message: " .. s_Json.message); + end) end) end diff --git a/ext/Shared/Utils/Timestamp.lua b/ext/Shared/Utils/Timestamp.lua index e7e1923e..40e12663 100644 --- a/ext/Shared/Utils/Timestamp.lua +++ b/ext/Shared/Utils/Timestamp.lua @@ -66,9 +66,10 @@ function ReadableTimetamp(p_Time, p_TimeUnit, p_Trim) elseif p_TimeUnit == TimeUnits.HOURS then return Trim(p_Trim, p_Time / 3600000) .. " Hours" elseif p_TimeUnit == TimeUnits.MINUTES then - return Trim(p_Trim, p_Time / 86400000) .. " Minutes" + return Trim(p_Trim, p_Time / 60000.) .. " Minutes" elseif p_TimeUnit == TimeUnits.SECONDS then return Trim(p_Trim, p_Time / 1000) .. " Seconds" + else return Trim(p_Trim, p_Time) .. " Milliseconds" end From 4b2712afee9c33fd6d9e620c045393f111442434 Mon Sep 17 00:00:00 2001 From: Firjen <57008952+Firjens@users.noreply.github.com> Date: Fri, 20 Aug 2021 16:59:30 +0200 Subject: [PATCH 9/9] Update bug-report.yml --- .github/ISSUE_TEMPLATE/bug-report.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml index 31870df2..be549b57 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.yml +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -13,9 +13,9 @@ body: label: Bug report token description: | Generate a bug report using the `!bugreport` command and enter the token or URL here. - This is a **required** step to create the bug report. - When reporting multiple bugs, use the same bug report token as long as you've made no changes to the server or configuration. You can only create two bug reports every 24 hours. - placeholder: https://report.funbots.dev/7e1..... + This is a **required** step to create the bug report, any bug report without this token will be closed. + When reporting multiple bugs, use the same bug report token as long as you've made no changes to the server or configuration. You can only create 3 bug reports every 24 hours. + placeholder: https://report.funbots.dev/7e1... validations: required: true - type: textarea