From 0ae9631880069d1fabcae2e6941ac9c311f873bd Mon Sep 17 00:00:00 2001 From: Linventif Date: Fri, 28 Jun 2024 18:46:43 +0000 Subject: [PATCH] add: isPrivateIP and allow http on private IP --- lua/gmod_integration/shared/sh_http.lua | 4 ++-- lua/gmod_integration/shared/sh_main.lua | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lua/gmod_integration/shared/sh_http.lua b/lua/gmod_integration/shared/sh_http.lua index e5a2667..b4a1ba8 100644 --- a/lua/gmod_integration/shared/sh_http.lua +++ b/lua/gmod_integration/shared/sh_http.lua @@ -2,7 +2,7 @@ local apiVersion = "v3" gmInte.http = gmInte.http || {} local function getAPIURL(endpoint) - local url = "https://" .. gmInte.config.apiFQDN .. "/" .. apiVersion + local method = gmInte.isPrivateIP(gmInte.config.apiFQDN) && "http" || "https" endpoint = string.gsub(endpoint, ":serverID", gmInte.config.id) @@ -10,7 +10,7 @@ local function getAPIURL(endpoint) endpoint = string.gsub(endpoint, ":steamID64", LocalPlayer():SteamID64()) end - return url .. endpoint + return method .. "://" .. gmInte.config.apiFQDN .. "/" .. apiVersion .. endpoint end local function showableBody(endpoint) diff --git a/lua/gmod_integration/shared/sh_main.lua b/lua/gmod_integration/shared/sh_main.lua index 280f944..f9c5f83 100644 --- a/lua/gmod_integration/shared/sh_main.lua +++ b/lua/gmod_integration/shared/sh_main.lua @@ -24,4 +24,17 @@ end function gmInte.logHint(msg, debug) if (debug && !gmInte.config.debug) then return end print("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] [Gmod Integration] [HINT] " .. msg) +end + +// Is Private IP +function gmInte.isPrivateIP(ip) + // detect localhost + if (ip == "localhost") then return true end + // detect private IP addresses (RFC 1918) + local parts = string.Explode(".", ip) + if (parts[1] == "192" && parts[2] == "168") then return true end + if (parts[1] == "10") then return true end + if (parts[1] == "172" && tonumber(parts[2]) >= 16 && tonumber(parts[2]) <= 31) then return true end + if (parts[1] == "127") then return true end + return false end \ No newline at end of file