full remake of http request and change auth method

This commit is contained in:
Linventif 2023-09-23 02:25:25 +02:00
parent 7205dbe74a
commit 64cab37168
2 changed files with 75 additions and 112 deletions

View File

@ -30,6 +30,7 @@ function gmInte.saveSetting(setting, value)
gmInte.log("Unknown Setting") gmInte.log("Unknown Setting")
return return
end end
gmInte.config[setting] = value gmInte.config[setting] = value
file.Write("gm_integration/config.json", util.TableToJSON(gmInte.config, true)) file.Write("gm_integration/config.json", util.TableToJSON(gmInte.config, true))
gmInte.log("Setting Saved") gmInte.log("Setting Saved")
@ -38,13 +39,15 @@ end
function gmInte.playerConnect(data) function gmInte.playerConnect(data)
if (data.bot == 1) then return end if (data.bot == 1) then return end
data.steam = util.SteamIDTo64(data.networkid) data.steam = util.SteamIDTo64(data.networkid)
gmInte.simplePost("userConnect", data)
gmInte.post("/server/user/connect", data)
end end
local function triggerChat(text) local function triggerChat(text)
for (k, v) in pairs(gmInte.config.chatTrigger) do for k, v in pairs(gmInte.config.chatTrigger) do
if (string.StartWith(text, v)) then return true end if (string.StartWith(text, k)) then return true end
end end
return false return false
end end
@ -52,34 +55,35 @@ function gmInte.playerSay(ply, text, team)
if (!gmInte.config.syncChat) then return end if (!gmInte.config.syncChat) then return end
if (!triggerChat(text) && !gmInte.config.chatTriggerAll) then return end if (!triggerChat(text) && !gmInte.config.chatTriggerAll) then return end
gmInte.simplePost("userSay", gmInte.post("/server/user/say",
{ {
steam = ply:SteamID64(), ["steam"] = ply:SteamID64(),
text = text ["text"] = text
} }
) )
end end
function gmInte.userFinishConnect(ply) function gmInte.userFinishConnect(ply)
if (!gmInte.plyValid(ply)) then return end if (!gmInte.plyValid(ply)) then return end
gmInte.simplePost("userFinishConnect",
gmInte.post("/server/user/finishConnect",
{ {
steam = ply:SteamID64(), // essential ["steam"] = ply:SteamID64(), // essential
name = ply:Nick(), // for the syncro name ["name"] = ply:Nick(), // for the syncro name
} }
) )
end end
function gmInte.sendStatus() function gmInte.sendStatus()
gmInte.simplePost("serverStatus", gmInte.post("/server/status",
{ {
hostname = GetHostName(), ["hostname"] = GetHostName(),
ip = game.GetIPAddress(), ["ip"] = game.GetIPAddress(),
port = GetConVar("hostport"):GetInt(), ["port"] = GetConVar("hostport"):GetInt(),
map = game.GetMap(), ["map"] = game.GetMap(),
players = #player.GetAll(), ["players"] = #player.GetAll(),
maxplayers = game.MaxPlayers(), ["maxplayers"] = game.MaxPlayers(),
gamemode = engine.ActiveGamemode(), ["gamemode"] = engine.ActiveGamemode()
} }
) )
end end
@ -91,30 +95,32 @@ end)
function gmInte.playerChangeName(ply, old, new) function gmInte.playerChangeName(ply, old, new)
if (!gmInte.plyValid(ply)) then return end if (!gmInte.plyValid(ply)) then return end
gmInte.simplePost("userChangeName",
gmInte.post("/server/user/changeName",
{ {
steam = ply:SteamID64(), ["steam"] = ply:SteamID64(),
old = old, ["old"] = old,
new = new, ["new"] = new,
} }
) )
end end
function gmInte.playerDisconnected(ply) function gmInte.playerDisconnected(ply)
if (!gmInte.plyValid(ply)) then return end if (!gmInte.plyValid(ply)) then return end
gmInte.simplePost("userDisconnect",
gmInte.post("/server/user/disconnect",
{ {
steam = ply:SteamID64(), ["steam"] = ply:SteamID64(),
kills = ply:Frags() || 0, ["kills"] = ply:Frags() || 0,
deaths = ply:Deaths() || 0, ["deaths"] = ply:Deaths() || 0,
money = ply:gmInteGetTotalMoney(), ["money"] = ply:gmInteGetTotalMoney(),
rank = ply:GetUserGroup() || "user", ["rank"] = ply:GetUserGroup() || "user",
} }
) )
end end
function gmInte.tryConfig() function gmInte.tryConfig()
gmInte.simplePost("tryConfig", {}, gmInte.post("/server/guild", {},
function( body, length, headers, code) function( body, length, headers, code)
gmInte.log("GG you are authorized, the link discord guild is: " .. body) gmInte.log("GG you are authorized, the link discord guild is: " .. body)
end) end)

