Skip to content

Commit

Permalink
Implement Serilog and LogsManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 22, 2023
1 parent 5099191 commit d1c432e
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Server/API/AgentUpdateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task<bool> CheckForDeviceBan(string deviceIp)

if (_appConfig.BannedDevices.Contains(deviceIp))
{
_dataService.WriteEvent($"Device IP ({deviceIp}) is banned. Sending uninstall command.", null);
_logger.LogInformation("Device IP ({deviceIp}) is banned. Sending uninstall command.", deviceIp);


var bannedDevices = _serviceSessionCache.GetAllDevices().Where(x => x.PublicIP == deviceIp);
Expand Down
15 changes: 10 additions & 5 deletions Server/API/RemoteControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Remotely.Server.Services.RcImplementations;
using Immense.RemoteControl.Server.Abstractions;
using Immense.RemoteControl.Shared.Helpers;
using Microsoft.Build.Framework;
using Microsoft.Extensions.Logging;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -32,6 +34,7 @@ public class RemoteControlController : ControllerBase
private readonly IHubEventHandler _hubEvents;
private readonly IDataService _dataService;
private readonly SignInManager<RemotelyUser> _signInManager;
private readonly ILogger<RemoteControlController> _logger;

public RemoteControlController(
SignInManager<RemotelyUser> signInManager,
Expand All @@ -41,7 +44,8 @@ public RemoteControlController(
IServiceHubSessionCache serviceSessionCache,
IOtpProvider otpProvider,
IHubEventHandler hubEvents,
IApplicationConfig appConfig)
IApplicationConfig appConfig,
ILogger<RemoteControlController> logger)
{
_dataService = dataService;
_serviceHub = serviceHub;
Expand All @@ -51,6 +55,7 @@ public RemoteControlController(
_otpProvider = otpProvider;
_hubEvents = hubEvents;
_signInManager = signInManager;
_logger = logger;
}

[HttpGet("{deviceID}")]
Expand All @@ -75,20 +80,20 @@ public async Task<IActionResult> Post([FromBody] RemoteControlRequest rcRequest)
if (result.Succeeded &&
_dataService.DoesUserHaveAccessToDevice(rcRequest.DeviceID, _dataService.GetUserByNameWithOrg(rcRequest.Email)))
{
_dataService.WriteEvent($"API login successful for {rcRequest.Email}.", orgId);
_logger.LogInformation("API login successful for {rcRequestEmail}.", rcRequest.Email);
return await InitiateRemoteControl(rcRequest.DeviceID, orgId);
}
else if (result.IsLockedOut)
{
_dataService.WriteEvent($"API login unsuccessful due to lockout for {rcRequest.Email}.", orgId);
_logger.LogInformation("API login successful for {rcRequestEmail}.", rcRequest.Email);
return Unauthorized("Account is locked.");
}
else if (result.RequiresTwoFactor)
{
_dataService.WriteEvent($"API login unsuccessful due to 2FA for {rcRequest.Email}.", orgId);
_logger.LogInformation("API login successful for {rcRequestEmail}.", rcRequest.Email);
return Unauthorized("Account requires two-factor authentication.");
}
_dataService.WriteEvent($"API login unsuccessful due to bad attempt for {rcRequest.Email}.", orgId);
_logger.LogInformation("API login unsuccessful due to bad attempt for {rcRequestEmail}.", rcRequest.Email);
return BadRequest();
}

Expand Down
34 changes: 24 additions & 10 deletions Server/API/ServerLogsController.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
using Microsoft.AspNetCore.Mvc;
using Remotely.Server.Auth;
using Remotely.Server.Services;
using System.Text;
using System.Text.Json;
using System;
using Microsoft.Extensions.Logging;
using Remotely.Server.Services;
using System.IO;
using System.Threading.Tasks;

namespace Remotely.Server.API
{
[Route("api/[controller]")]
[ApiController]
public class ServerLogsController : ControllerBase
{
private readonly IDataService _dataService;
private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions() { WriteIndented = true };
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
private readonly ILogsManager _logsManager;
private readonly ILogger<ServerLogsController> _logger;

public ServerLogsController(IDataService dataService)
public ServerLogsController(
ILogsManager logsManager,
ILogger<ServerLogsController> logger)
{
_dataService = dataService;
_logsManager = logsManager;
_logger = logger;
}

[ServiceFilter(typeof(ApiAuthorizationFilter))]
[HttpGet("Download")]
public ActionResult Download()
public async Task<IActionResult> Download()
{
Request.Headers.TryGetValue("OrganizationID", out var orgId);
_logger.LogInformation(
"Downloading server logs. Remote IP: {ip}",
HttpContext.Connection.RemoteIpAddress);

var zipFile = await _logsManager.ZipAllLogs();
Response.OnCompleted(() =>
{
Directory.Delete(zipFile.DirectoryName, true);
return Task.CompletedTask;
});

var logs = _dataService.GetAllEventLogs(User.Identity?.Name, orgId);
var fileBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(logs, _jsonOptions));
return File(fileBytes, "application/octet-stream", "ServerLogs.json");
return File(zipFile.OpenRead(), "application/octet-stream", zipFile.Name);
}
}
}
12 changes: 9 additions & 3 deletions Server/Areas/Identity/Pages/Account/ForgotPassword.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.WebUtilities;
using Remotely.Shared.Models;
using Remotely.Server.Services;
using Microsoft.Extensions.Logging;

