updating log entry server scripts

This commit is contained in:
jakani24
2024-03-09 14:29:45 +01:00
parent 732dbb8432
commit e22fffb15f
10 changed files with 125 additions and 30 deletions

View File

@@ -6,14 +6,14 @@
"AbsoluteMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\log.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|solutionrelative:log.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|solutionrelative:connect.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|solutionrelative:connect.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|solutionrelative:connect.h||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\virus_ctrl.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{56E65283-AAC9-43F6-9613-72BE8D648AC4}|client_backend.vcxproj|solutionrelative:virus_ctrl.cpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
@@ -74,13 +74,13 @@
"Children": [
{
"$type": "Document",
"DocumentIndex": 1,
"DocumentIndex": 2,
"Title": "connect.h",
"DocumentMoniker": "C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.h",
"RelativeDocumentMoniker": "connect.h",
"ToolTip": "C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.h",
"RelativeToolTip": "connect.h",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAoAAAABAAAA",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAsAAABPAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-03-09T12:32:01.859Z",
"EditorCaption": ""
@@ -105,7 +105,7 @@
"RelativeDocumentMoniker": "log.h",
"ToolTip": "C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\log.h",
"RelativeToolTip": "log.h",
"ViewState": "AQIAAFEAAAAAAAAAAAAAAHEAAABCAAAA",
"ViewState": "AQIAAGYAAAAAAAAAAAAYwJMAAAAkAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-02-25T09:02:55.874Z",
"EditorCaption": ""
@@ -120,8 +120,7 @@
"RelativeToolTip": "log.cpp",
"ViewState": "AQIAAAAAAAAAAAAAAAAAABkAAAAeAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-02-25T09:08:59.07Z",
"EditorCaption": ""
"WhenOpened": "2024-02-25T09:08:59.07Z"
},
{
"$type": "Document",
@@ -157,8 +156,7 @@
"RelativeToolTip": "thread_ctrl.cpp",
"ViewState": "AQIAAAAAAAAAAAAAAAAAADoAAAAqAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-01-28T20:40:06.264Z",
"EditorCaption": ""
"WhenOpened": "2024-01-28T20:40:06.264Z"
},
{
"$type": "Document",
@@ -170,8 +168,7 @@
"RelativeToolTip": "client_backend.cpp",
"ViewState": "AQIAAAAAAAAAAAAAAAAAACwAAAAiAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-01-28T20:40:06.279Z",
"EditorCaption": ""
"WhenOpened": "2024-01-28T20:40:06.279Z"
},
{
"$type": "Document",
@@ -183,8 +180,7 @@
"RelativeToolTip": "scan.cpp",
"ViewState": "AQIAABgAAAAAAAAAAAAAAA4AAAAVAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-01-28T20:40:06.248Z",
"EditorCaption": ""
"WhenOpened": "2024-01-28T20:40:06.248Z"
},
{
"$type": "Document",
@@ -196,8 +192,7 @@
"RelativeToolTip": "update.cpp",
"ViewState": "AQIAABMAAAAAAAAAAAAkwEQAAABzAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-01-28T20:40:06.248Z",
"EditorCaption": ""
"WhenOpened": "2024-01-28T20:40:06.248Z"
},
{
"$type": "Document",
@@ -237,13 +232,13 @@
},
{
"$type": "Document",
"DocumentIndex": 2,
"DocumentIndex": 1,
"Title": "connect.cpp",
"DocumentMoniker": "C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.cpp",
"RelativeDocumentMoniker": "connect.cpp",
"ToolTip": "C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\connect.cpp",
"RelativeToolTip": "connect.cpp",
"ViewState": "AQIAAG8AAAAAAAAAAAAAAIMAAAABAAAA",
"ViewState": "AQIAAJUAAAAAAAAAAAAQwLsAAAAaAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-01-28T20:40:06.315Z",
"EditorCaption": ""
@@ -258,8 +253,7 @@
"RelativeToolTip": "virus_ctrl.cpp",
"ViewState": "AQIAADYAAAAAAAAAAAAAAGEAAAAvAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-02-18T12:49:41.861Z",
"EditorCaption": ""
"WhenOpened": "2024-02-18T12:49:41.861Z"
}
]
}

View File

@@ -164,4 +164,35 @@ char* url_encode(const char* input) {
return encoded;
}
int upload_to_srv(const char* url, const char* filepath,bool ignore_insecure) {
//upload a file to the server
CURL* curl;
CURLcode res;
struct curl_httppost* formpost = NULL;
struct curl_httppost* lastptr = NULL;
struct curl_slist* headerlist = NULL;
static const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, filepath, CURLFORM_END);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
if(ignore_insecure==true)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
if (res == CURLE_OK) {
return 0;
}
else {
return 1;
}
}
}
#endif

View File

@@ -8,4 +8,5 @@
int connect_to_srv(const char* url, char* out, int max_len, bool ignore_insecure);
int download_file_from_srv(const char* url, const char* outputFileName,bool ignore_insecure);
int fast_send(const char* url, bool ignore_insecure);
char* url_encode(const char* input);
char* url_encode(const char* input);
int upload_to_srv(const char* url, const char* filepath, bool ignore_insecure);

View File

