-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (71 loc) · 2.26 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (71 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <filesystem>
#include <thread>
#include <chrono>
#include <mutex>
#include "Includes/Config.h"
#include "Includes/Monitor.h"
#include "Includes/Webhook.h"
#include "Includes/Whitelist.h"
namespace fs = std::filesystem;
int main()
{
const std::string configPath = "config.json";
Config cfg = loadConfig(configPath);
whitelist whitelist(cfg.whitelistIps);
Webhook webhook(cfg.webhookUrl);
std::mutex configMutex;
auto lastWrite = fs::last_write_time(configPath);
std::thread watcher([&]()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
try
{
auto current = fs::last_write_time(configPath);
if (current != lastWrite)
{
lastWrite = current;
std::lock_guard<std::mutex> lock(configMutex);
std::cout << "[CONFIG] Changement détecté, rechargement..." << std::endl;
cfg = loadConfig(configPath);
whitelist = ::whitelist(cfg.whitelistIps);
webhook = Webhook(cfg.webhookUrl);
std::cout << "[CONFIG] Rechargement terminé." << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << "[CONFIG] Erreur : " << e.what() << std::endl;
}
}
});
watcher.detach();
Monitor monitor;
monitor.Start(
[&](const std::string& user,
const std::string& ip)
{
std::lock_guard<std::mutex> lock(configMutex);
std::cout
<< "[SSH] "
<< user
<< " -> "
<< ip
<< std::endl;
if (!whitelist.isAllowed(ip))
{
std::cout << "[ALERT] IP non whitelistée, envoi du webhook..." << std::endl;
if (!webhook.sendAlert(user, ip))
{
std::cerr << "[ERROR] Impossible d'envoyer le webhook." << std::endl;
}
}
else
{
std::cout << "[INFO] IP whitelistée." << std::endl;
}
});
return 0;
}