namespace Remotely.Server.Areas.Identity.Pages.Account
{
Expand All @@ -21,14 +22,18 @@ public class ForgotPasswordModel : PageModel
private readonly UserManager<RemotelyUser> _userManager;
private readonly IEmailSenderEx _emailSender;
private readonly IDataService _dataService;
private readonly ILogger<ForgotPasswordModel> _logger;

public ForgotPasswordModel(UserManager<RemotelyUser> userManager,
public ForgotPasswordModel(
UserManager<RemotelyUser> userManager,
IEmailSenderEx emailSender,
IDataService dataService)
IDataService dataService,
ILogger<ForgotPasswordModel> logger)
{
_userManager = userManager;
_emailSender = emailSender;
_dataService = dataService;
_logger = logger;
}

[BindProperty]
Expand Down Expand Up @@ -62,7 +67,8 @@ public async Task<IActionResult> OnPostAsync()
values: new { area = "Identity", code },
protocol: Request.Scheme);

_dataService.WriteEvent($"Sending password reset for user {user.UserName}. Reset URL: {callbackUrl}", user.OrganizationID);
_logger.LogInformation(
"Sending password reset for user {username}. Reset URL: {callbackUrl}", user.UserName, callbackUrl);

var emailResult = await _emailSender.SendEmailAsync(
Input.Email,
Expand Down
9 changes: 6 additions & 3 deletions Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AgentHub : Hub
private readonly ICircuitManager _circuitManager;
private readonly IDataService _dataService;
private readonly IExpiringTokenService _expiringTokenService;
private readonly ILogger<AgentHub> _logger;
private readonly IServiceHubSessionCache _serviceSessionCache;
private readonly IHubContext<ViewerHub> _viewerHubContext;

Expand All @@ -31,14 +32,16 @@ public AgentHub(IDataService dataService,
IServiceHubSessionCache serviceSessionCache,
IHubContext<ViewerHub> viewerHubContext,
ICircuitManager circuitManager,
IExpiringTokenService expiringTokenService)
IExpiringTokenService expiringTokenService,
ILogger<AgentHub> logger)
{
_dataService = dataService;
_serviceSessionCache = serviceSessionCache;
_viewerHubContext = viewerHubContext;
_appConfig = appConfig;
_circuitManager = circuitManager;
_expiringTokenService = expiringTokenService;
_logger = logger;
}

// TODO: Replace with new invoke capability in .NET 7 in ScriptingController.
Expand Down Expand Up @@ -133,7 +136,7 @@ public Task<bool> DeviceCameOnline(Device device)
}
catch (Exception ex)
{
_dataService.WriteEvent(ex, device?.OrganizationID);
_logger.LogError(ex, "Error while setting device to online status.");
}

