Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
tray_notifications.cpp
Go to the documentation of this file.
2#include "utils.h"
3#include <shellapi.h>
4#include <strsafe.h>
5#include <condition_variable>
6#include <deque>
7#include <mutex>
8#include <thread>
9
10// How long each balloon stays up
11#define PANO_BALLOON_MS 5000
12
13// Notifications waiting to be shown. A burst of detections would otherwise queue minutes of
14// balloons; beyond this many, the rest are summed into one "and N more" balloon
15#define PANO_MAX_QUEUED_BALLOONS 4
16
17struct Balloon {
18 std::string Title;
19 std::string Text;
20};
21
22static std::mutex g_Lock;
23static std::condition_variable g_Wake;
24static std::deque<Balloon> g_Queue;
25static size_t g_Dropped = 0;
26static bool g_WorkerStarted = false;
27
28// Shows one balloon and keeps it up for PANO_BALLOON_MS
29static void ShowBalloon(const Balloon& balloon)
30{
31 NOTIFYICONDATAA nid = {};
32 nid.cbSize = sizeof(NOTIFYICONDATAA);
33 nid.hWnd = GetActiveWindow(); // Or use your main window handle
34 nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO;
35 nid.uTimeout = PANO_BALLOON_MS;
36 nid.dwInfoFlags = NIIF_INFO | NIIF_USER;
37 nid.uVersion = NOTIFYICON_VERSION_4;
38 // Relative to the executable, not the current directory, which depends on how the service
39 // was started
40 std::string icon = GetExecutableDirectory() + "\\assets\\panoptes-head.ico";
41 nid.hIcon = (HICON)LoadImageA(NULL, icon.c_str(), IMAGE_ICON, 128, 128, LR_LOADFROMFILE | LR_SHARED);
42 if (nid.hIcon == NULL) {
43 return;
44 }
45
46 // StringCchCopy truncates rather than fails the balloon when a text is too long
47 StringCchCopyA(nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle), balloon.Title.c_str());
48 StringCchCopyA(nid.szTip, ARRAYSIZE(nid.szTip), "Panoptes EDR");
49 StringCchCopyA(nid.szInfo, ARRAYSIZE(nid.szInfo), balloon.Text.c_str());
50
51 Shell_NotifyIconA(NIM_ADD, &nid);
52 Sleep(PANO_BALLOON_MS);
53 Shell_NotifyIconA(NIM_DELETE, &nid);
54}
55
56// Shows queued balloons one at a time, so callers never wait for one to finish
57static void BalloonThread()
58{
59 for (;;) {
60 Balloon next;
61 {
62 std::unique_lock<std::mutex> lock(g_Lock);
63 g_Wake.wait(lock, [] { return !g_Queue.empty() || g_Dropped > 0; });
64 if (!g_Queue.empty()) {
65 next = std::move(g_Queue.front());
66 g_Queue.pop_front();
67 }
68 else {
69 next.Title = "Panoptes EDR Detection";
70 next.Text = std::to_string(g_Dropped) + " more detections. See C:\\ProgramData\\Panoptes\\Logs";
71 g_Dropped = 0;
72 }
73 }
74 ShowBalloon(next);
75 }
76}
77
78BOOL TrayNotifications::Tray::ShowTrayIconBalloon(LPCSTR pszTitle, LPCSTR pszText)
79{
80 std::lock_guard<std::mutex> guard(g_Lock);
81 if (!g_WorkerStarted) {
82 // Detached: it only ever waits or shows a balloon, and ends with the process
83 std::thread(BalloonThread).detach();
84 g_WorkerStarted = true;
85 }
86 if (g_Queue.size() >= PANO_MAX_QUEUED_BALLOONS) {
87 g_Dropped++;
88 }
89 else {
90 g_Queue.push_back({ pszTitle, pszText });
91 }
92 g_Wake.notify_one();
93 return TRUE;
94}
static BOOL ShowTrayIconBalloon(LPCSTR pszTitle, LPCSTR pszText)
Queues a tray icon balloon notification and returns at once. A background thread shows queued balloon...
std::string Title
std::string Text
#define PANO_BALLOON_MS
#define PANO_MAX_QUEUED_BALLOONS
std::string GetExecutableDirectory()
Definition utils.cpp:71