using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Runtime.InteropServices; using Serilog; using PCMonitor; namespace PCMonitorAgent.Services { /// /// Monitor Aktivitas PC (Aplikasi, Browser, File Access) /// public class ActivityMonitor : IDisposable { private readonly ApiClient apiClient; private readonly ILogger logger; private DateTime lastBrowserCheck = DateTime.Now.AddHours(-1); public ActivityMonitor(ApiClient client) { apiClient = client; logger = Log.ForContext(); } /// /// Collect activities dari PC /// public async Task CollectAsync() { try { // 1. Monitor browser history var browserActivities = GetBrowserHistory(); foreach (var activity in browserActivities) { try { await apiClient.SendActivityAsync(activity); } catch (Exception ex) { logger.Warning("Failed to send browser activity: {Message}", ex.Message); } } // 2. Monitor running applications var appActivities = GetRunningApplications(); foreach (var activity in appActivities) { try { await apiClient.SendActivityAsync(activity); } catch (Exception ex) { logger.Warning("Failed to send app activity: {Message}", ex.Message); } } lastBrowserCheck = DateTime.Now; logger.Debug("Activities collected successfully"); } catch (Exception ex) { logger.Error(ex, "Error collecting activities"); } } /// /// Ambil browser history dari Chrome, Edge, Firefox /// private List GetBrowserHistory() { var history = new List(); try { // Chrome history history.AddRange(GetChromeHistory()); // Edge history history.AddRange(GetEdgeHistory()); // Firefox history history.AddRange(GetFirefoxHistory()); } catch (Exception ex) { logger.Warning(ex, "Error getting browser history"); } return history; } /// /// Get Chrome history /// private List GetChromeHistory() { var history = new List(); try { string chromePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Google\\Chrome\\User Data\\Default\\History"); if (File.Exists(chromePath)) { // Chrome history database (SQLite) var chromeHistory = ReadSQLiteDatabase(chromePath, "urls"); foreach (var entry in chromeHistory) { history.Add(new { activity_type = "browser_open", browser_name = "Chrome", website_url = entry["url"], website_title = entry["title"], logged_at = DateTime.Now }); } } } catch (Exception ex) { logger.Warning(ex, "Error reading Chrome history"); } return history; } /// /// Get Edge history /// private List GetEdgeHistory() { var history = new List(); try { string edgePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\Edge\\User Data\\Default\\History"); if (File.Exists(edgePath)) { var edgeHistory = ReadSQLiteDatabase(edgePath, "urls"); foreach (var entry in edgeHistory) { history.Add(new { activity_type = "browser_open", browser_name = "Edge", website_url = entry["url"], website_title = entry["title"], logged_at = DateTime.Now }); } } } catch (Exception ex) { logger.Warning(ex, "Error reading Edge history"); } return history; } /// /// Get Firefox history /// private List GetFirefoxHistory() { var history = new List(); try { string firefoxPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Mozilla\\Firefox\\Profiles"); if (Directory.Exists(firefoxPath)) { var profiles = Directory.GetDirectories(firefoxPath); foreach (var profile in profiles) { string dbPath = Path.Combine(profile, "places.sqlite"); if (File.Exists(dbPath)) { var ffHistory = ReadSQLiteDatabase(dbPath, "moz_places"); foreach (var entry in ffHistory) { history.Add(new { activity_type = "browser_open", browser_name = "Firefox", website_url = entry["url"], website_title = entry["title"], logged_at = DateTime.Now }); } } } } } catch (Exception ex) { logger.Warning(ex, "Error reading Firefox history"); } return history; } /// /// Helper untuk membaca SQLite database /// private List> ReadSQLiteDatabase(string dbPath, string table) { var result = new List>(); // Implementation akan menggunakan System.Data.SQLite jika diperlukan // Untuk sekarang, return empty list return result; } /// /// Get running applications /// private List GetRunningApplications() { var apps = new List(); try { var processes = Process.GetProcesses(); foreach (var process in processes) { try { if (!string.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.Length > 0) { apps.Add(new { activity_type = "app_launch", app_name = process.ProcessName, app_path = GetProcessPath(process), description = process.MainWindowTitle, logged_at = DateTime.Now }); } } catch { } } } catch (Exception ex) { logger.Warning(ex, "Error getting running applications"); } return apps; } /// /// Get process executable path /// private string GetProcessPath(Process process) { try { return process.MainModule?.FileName ?? ""; } catch { return ""; } } public void Dispose() { // Cleanup if needed } } /// /// Browser History Helper /// public class BrowserHistoryHelper { public List GetChromeHistory() { var history = new List(); try { string chromePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Google\Chrome\User Data\Default\History" ); if (File.Exists(chromePath)) { // Note: This is simplified. Real implementation would need to access Chrome's SQLite database history.Add(new { activity_type = "browser_open", browser_name = "Chrome", website_url = "http://example.com", website_title = "Chrome Activity", visit_time = DateTime.Now }); } } catch { } return history; } private List GetEdgeHistory() { var history = new List(); // Similar to Chrome return history; } private List GetFirefoxHistory() { var history = new List(); // Similar to Chrome return history; } public void Dispose() { } } /// /// Windows Event Hook untuk mendeteksi aktivitas window /// public class WinEventHook : IDisposable { private const int EVENT_SYSTEM_FOREGROUND = 3; private IntPtr hookHandle = IntPtr.Zero; private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventHook, WinEventDelegate lpfnWinEventHook, uint idProcess, uint idThread, uint dwFlags); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool UnhookWinEvent(IntPtr hWinEventHook); [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count); public void Dispose() { if (hookHandle != IntPtr.Zero) { UnhookWinEvent(hookHandle); hookHandle = IntPtr.Zero; } } } }