Context.Abort();
Expand Down Expand Up @@ -286,7 +289,7 @@ private bool CheckForDeviceBan(params string[] deviceIdNameOrIPs)
if (_appConfig.BannedDevices.Any(x => !string.IsNullOrWhiteSpace(x) &&
x.Equals(device, StringComparison.OrdinalIgnoreCase)))
{
_dataService.WriteEvent($"Device ID/name/IP ({device}) is banned. Sending uninstall command.", null);
_logger.LogWarning("Device ID/name/IP ({device}) is banned. Sending uninstall command.", device);

_ = Clients.Caller.SendAsync("UninstallAgent");
return true;
Expand Down
17 changes: 9 additions & 8 deletions Server/Hubs/CircuitConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ public async Task<Result<RemoteControlSessionEx>> RemoteControl(string deviceId,
if (!_dataService.DoesUserHaveAccessToDevice(deviceId, User))
{
var device = _dataService.GetDevice(targetDevice.ID);
_dataService.WriteEvent($"Remote control attempted by unauthorized user. Device ID: {deviceId}. User Name: {User.UserName}.", EventType.Warning, device?.OrganizationID);
_logger.LogWarning(
"Remote control attempted by unauthorized user. Device ID: {deviceId}. User Name: {userName}.",
deviceId,
User.UserName);
return Result.Fail<RemoteControlSessionEx>("Unauthorized.");

}
Expand Down Expand Up @@ -414,13 +417,11 @@ public Task UpdateTags(string deviceID, string tags)

public Task UploadFiles(List<string> fileIDs, string transferID, string[] deviceIDs)
{
_dataService.WriteEvent(new EventLog()
{
EventType = EventType.Info,
Message = $"File transfer started by {User.UserName}. File transfer IDs: {string.Join(", ", fileIDs)}.",
TimeStamp = Time.Now,
OrganizationID = User.OrganizationID
});
_logger.LogInformation(
"File transfer started by {userName}. File transfer IDs: {fileIds}.",
User.UserName,
string.Join(", ", fileIDs));

deviceIDs = _dataService.FilterDeviceIDsByUserPermission(deviceIDs, User);
var connections = GetActiveConnectionsForUserOrg(deviceIDs);
foreach (var connection in connections)
Expand Down
14 changes: 8 additions & 6 deletions Server/Pages/ServerLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@inject IDataService DataService
@inject IToastService ToastService
@inject IJsInterop JsInterop
@inject ILogsManager LogsManager

<h3 class="mb-3">Server Logs</h3>

Expand Down Expand Up @@ -109,11 +110,12 @@ else
{
get
{
return DataService.GetEventLogs(User.UserName,
_fromDate,
_toDate,
_eventType,
_messageFilter);
return Enumerable.Empty<EventLog>();
//return DataService.GetEventLogs(User.UserName,
// _fromDate,
// _toDate,
// _eventType,
// _messageFilter);
}
}

Expand All @@ -122,7 +124,7 @@ else
var result = await JsInterop.Confirm("Are you sure you want to delete all logs?");
if (result)
{
await DataService.ClearLogs(User.UserName);
await LogsManager.DeleteLogs();
ToastService.ShowToast("Logs deleted.");
}
}
Expand Down
4 changes: 2 additions & 2 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
services.AddScoped<IScriptScheduleDispatcher, ScriptScheduleDispatcher>();
services.AddSingleton<IOtpProvider, OtpProvider>();
services.AddSingleton<IEmbeddedServerDataSearcher, EmbeddedServerDataSearcher>();
services.AddSingleton<ILogsManager>(LogsManager.Default);

services.AddRemoteControlServer(config =>
{
Expand Down Expand Up @@ -329,8 +330,7 @@ void ConfigureSerilog(WebApplicationBuilder webAppBuilder)
dataRetentionDays = retentionSetting;
}

var logPath = Directory.Exists("/remotely-data") ? "/remotely-data/logs" : "logs";
Directory.CreateDirectory(logPath);
var logPath = LogsManager.Default.GetLogsDirectory();

void ApplySharedLoggerConfig(LoggerConfiguration loggerConfiguration)
{
Expand Down
83 changes: 0 additions & 83 deletions Server/Services/DbLogger.cs

This file was deleted.

Loading

0 comments on commit d1c432e

Please sign in to comment.