-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebhook.cpp
More file actions
79 lines (66 loc) · 2.42 KB
/
Copy pathWebhook.cpp
File metadata and controls
79 lines (66 loc) · 2.42 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
#include "Includes/Webhook.h"
#include <iostream>
#include <ctime>
#include "curl/curl.h"
Webhook::Webhook(const std::string& url) : m_url(url) {}
bool Webhook::sendAlert(const std::string& username, const std::string& ip)
{
CURL* curl = curl_easy_init();
if (!curl)
return false;
std::string json =
"{"
"\"embeds\":["
"{"
"\"title\":\"SSH Unauthorized Access\","
"\"description\":\"Une tentative de connexion SSH non autorisée a été détectée.\","
"\"color\":15158332,"
"\"fields\":["
"{"
"\"name\":\"Utilisateur\","
"\"value\":\"" + username + "\","
"\"inline\":true"
"},"
"{"
"\"name\":\"Adresse IP\","
"\"value\":\"" + ip + "\","
"\"inline\":true"
"}"
"],"
"\"footer\":{"
"\"text\":\"SSH Security Monitor\""
"},"
"\"timestamp\":\"" + []() {
char buffer[64];
std::time_t now = std::time(nullptr);
std::tm* utc = std::gmtime(&now);
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", utc);
return std::string(buffer);
}() + "\""
"}"
"]"
"}";
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
CURLcode result = curl_easy_perform(curl);
long httpCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
if (result != CURLE_OK)
{
std::cerr << "CURL Error: "
<< curl_easy_strerror(result)
<< std::endl;
}
else
{
std::cout << "Discord HTTP Code: "
<< httpCode
<< std::endl;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return result == CURLE_OK && httpCode == 204;
}