View File

@ -1,12 +1,7 @@
// //
// HTTP
//
// Variables
gmInte.api = 'https://api.gmod-integration.com/'
gmInte.defParams = "&version=" .. gmInte.version
// Functions // Functions
//
function gmInte.isCodeValid(code) function gmInte.isCodeValid(code)
// if first number is 2 // if first number is 2
return string.sub(code, 1, 1) == "2" return string.sub(code, 1, 1) == "2"
@ -17,89 +12,53 @@ function gmInte.httpError(error)
gmInte.log("Error details: "..error) gmInte.log("Error details: "..error)
end end
function gmInte.ulrGenerate(endpoint, parameters) //
local params = "=" // HTTP
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
function gmInte.fetch(endpoint, parameters, onSuccess) local function sendHTTP(params)
gmInte.log("Fetching " .. endpoint, true) HTTP({
http.Fetch( url = "https://api.gmod-integration.com" .. params.endpoint,
// URL method = params.method,
gmInte.ulrGenerate(endpoint, parameters),
// onSuccess
function (body, length, headers, code )
if gmInte.isCodeValid(code) then
onSuccess(body, length, headers, code)
else
gmInte.httpError(body)
end
end,
gmInte.httpError
)
end
function gmInte.post(endpoint, parameters, data, onSuccess)
local bodyData = util.TableToJSON(data)
gmInte.log("Posting " .. endpoint, true)
HTTP(
{
url = gmInte.ulrGenerate(endpoint, parameters),
method = "POST",
headers = { headers = {
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
["Content-Length"] = tostring(#bodyData), ["Content-Length"] = tostring(#params.body),
["id"] = gmInte.config.id,
["token"] = gmInte.config.token,
["version"] = gmInte.version
}, },
body = bodyData, body = params.body,
type = "application/json", type = "application/json",
success = function(code, body, headers) success = function(code, body, headers)
if (gmInte.isCodeValid(code)) then if (gmInte.isCodeValid(code)) then
if (onSuccess) then if (params.success) then
onSuccess(body, length, headers, code) params.success(code, body, headers)
end end
else else
gmInte.httpError(body) gmInte.httpError(body)
end end
end, end,
failed = gmInte.httpError, failed = gmInte.httpError,
} })
)
end end
function gmInte.simplePost(request_id, data, onSuccess) function gmInte.fetch(endpoint, parameters, onSuccess)
gmInte.post( gmInte.log("Fetching " .. endpoint, true)
"", sendHTTP({
{ endpoint = endpoint .. "?" .. util.TableToJSON(parameters) .. gmInte.defParams,
request = request_id method = "GET",
}, success = onSuccess
data, })
function( body, length, headers, code )
if (onSuccess) then
onSuccess(body, length, headers, code)
end
end
)
end end
function gmInte.simpleFetch(request_id, onSuccess) function gmInte.post(endpoint, data, onSuccess)
gmInte.fetch( gmInte.log("Posting " .. endpoint, true)
"", sendHTTP({
{ endpoint = endpoint,
request = request_id method = "POST",
}, body = util.TableToJSON(data),
function( body, length, headers, code ) success = onSuccess
if (onSuccess) then })
onSuccess(body, length, headers, code)
end
end
)
end end
/* /*
@ -119,8 +78,6 @@ gmInte.fetch(
gmInte.post( gmInte.post(
// Endpoint // Endpoint
"", "",
// Parameters
{ request = "requ" },
// Data // Data
{ {
data = "data" data = "data"