front and backend can now communicate

This commit is contained in:
jakani24
2024-03-17 20:15:58 +01:00
parent 7ea487d0e1
commit 9daeaff131
33 changed files with 1301 additions and 148 deletions

View File

@@ -7,24 +7,151 @@
#include <windows.h>
#include <locale>
#include <codecvt>
#include <fstream>
#include "../client_backend/well_known.h"
// Function to simulate file scanning
void scan_file(nanogui::Screen* screen, const std::string& filePath) {
// Simulate file scanning by waiting for 10 seconds
std::this_thread::sleep_for(std::chrono::seconds(10));
void scan_file(nanogui::Screen*screen ,nanogui::Widget* contentWidget, const std::string& filePath) {
// Remove the answer file
std::remove(ANSWER_COM_PATH);
// Display the scanned file path in the window
screen->add<nanogui::Label>("Scanned file: " + filePath);
// Refresh layout to ensure the new label is visible
nanogui::Label* lineLabel1 = new nanogui::Label(contentWidget, "Scanning file: " + filePath + "\n");
screen->performLayout();
bool answered = false;
// Write command into com file
std::ofstream outputFile(MAIN_COM_PATH);
if (outputFile.is_open()) {
outputFile << "scanfile \"" << filePath << "\"";
outputFile.close();
}
else {
nanogui::Label* lineLabel2 = new nanogui::Label(contentWidget, "Error: Unable to talk to daemon!\n");
screen->performLayout();
return;
}
while (!answered) {
// Wait for answer in file
std::ifstream inputFile(ANSWER_COM_PATH);
// The structure of the answer file is as follows:
// found/not_found
// filepath
// hash
// action_taken/no_action_taken
if (inputFile.is_open()) {
std::string status, scannedFilePath, hash, action;
if (inputFile >> status) {
inputFile.ignore(1); // Ignore space
inputFile.ignore(1); // Ignore starting double quote
if (status == "found" || status == "not_found") {
std::getline(inputFile, scannedFilePath, '\"'); // Read until closing double quote
inputFile.ignore(1); // Ignore space between filepath and hash
inputFile.ignore(1); // Ignore starting double quote
std::getline(inputFile, hash, ' '); // Read until space
std::getline(inputFile, action); // Read until end of line
answered = true;
if (status == "found") {
nanogui::Label* lineLabel3 = new nanogui::Label(contentWidget, "Virus found in file: " + scannedFilePath + "\n");
nanogui::Label* lineLabel4 = new nanogui::Label(contentWidget, "File: " + scannedFilePath + " is infected\n");
nanogui::Label* lineLabel5 = new nanogui::Label(contentWidget, "Hash: " + hash + "\n");
nanogui::Label* lineLabel6 = new nanogui::Label(contentWidget, "Action taken: " + action + "\n");
}
else {
nanogui::Label* lineLabel7 = new nanogui::Label(contentWidget, "No virus found in file: " + scannedFilePath + "\n");
nanogui::Label* lineLabel8 = new nanogui::Label(contentWidget, "File: " + scannedFilePath + " is not infected\n");
}
nanogui::Label* lineLabel9 = new nanogui::Label(contentWidget, "------------------------------------------");
screen->performLayout();
}
}
else {
answered = true;
nanogui::Label* lineLabel10 = new nanogui::Label(contentWidget, "Error: Unable to talk to daemon!\n");
nanogui::Label* lineLabel11 = new nanogui::Label(contentWidget, "------------------------------------------");
screen->performLayout();
}
inputFile.close();
std::remove(ANSWER_COM_PATH);
}
// Wait for 1 second before checking again
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Remove the answer file
std::remove(ANSWER_COM_PATH);
}
// Function to simulate folder scanning
void scan_folder(nanogui::Screen* screen, const std::string& folderPath) {
// Simulate folder scanning by waiting for 10 seconds
std::this_thread::sleep_for(std::chrono::seconds(10));
void scan_folder(nanogui::Screen*screen,nanogui::Widget* contentWidget, const std::string& filePath) {
// Remove the answer file
std::remove(ANSWER_COM_PATH);
// Display the scanned folder path in the window
screen->add<nanogui::Label>("Scanned folder: " + folderPath);
// Refresh layout to ensure the new label is visible
nanogui::Label* lineLabel1 = new nanogui::Label(contentWidget, "Scanning folder: " + filePath + "\n");
screen->performLayout();
bool answered = false;
// Write command into com file
std::ofstream outputFile(MAIN_COM_PATH);
if (outputFile.is_open()) {
outputFile << "scanfolder \"" << filePath << "\"";
outputFile.close();
}
else {
nanogui::Label* lineLabel2 = new nanogui::Label(contentWidget, "Error: Unable to talk to daemon!\n");
screen->performLayout();
return;
}
while (!answered) {
// Wait for answer in file
std::ifstream inputFile(ANSWER_COM_PATH);
// The structure of the answer file is as follows:
// found/not_found
// filepath
// hash
// action_taken/no_action_taken
if (inputFile.is_open()) {
std::string status, scannedFilePath, hash, action;
while (!inputFile.eof()) {
if (inputFile >> status) {
inputFile.ignore(1); // Ignore space
inputFile.ignore(1); // Ignore starting double quote
if (status == "found" || status == "not_found") {
std::getline(inputFile, scannedFilePath, '\"'); // Read until closing double quote
inputFile.ignore(1); // Ignore space between filepath and hash
inputFile.ignore(1); // Ignore starting double quote
std::getline(inputFile, hash, ' '); // Read until space
std::getline(inputFile, action); // Read until end of line
//answered = true;
if (status == "found") {
nanogui::Label* lineLabel3 = new nanogui::Label(contentWidget, "Virus found in file: " + scannedFilePath + "\n");
nanogui::Label* lineLabel4 = new nanogui::Label(contentWidget, "File: " + scannedFilePath + " is infected\n");
nanogui::Label* lineLabel5 = new nanogui::Label(contentWidget, "Hash: " + hash + "\n");
nanogui::Label* lineLabel6 = new nanogui::Label(contentWidget, "Action taken: " + action + "\n");
}
else {
nanogui::Label* lineLabel7 = new nanogui::Label(contentWidget, "No virus found in file: " + scannedFilePath + "\n");
nanogui::Label* lineLabel8 = new nanogui::Label(contentWidget, "File: " + scannedFilePath + " is not infected\n");
}
nanogui::Label* lineLabel9 = new nanogui::Label(contentWidget, "------------------------------------------");
screen->performLayout();
}
if (status == "end")
answered = true;
}
else {
//answered = true;
nanogui::Label* lineLabel10 = new nanogui::Label(contentWidget, "Error: Unable to talk to daemon!\n");
nanogui::Label* lineLabel11 = new nanogui::Label(contentWidget, "------------------------------------------");
screen->performLayout();
}
}
inputFile.close();
}
// Wait for 1 second before checking again
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Remove the answer file
std::remove(ANSWER_COM_PATH);
}
std::string getFolderPath() {
std::string selectedFolderPath;
@@ -67,8 +194,7 @@ int main() {
nanogui::init();
// Create a NanoGUI screen
nanogui::Screen screen(nanogui::Vector2i(400, 300), "Antivirus");
nanogui::Screen screen(nanogui::Vector2i(800, 600), "Cyberhex endpoint protection");
// Create a layout for the buttons
nanogui::Widget* buttonWidget = new nanogui::Widget(&screen);
nanogui::BoxLayout* buttonLayout = new nanogui::BoxLayout(nanogui::Orientation::Vertical,
@@ -76,6 +202,16 @@ int main() {
5, 5);
buttonWidget->setLayout(buttonLayout);
// Create a widget inside the scroll panel to hold the content
nanogui::VScrollPanel* scrollPanel = new nanogui::VScrollPanel(&screen);
scrollPanel->setLayout(new nanogui::GroupLayout(10));
nanogui::Widget* contentWidget = new nanogui::Widget(scrollPanel);
contentWidget->setLayout(new nanogui::GroupLayout(10));
scrollPanel->setFixedSize(nanogui::Vector2i(750, 450));
// Create a button for scanning a file
nanogui::Button* scanFileButton = new nanogui::Button(buttonWidget, "Scan File");
scanFileButton->setFixedWidth(150);
@@ -87,7 +223,7 @@ int main() {
);
if (!selectedFile.empty()) {
// Call scan_file function in a separate thread
std::thread(scan_file, &screen, selectedFile).detach();
std::thread(scan_file,&screen, contentWidget, selectedFile).detach();
}
});
@@ -99,16 +235,32 @@ int main() {
std::string selectedFolder = getFolderPath();
if (!selectedFolder.empty()) {
// Call scan_folder function in a separate thread
std::thread(scan_folder, &screen, selectedFolder).detach();
std::thread(scan_folder, &screen,contentWidget, selectedFolder).detach();
}
});
// Set the layout for the main screen
nanogui::BoxLayout* layout = new nanogui::BoxLayout(nanogui::Orientation::Horizontal,
nanogui::Alignment::Middle,
nanogui::BoxLayout* layout = new nanogui::BoxLayout(nanogui::Orientation::Vertical,
nanogui::Alignment::Minimum,
10, 10);
// Add 100 lines to the content widget
//for (int i = 0; i < 100; ++i) {
// nanogui::Label* lineLabel = new nanogui::Label(contentWidget, "Line " + std::to_string(i));
// lineLabel->setFixedWidth(200);
//}
// Scroll panel will automatically take the size of its content
//scrollPanel->setFixedHeight(300);
screen.setVisible(true);
screen.setLayout(layout);
// Draw NanoGUI
screen.performLayout();
screen.setVisible(true);
@@ -120,4 +272,4 @@ int main() {
nanogui::shutdown();
return 0;
}
}