From 64cab3716829a62bcc7bb104abef9126498d22e3 Mon Sep 17 00:00:00 2001 From: Linventif Date: Sat, 23 Sep 2023 02:25:25 +0200 Subject: [PATCH] full remake of http request and change auth method --- lua/gmod_integration/server/sv_main.lua | 62 ++++++------ lua/gmod_integration/shared/sh_http.lua | 125 ++++++++---------------- 2 files changed, 75 insertions(+), 112 deletions(-) diff --git a/lua/gmod_integration/server/sv_main.lua b/lua/gmod_integration/server/sv_main.lua index c1139d5..6f96a94 100644 --- a/lua/gmod_integration/server/sv_main.lua +++ b/lua/gmod_integration/server/sv_main.lua @@ -30,6 +30,7 @@ function gmInte.saveSetting(setting, value) gmInte.log("Unknown Setting") return end + gmInte.config[setting] = value file.Write("gm_integration/config.json", util.TableToJSON(gmInte.config, true)) gmInte.log("Setting Saved") @@ -38,13 +39,15 @@ end function gmInte.playerConnect(data) if (data.bot == 1) then return end data.steam = util.SteamIDTo64(data.networkid) - gmInte.simplePost("userConnect", data) + + gmInte.post("/server/user/connect", data) end local function triggerChat(text) - for (k, v) in pairs(gmInte.config.chatTrigger) do - if (string.StartWith(text, v)) then return true end + for k, v in pairs(gmInte.config.chatTrigger) do + if (string.StartWith(text, k)) then return true end end + return false end @@ -52,34 +55,35 @@ function gmInte.playerSay(ply, text, team) if (!gmInte.config.syncChat) then return end if (!triggerChat(text) && !gmInte.config.chatTriggerAll) then return end - gmInte.simplePost("userSay", + gmInte.post("/server/user/say", { - steam = ply:SteamID64(), - text = text + ["steam"] = ply:SteamID64(), + ["text"] = text } ) end function gmInte.userFinishConnect(ply) if (!gmInte.plyValid(ply)) then return end - gmInte.simplePost("userFinishConnect", + + gmInte.post("/server/user/finishConnect", { - steam = ply:SteamID64(), // essential - name = ply:Nick(), // for the syncro name + ["steam"] = ply:SteamID64(), // essential + ["name"] = ply:Nick(), // for the syncro name } ) end function gmInte.sendStatus() - gmInte.simplePost("serverStatus", + gmInte.post("/server/status", { - hostname = GetHostName(), - ip = game.GetIPAddress(), - port = GetConVar("hostport"):GetInt(), - map = game.GetMap(), - players = #player.GetAll(), - maxplayers = game.MaxPlayers(), - gamemode = engine.ActiveGamemode(), + ["hostname"] = GetHostName(), + ["ip"] = game.GetIPAddress(), + ["port"] = GetConVar("hostport"):GetInt(), + ["map"] = game.GetMap(), + ["players"] = #player.GetAll(), + ["maxplayers"] = game.MaxPlayers(), + ["gamemode"] = engine.ActiveGamemode() } ) end @@ -91,30 +95,32 @@ end) function gmInte.playerChangeName(ply, old, new) if (!gmInte.plyValid(ply)) then return end - gmInte.simplePost("userChangeName", + + gmInte.post("/server/user/changeName", { - steam = ply:SteamID64(), - old = old, - new = new, + ["steam"] = ply:SteamID64(), + ["old"] = old, + ["new"] = new, } ) end function gmInte.playerDisconnected(ply) if (!gmInte.plyValid(ply)) then return end - gmInte.simplePost("userDisconnect", + + gmInte.post("/server/user/disconnect", { - steam = ply:SteamID64(), - kills = ply:Frags() || 0, - deaths = ply:Deaths() || 0, - money = ply:gmInteGetTotalMoney(), - rank = ply:GetUserGroup() || "user", + ["steam"] = ply:SteamID64(), + ["kills"] = ply:Frags() || 0, + ["deaths"] = ply:Deaths() || 0, + ["money"] = ply:gmInteGetTotalMoney(), + ["rank"] = ply:GetUserGroup() || "user", } ) end function gmInte.tryConfig() - gmInte.simplePost("tryConfig", {}, + gmInte.post("/server/guild", {}, function( body, length, headers, code) gmInte.log("GG you are authorized, the link discord guild is: " .. body) end) diff --git a/lua/gmod_integration/shared/sh_http.lua b/lua/gmod_integration/shared/sh_http.lua index da0830c..c81990b 100644 --- a/lua/gmod_integration/shared/sh_http.lua +++ b/lua/gmod_integration/shared/sh_http.lua @@ -1,12 +1,7 @@ // -// HTTP -// - -// Variables -gmInte.api = 'https://api.gmod-integration.com/' -gmInte.defParams = "&version=" .. gmInte.version - // Functions +// + function gmInte.isCodeValid(code) // if first number is 2 return string.sub(code, 1, 1) == "2" @@ -17,89 +12,53 @@ function gmInte.httpError(error) gmInte.log("Error details: "..error) end -function gmInte.ulrGenerate(endpoint, parameters) - local params = "=" - for k, v in pairs(parameters) do - params = params .. "&" .. k .. "=" .. v - end - if SERVER then - return gmInte.api .. endpoint .. "?" .. params .. gmInte.defParams .. "&id=" .. gmInte.config.id .. "&token=" .. gmInte.config.token - else - return gmInte.api .. endpoint .. "?" .. params .. gmInte.defParams - end -end +// +// HTTP +// -function gmInte.fetch(endpoint, parameters, onSuccess) - gmInte.log("Fetching " .. endpoint, true) - http.Fetch( - // URL - gmInte.ulrGenerate(endpoint, parameters), - // onSuccess - function (body, length, headers, code ) - if gmInte.isCodeValid(code) then - onSuccess(body, length, headers, code) +local function sendHTTP(params) + HTTP({ + url = "https://api.gmod-integration.com" .. params.endpoint, + method = params.method, + headers = { + ["Content-Type"] = "application/json", + ["Content-Length"] = tostring(#params.body), + ["id"] = gmInte.config.id, + ["token"] = gmInte.config.token, + ["version"] = gmInte.version + }, + body = params.body, + type = "application/json", + success = function(code, body, headers) + if (gmInte.isCodeValid(code)) then + if (params.success) then + params.success(code, body, headers) + end else gmInte.httpError(body) end end, - gmInte.httpError - ) + failed = gmInte.httpError, + }) end -function gmInte.post(endpoint, parameters, data, onSuccess) - local bodyData = util.TableToJSON(data) +function gmInte.fetch(endpoint, parameters, onSuccess) + gmInte.log("Fetching " .. endpoint, true) + sendHTTP({ + endpoint = endpoint .. "?" .. util.TableToJSON(parameters) .. gmInte.defParams, + method = "GET", + success = onSuccess + }) +end + +function gmInte.post(endpoint, data, onSuccess) gmInte.log("Posting " .. endpoint, true) - HTTP( - { - url = gmInte.ulrGenerate(endpoint, parameters), - method = "POST", - headers = { - ["Content-Type"] = "application/json", - ["Content-Length"] = tostring(#bodyData), - }, - body = bodyData, - type = "application/json", - success = function(code, body, headers) - if (gmInte.isCodeValid(code)) then - if (onSuccess) then - onSuccess(body, length, headers, code) - end - else - gmInte.httpError(body) - end - end, - failed = gmInte.httpError, - } - ) -end - -function gmInte.simplePost(request_id, data, onSuccess) - gmInte.post( - "", - { - request = request_id - }, - data, - function( body, length, headers, code ) - if (onSuccess) then - onSuccess(body, length, headers, code) - end - end - ) -end - -function gmInte.simpleFetch(request_id, onSuccess) - gmInte.fetch( - "", - { - request = request_id - }, - function( body, length, headers, code ) - if (onSuccess) then - onSuccess(body, length, headers, code) - end - end - ) + sendHTTP({ + endpoint = endpoint, + method = "POST", + body = util.TableToJSON(data), + success = onSuccess + }) end /* @@ -119,8 +78,6 @@ gmInte.fetch( gmInte.post( // Endpoint "", - // Parameters - { request = "requ" }, // Data { data = "data"