Update bunkernet.lua and bunkernet.py with OS information

This commit is contained in:
Théophile Diot 2024-02-19 16:23:25 +01:00
parent b651a9ca46
commit 39ab366eba
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 75 additions and 5 deletions

View file

@ -204,9 +204,20 @@ function bunkernet:log(bypass_checks)
end
local ok, err
-- luacheck: ignore 212 431
local function report_callback(premature, obj, ip, reason, reason_data, method, url, headers, use_redis)
local function report_callback(
premature,
obj,
ip,
reason,
reason_data,
method,
url,
headers,
server_name,
use_redis
)
local status, _
ok, err, status, _ = obj:report(ip, reason, reason_data, method, url, headers)
ok, err, status, _ = obj:report(ip, reason, reason_data, method, url, headers, server_name)
if status == 429 then
obj.logger:log(WARN, "bunkernet API is rate limiting us")
elseif not ok then
@ -229,7 +240,8 @@ function bunkernet:log(bypass_checks)
reason_data,
self.ctx.bw.request_method,
self.ctx.bw.request_uri,
ngx.req.get_headers()
ngx.req.get_headers(),
self.ctx.bw.server_name
)
if not hdr then
return self:ret(false, "can't create report timer : " .. err)
@ -263,10 +275,48 @@ function bunkernet:request(method, url, data)
if not httpc then
return false, "can't instantiate http object : " .. err
end
local os_data = {
name = "Linux",
version = "Unknown",
version_id = "Unknown",
version_codename = "Unknown",
id = "Unknown",
arch = "Unknown",
}
local uname_cmd = io.popen("uname -m")
if uname_cmd then
os_data.arch = uname_cmd:read("*a"):gsub("\n", "")
uname_cmd:close()
end
local file = io.open("/etc/os-release", "r")
if file then
for line in file:lines() do
local key, value = line:match("^(%w+)=(.+)$")
if key and value then
value = value:gsub('"', "")
if key == "NAME" then
os_data.name = value
elseif key == "VERSION" then
os_data.version = value
elseif key == "VERSION_ID" then
os_data.version_id = value
elseif key == "VERSION_CODENAME" then
os_data.version_codename = value
elseif key == "ID" then
os_data.id = value
end
end
end
file:close()
end
local all_data = {
id = self.bunkernet_id,
version = self.version,
integration = self.integration,
os = os_data,
}
if data then
for k, v in pairs(data) do
@ -299,7 +349,7 @@ function bunkernet:ping()
return self:request("GET", "/ping", {})
end
function bunkernet:report(ip, reason, reason_data, method, url, headers)
function bunkernet:report(ip, reason, reason_data, method, url, headers, server_name)
local data = {
ip = ip,
reason = reason,
@ -307,6 +357,7 @@ function bunkernet:report(ip, reason, reason_data, method, url, headers)
method = method,
url = url,
headers = headers,
server_name = server_name,
}
return self:request("POST", "/report", data)
end

View file

@ -2,12 +2,31 @@
from os import getenv, sep
from pathlib import Path
from platform import machine
from requests import request as requests_request, ReadTimeout
from typing import Literal, Optional, Tuple, Union
def request(method: Union[Literal["POST"], Literal["GET"]], url: str, _id: Optional[str] = None) -> Tuple[bool, Optional[int], Union[str, dict]]:
data = {"integration": get_integration(), "version": get_version()}
data = {
"integration": get_integration(),
"version": get_version(),
"os": {
"name": "Linux",
"version": "Unknown",
"version_id": "Unknown",
"version_codename": "Unknown",
"id": "Unknown",
"arch": machine(),
},
}
os_release = Path("/etc/os-release")
if os_release.exists():
for line in os_release.read_text().splitlines():
if "=" not in line or line.split("=")[0].strip().lower() not in data["os"]:
continue
data["os"][line.split("=")[0].lower()] = line.split("=")[1].strip('"')
headers = {"User-Agent": f"BunkerWeb/{get_version()}"}
if _id is not None:
data["id"] = _id