@@ -9,6 +9,8 @@
#include "well_known.h"
#include "settings.h"
#include "security.h"
int srv_timeout = 0;
int timeout_reset = 0;
enum class LOGLEVEL {
INFO,
WARN,
@@ -26,6 +28,7 @@ std::string get_loglevel(LOGLEVEL level);
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... args) {
timeout_reset++;
std::string prefix = get_loglevel(level);
std::time_t now = std::time(nullptr);
std::tm tm;
@@ -92,8 +95,8 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
fprintf_s(fp, "%s", logString.c_str());
fclose(fp);
}
//write to server log file
if (fopen_s(&fp, SRV_LOGFILE, "a") == 0) {
//write to server log file only if we werent able to send the logs directly. this file will store them until we can upload them
if (fopen_s(&fp, SRV_LOGFILE, "a") == 0 && srv_timout>=5) { //if server already did not respon over 5 times, we add it to the log file
fprintf_s(fp, "%s\n", to_srv_string.c_str());
fclose(fp);
}
@@ -102,8 +105,9 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
//build up the log string: loglevel&logtext&machineid&date
//to_srv_string=includes the log message
//we now need to build up the request string and append the machineid
if (level!=LOGLEVEL::INFO_NOSEND && level!=LOGLEVEL::WARN_NOSEND && level!=LOGLEVEL::ERR_NOSEND && level!=LOGLEVEL::PANIC_NOSEND) {
if (level!=LOGLEVEL::INFO_NOSEND && level!=LOGLEVEL::WARN_NOSEND && level!=LOGLEVEL::ERR_NOSEND && level!=LOGLEVEL::PANIC_NOSEND && srv_timeout<5) {
char* url = new char[3000];
int res = 0;
if (get_setting("server:server_url", url) == 0 or strcmp(url, "nan") == 0) {
strcat_s(url, 3000, "/api/php/log/add_entry.php?logtext=");//need to add machine_id and apikey
strcat_s(url, 3000, url_encode(to_srv_string.c_str()));
@@ -111,7 +115,11 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
strcat_s(url, 3000, get_machineid(SECRETS));
strcat_s(url, 3000, "&apikey=");
strcat_s(url, 3000, get_apikey(SECRETS));
fast_send(url, get_setting("communication:unsafe_tls");
res=fast_send(url, get_setting("communication:unsafe_tls");
if (res != 0) {
//we know that the server might be down, so we will increment the timeout counter
srv_timeout++;
}
//we might not want to log an error occuring here because it will create a loop
delete[] url;
}
@@ -120,6 +128,29 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
return;
}
}//else we do not send the log to the server
if(timeout_reset>100){ //after 100 log entrys, we cna try again to reach out ot the server.
timeout_reset=0;
srv_timeout=0;
//try to upload the server_log file, where we stored the logs which we could not upload.
char* url = new char[3000];
int res = 0;
if (get_setting("server:server_url", url) == 0 or strcmp(url, "nan") == 0) {
strcat_s(url, 3000, "/api/php/log/add_log.php?machine_id=");
strcat_s(url, 3000, get_machineid(SECRETS));
strcat_s(url, 3000, "&apikey=");
strcat_s(url, 3000, get_apikey(SECRETS));
res=upload_to_srv(SRV_LOGFILE, url, get_setting("communication:unsafe_tls"));
if (res != 0) {
//we know that the server might be down, so we will increment the timeout counter
srv_timeout++;
}
else {
//remove the logfile
remove(SRV_LOGFILE);
}
}
delete [] url;
}
}

View File

@@ -16,7 +16,7 @@ else{
$message=$log[2];
$loglevel=$log[1];
$machine_id=htmlspecialchars($_GET["machine_id"]);
$time=log[0];
$time=$log[0];
//include db pw
include "../../../config.php";
@@ -26,7 +26,7 @@ else{
}
$sql = "INSERT INTO log (loglevel,machine_id,logtext,time) VALUES (?,?,?,?);";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $loglevel,$machine_id,$logtext,$time);
$stmt->bind_param("ssss", $loglevel,$machine_id,$message,$time);
// Execute the statement
if(!$stmt->execute())
echo("wrt_err");

View File

@@ -0,0 +1,38 @@
<?php
include "../accessctrl/check_apikey.php";
if(check_apikey()!==true){
die("no_auth");
}
// Check if a file was uploaded
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["file"])) {
//echo($_FILES["file"]["tmp_name"]);
//include db pw
include "../../../config.php";
$conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE);
if ($conn->connect_error) {
die("conn_err");
}
$fp=fopen($_FILES["file"]["tmp_name"],"r");
while(!feof($fp)){
$log_entry=fgets($fp);
$log_entry=str_replace("\n","",$log_entry);
$log=explode(";",$log_entry);
$message=$log[2];
$loglevel=$log[1];
$machine_id=htmlspecialchars($_GET["machine_id"]);
$time=$log[0];
$sql = "INSERT INTO log (loglevel,machine_id,logtext,time) VALUES (?,?,?,?);";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $loglevel,$machine_id,$message,$time);
// Execute the statement
if(!$stmt->execute())
echo("wrt_err");
else
echo("wrt_ok");
$stmt->close();
}
$conn->close();
}
?>

View File

@@ -29,7 +29,7 @@ if($perms[2]!=="1"){
</head>
<body>
<div class="container mt-5">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">

View File

@@ -22,7 +22,7 @@ $email = $_SESSION["email"];
</head>
<body>
<div class="container mt-5">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">