Add anonymous-report job

This commit is contained in:
Théophile Diot 2023-12-07 11:18:18 +01:00
parent 4c7bc9f99e
commit 74dff76651
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
3 changed files with 85 additions and 0 deletions

View file

@ -0,0 +1,59 @@
#!/usr/bin/python3
from os import getenv, sep
from os.path import join
from sys import exit as sys_exit, path as sys_path
from traceback import format_exc
from typing import Any, Dict
for deps_path in [
join(sep, "usr", "share", "bunkerweb", *paths)
for paths in (
("deps", "python"),
("utils",),
("db",),
)
]:
if deps_path not in sys_path:
sys_path.append(deps_path)
from Database import Database # type: ignore
from logger import setup_logger # type: ignore
from requests import post
logger = setup_logger("ANONYMOUS-REPORT", getenv("LOG_LEVEL", "INFO"))
status = 0
if getenv("SEND_ANONYMOUS_REPORT", "yes") != "yes":
logger.info("Skipping the sending of anonymous report (disabled)")
sys_exit(status)
try:
db = Database(logger, sqlalchemy_string=getenv("DATABASE_URI", None), pool=False)
# ? Get version and integration of BunkerWeb
data: Dict[str, Any] = db.get_metadata()
data["integration"] = data["integration"].lower()
data["database"] = db.database_uri.split(":")[0].split("+")[0]
data["service_number"] = str(len(getenv("SERVER_NAME", "").split(" ")))
data["use_ui"] = getenv("USE_UI", "no")
if data["use_ui"] == "no":
for server in getenv("SERVER_NAME", "").split(" "):
if getenv(f"{server}_USE_UI", "no") == "yes":
data["use_ui"] = "yes"
break
data["external_plugins"] = [plugin["id"] for plugin in db.get_plugins(external=True)]
response = post(
"http://api:8080/data",
json=data,
headers={"User-Agent": f"BunkerWeb/{data['version']}"},
allow_redirects=True,
timeout=10,
)
response.raise_for_status()
except:
status = 2
logger.error(f"Exception while running anonymous-report.py :\n{format_exc()}")
sys_exit(status)

View file

@ -158,6 +158,15 @@
"regex": "^(403|444)$",
"type": "select",
"select": ["403", "444"]
},
"SEND_ANONYMOUS_REPORT": {
"context": "global",
"default": "yes",
"help": "Send anonymous report to BunkerWeb maintainers.",
"id": "send-anonymous-report",
"label": "Send anonymous report",
"regex": "^(yes|no)$",
"type": "check"
}
},
"jobs": [
@ -172,6 +181,12 @@
"file": "update-check.py",
"every": "day",
"reload": false
},
{
"name": "anonymous-report",
"file": "anonymous-report.py",
"every": "day",
"reload": false
}
]
}

View file

@ -265,6 +265,17 @@ class Database:
return ""
def get_metadata(self) -> Dict[str, str]:
"""Get the metadata from the database"""
data = {"version": "1.5.4", "integration": "unknown"}
with self.__db_session() as session:
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).with_entities(Metadata.version, Metadata.integration).filter_by(id=1).first()
if metadata:
data = {"version": metadata.version, "integration": metadata.integration}
return data
def check_changes(self) -> Union[Dict[str, bool], bool, str]:
"""Check if either the config, the custom configs, plugins or instances have changed inside the database"""
with self.__db_session() as session: