public async Task <SendAsyncResult> SendAsync(PushUpdatesRequest request) { return(await Task.Run(async() => { var result = new SendAsyncResult { Status = true }; try { //prepare file name var dir = $"{AppDomain.CurrentDomain.BaseDirectory}\\Logs"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var fileName = $"dump_{DateTime.UtcNow.ToString("yyMMdd_HHmmss")}"; // serialize JSON to a string and then write string to a file File.WriteAllText(Path.Combine(dir, fileName), JsonConvert.SerializeObject(request)); // example how to canvert screenshot byte array back to image if (request.Screenshots != null && request.Screenshots.Screenshots.Any()) { foreach (var screenshot in request.Screenshots.Screenshots) { ImageConverter converter = new ImageConverter(); var img = (Image)converter.ConvertFrom(screenshot.Image); img.Save($"{AppDomain.CurrentDomain.BaseDirectory}\\Logs\\{screenshot.Id.ToString()}.jpg", ImageFormat.Jpeg); img.Dispose(); } } // example of sending request to authorized API var signinInfo = _storageService.LoadSignInInfo(); HttpClient apiClient = null; if (signinInfo != null) { apiClient = new HttpClient(new IdentityHttpHandler(storageService: _storageService, refreshToken: signinInfo.RefreshToken, accessToken: signinInfo.AccessToken)); } else { apiClient = new HttpClient(); } apiClient.BaseAddress = new Uri("https://demo.identityserver.io/api/"); var apiResult = await apiClient.GetAsync("test"); _logger.LogDebug(JsonConvert.SerializeObject(apiResult)); _logger.LogDebug($"Result content: {JArray.Parse(await apiResult.Content.ReadAsStringAsync()).ToString()}"); } catch (Exception ex) { _logger.LogError(ex.ToString()); result.Status = false; result.ErrorMessage = Resources.APIStubErrorMessage; } return result; })); }
/// <summary> /// Pushes user activity to API. /// </summary> /// <returns></returns> public async Task <PushUpdatesResult> PushUpdatesAsync() { var result = new PushUpdatesResult { Status = true, }; // gather data from services var request = new PushUpdatesRequest { MouseClicks = _mouseSnapshot.TakeSnapshot(), KeyboardClicks = _keyboardSnapshot.TakeSnapshot(), InstalledApplications = _installedApplicationsSnapshot.TakeSnapshot(), OpenedApplications = _openedApplicationsSnapshot.TakeSnapshot(), DnsCache = _dnsCacheSnapshot.TakeSnapshot(), Screenshots = _screenshotService.TakeSnapshot(), SystemPerformance = _systemPerformanceSnapshot.TakeSnapshot(), UserTimeZoneOffset = DateTimeOffset.Now.Offset }; // push request object to API var sendResult = await _trackApiWrapper.SendAsync(request); if (!sendResult.Status) { result.Status = false; result.ErrorMessage = "An error occurred while sending PushUpdatesRequest to API"; return(result); } // gather ids of handles snapshot items result.MouseIdList = request.MouseClicks.Items.Select(t => t.Id); result.KeyboardIdList = request.KeyboardClicks.Items.Select(t => t.Id); result.InstalledAppsIdList = request.InstalledApplications.Items.Select(t => t.Id); result.OpenedAppsIdList = request.OpenedApplications.Items.Select(t => t.Id); result.DnsCacheIdList = request.DnsCache.Items.Select(t => t.Id); result.ScreenshotsIdList = request.Screenshots.Screenshots.Select(t => t.Id); result.SystemPerformanceIdList = request.SystemPerformance.Items.Select(t => t.Id); // clear items which were already posted to API _mouseSnapshot.ClearSnapshot(result.MouseIdList); _keyboardSnapshot.ClearSnapshot(result.KeyboardIdList); _installedApplicationsSnapshot.ClearSnapshot(result.InstalledAppsIdList); _openedApplicationsSnapshot.ClearSnapshot(result.OpenedAppsIdList); _dnsCacheSnapshot.ClearSnapshot(result.DnsCacheIdList); _screenshotService.ClearSnapshot(result.ScreenshotsIdList); _systemPerformanceSnapshot.ClearSnapshot(result.SystemPerformanceIdList); LastSyncTime = DateTime.UtcNow; return(result); }