Skip to content

Commit

Permalink
Move appsettings to DB.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Feb 21, 2024
1 parent 2af48bf commit 8afdd97
Show file tree
Hide file tree
Showing 48 changed files with 507 additions and 725 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>x64</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<PublishDir>..\Server\wwwroot\Content\Win-x64\</PublishDir>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>x86</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<PublishDir>..\Server\wwwroot\Content\Win-x86\</PublishDir>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<PublishDir>..\Agent\bin\publish\win-x64\Desktop</PublishDir>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>x64</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<PublishDir>..\Agent\bin\publish\win-x64\Desktop</PublishDir>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>x86</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<PublishDir>..\Agent\bin\publish\win-x86\Desktop</PublishDir>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
Expand Down
9 changes: 5 additions & 4 deletions Server/API/AgentUpdateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ public class AgentUpdateController : ControllerBase
{
private readonly IHubContext<AgentHub, IAgentHubClient> _agentHubContext;
private readonly ILogger<AgentUpdateController> _logger;
private readonly IApplicationConfig _appConfig;
private readonly IDataService _dataService;
private readonly IWebHostEnvironment _hostEnv;
private readonly IAgentHubSessionCache _serviceSessionCache;

public AgentUpdateController(IWebHostEnvironment hostingEnv,
IApplicationConfig appConfig,
IDataService dataService,
IAgentHubSessionCache serviceSessionCache,
IHubContext<AgentHub, IAgentHubClient> agentHubContext,
ILogger<AgentUpdateController> logger)
{
_hostEnv = hostingEnv;
_appConfig = appConfig;
_dataService = dataService;
_serviceSessionCache = serviceSessionCache;
_agentHubContext = agentHubContext;
_logger = logger;
Expand Down Expand Up @@ -105,7 +105,8 @@ private async Task<bool> CheckForDeviceBan(string deviceIp)
return false;
}

if (_appConfig.BannedDevices.Contains(deviceIp))
var settings = await _dataService.GetSettings();
if (settings.BannedDevices.Contains(deviceIp))
{
_logger.LogInformation("Device IP ({deviceIp}) is banned. Sending uninstall command.", deviceIp);

Expand Down
26 changes: 15 additions & 11 deletions Server/API/ClientDownloadsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ namespace Remotely.Server.API;
[ApiController]
public class ClientDownloadsController : ControllerBase
{
private readonly IApplicationConfig _appConfig;
private readonly IDataService _dataService;
private readonly IEmbeddedServerDataSearcher _embeddedDataSearcher;
private readonly SemaphoreSlim _fileLock = new(1, 1);
private readonly IWebHostEnvironment _hostEnv;
private readonly ILogger<ClientDownloadsController> _logger;
public ClientDownloadsController(
IWebHostEnvironment hostEnv,
IEmbeddedServerDataSearcher embeddedDataSearcher,
IApplicationConfig appConfig,
IDataService dataService,
ILogger<ClientDownloadsController> logger)
{
_hostEnv = hostEnv;
_embeddedDataSearcher = embeddedDataSearcher;
_appConfig = appConfig;
_dataService = dataService;
_logger = logger;
}

Expand Down Expand Up @@ -133,7 +133,8 @@ private async Task<IActionResult> GetBashInstaller(string fileName, string organ
var hostIndex = fileContents.IndexOf("HostName=");
var orgIndex = fileContents.IndexOf("Organization=");

var effectiveScheme = _appConfig.ForceClientHttps ? "https" : Request.Scheme;
var settings = await _dataService.GetSettings();
var effectiveScheme = settings.ForceClientHttps ? "https" : Request.Scheme;

fileContents[hostIndex] = $"HostName=\"{effectiveScheme}://{Request.Host}\"";
fileContents[orgIndex] = $"Organization=\"{organizationId}\"";
Expand All @@ -143,9 +144,10 @@ private async Task<IActionResult> GetBashInstaller(string fileName, string organ

private async Task<IActionResult> GetDesktopFile(string filePath, string? organizationId = null)
{
LogRequest(nameof(GetDesktopFile));
var settings = await _dataService.GetSettings();
await LogRequest(nameof(GetDesktopFile));

var effectiveScheme = _appConfig.ForceClientHttps ? "https" : Request.Scheme;
var effectiveScheme = settings.ForceClientHttps ? "https" : Request.Scheme;
var serverUrl = $"{effectiveScheme}://{Request.Host}";
var embeddedData = new EmbeddedServerData(new Uri(serverUrl), organizationId);
var result = await _embeddedDataSearcher.GetRewrittenStream(filePath, embeddedData);
Expand All @@ -160,7 +162,8 @@ private async Task<IActionResult> GetDesktopFile(string filePath, string? organi

private async Task<IActionResult> GetInstallFile(string organizationId, string platformID)
{
LogRequest(nameof(GetInstallFile));
var settings = await _dataService.GetSettings();
await LogRequest(nameof(GetInstallFile));

if (!await _fileLock.WaitAsync(TimeSpan.FromSeconds(15)))
{
Expand All @@ -173,7 +176,7 @@ private async Task<IActionResult> GetInstallFile(string organizationId, string p
{
case "WindowsInstaller":
{
var effectiveScheme = _appConfig.ForceClientHttps ? "https" : Request.Scheme;
var effectiveScheme = settings.ForceClientHttps ? "https" : Request.Scheme;
var serverUrl = $"{effectiveScheme}://{Request.Host}";
var filePath = Path.Combine(_hostEnv.WebRootPath, "Content", "Remotely_Installer.exe");
var embeddedData = new EmbeddedServerData(new Uri(serverUrl), organizationId);
Expand Down Expand Up @@ -220,17 +223,18 @@ private async Task<IActionResult> GetInstallFile(string organizationId, string p
}
}

private void LogRequest(string methodName)
private async Task LogRequest(string methodName)
{
if (_appConfig.UseHttpLogging)
var settings = await _dataService.GetSettings();
if (settings.UseHttpLogging)
{
var ip = Request.HttpContext.Connection.RemoteIpAddress;
if (ip?.IsIPv4MappedToIPv6 == true)
{
ip = ip.MapToIPv4();
}

var effectiveScheme = _appConfig.ForceClientHttps ? "https" : Request.Scheme;
var effectiveScheme = settings.ForceClientHttps ? "https" : Request.Scheme;

_logger.LogInformation(
"Started client download via {methodName}. Effective Scheme: {scheme}. Effective Host: {host}. Remote IP: {ip}.",
Expand Down
6 changes: 2 additions & 4 deletions Server/API/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Remotely.Server.API;
[Obsolete("This controller is here only for legacy purposes. For new integrations, use API tokens.")]
public class LoginController : ControllerBase
{
private readonly IApplicationConfig _appConfig;
private readonly IDataService _dataService;
private readonly IHubContext<DesktopHub> _desktopHub;
private readonly IRemoteControlSessionCache _remoteControlSessionCache;
Expand All @@ -31,15 +30,13 @@ public class LoginController : ControllerBase
public LoginController(
SignInManager<RemotelyUser> signInManager,
IDataService dataService,
IApplicationConfig appConfig,
IHubContext<DesktopHub> casterHubContext,
IRemoteControlSessionCache remoteControlSessionCache,
IHubContext<ViewerHub> viewerHubContext,
ILogger<LoginController> logger)
{
_signInManager = signInManager;
_dataService = dataService;
_appConfig = appConfig;
_desktopHub = casterHubContext;
_remoteControlSessionCache = remoteControlSessionCache;
_viewerHub = viewerHubContext;
Expand Down Expand Up @@ -76,7 +73,8 @@ public async Task<IActionResult> Logout()
[HttpPost]
public async Task<IActionResult> Post([FromBody] ApiLogin login)
{
if (!_appConfig.AllowApiLogin)
var settings = await _dataService.GetSettings();
if (!settings.AllowApiLogin)
{
return NotFound();
}
Expand Down
11 changes: 5 additions & 6 deletions Server/API/RemoteControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public class RemoteControlController : ControllerBase
private readonly IHubContext<AgentHub, IAgentHubClient> _agentHub;
private readonly IRemoteControlSessionCache _remoteControlSessionCache;
private readonly IAgentHubSessionCache _serviceSessionCache;
private readonly IApplicationConfig _appConfig;
private readonly IDataService _dataService;
private readonly IOtpProvider _otpProvider;
private readonly IHubEventHandler _hubEvents;
private readonly IDataService _dataService;
private readonly SignInManager<RemotelyUser> _signInManager;
private readonly ILogger<RemoteControlController> _logger;

Expand All @@ -42,14 +41,12 @@ public RemoteControlController(
IAgentHubSessionCache serviceSessionCache,
IOtpProvider otpProvider,
IHubEventHandler hubEvents,
IApplicationConfig appConfig,
ILogger<RemoteControlController> logger)
{
_dataService = dataService;
_agentHub = agentHub;
_remoteControlSessionCache = remoteControlSessionCache;
_serviceSessionCache = serviceSessionCache;
_appConfig = appConfig;
_otpProvider = otpProvider;
_hubEvents = hubEvents;
_signInManager = signInManager;
Expand All @@ -72,7 +69,8 @@ public async Task<IActionResult> Get(string deviceID)
[Obsolete("This method is deprecated. Use the GET method along with API keys instead.")]
public async Task<IActionResult> Post([FromBody] RemoteControlRequest rcRequest)
{
if (!_appConfig.AllowApiLogin)
var settings = await _dataService.GetSettings();
if (!settings.AllowApiLogin)
{
return NotFound();
}
Expand Down Expand Up @@ -145,7 +143,8 @@ private async Task<IActionResult> InitiateRemoteControl(string deviceID, string
.OfType<RemoteControlSessionEx>()
.Count(x => x.OrganizationId == orgId);

if (sessionCount > _appConfig.RemoteControlSessionLimit)
var settings = await _dataService.GetSettings();
if (sessionCount > settings.RemoteControlSessionLimit)
{
return BadRequest("There are already the maximum amount of active remote control sessions for your organization.");
}
Expand Down
7 changes: 3 additions & 4 deletions Server/Auth/TwoFactorRequiredHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ namespace Remotely.Server.Auth;
public class TwoFactorRequiredHandler : AuthorizationHandler<TwoFactorRequiredRequirement>
{
private readonly IDataService _dataService;
private readonly IApplicationConfig _appConfig;

public TwoFactorRequiredHandler(IDataService dataService, IApplicationConfig appConfig)
public TwoFactorRequiredHandler(IDataService dataService)
{
_dataService = dataService;
_appConfig = appConfig;
}

protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, TwoFactorRequiredRequirement requirement)
{
var settings = await _dataService.GetSettings();
if (context.User.Identity?.IsAuthenticated == true &&
context.User.Identity.Name is not null &&
_appConfig.Require2FA)
settings.Require2FA)
{
var userResult = await _dataService.GetUserByName(context.User.Identity.Name);

Expand Down
16 changes: 9 additions & 7 deletions Server/Components/Account/Pages/Register.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
@inject NavigationManager NavigationManager
@inject IdentityRedirectManager RedirectManager
@inject IDataService DataService
@inject IApplicationConfig AppConfig
@inject IWebHostEnvironment HostEnv

<PageTitle>Register</PageTitle>
<h1>Register</h1>

@if (!IsRegistrationEnabled())
@if (!_registrationEnabled)
{
<h2>Registration is disabled.</h2>
}
Expand Down Expand Up @@ -69,6 +68,7 @@ else
@code {
private IEnumerable<IdentityError>? identityErrors;
private int _organizationCount;
private bool _registrationEnabled;

[SupplyParameterFromForm]
private InputModel Input { get; set; } = new();
Expand All @@ -78,12 +78,13 @@ else

private string? Message => identityErrors is null ? null : $"Error: {string.Join(", ", identityErrors.Select(error => error.Description))}";

protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
_registrationEnabled = await IsRegistrationEnabled();
_organizationCount = DataService.GetOrganizationCount();
base.OnInitialized();
await base.OnInitializedAsync();
}

public async Task RegisterUser(EditContext editContext)
{
var user = CreateUser();
Expand Down Expand Up @@ -155,9 +156,10 @@ else
return (IUserEmailStore<RemotelyUser>)UserStore;
}

private bool IsRegistrationEnabled()
private async Task<bool> IsRegistrationEnabled()
{
return AppConfig.MaxOrganizationCount < 0 || _organizationCount < AppConfig.MaxOrganizationCount;
var settings = await DataService.GetSettings();
return settings.MaxOrganizationCount < 0 || _organizationCount < settings.MaxOrganizationCount;
}

private sealed class InputModel
Expand Down
15 changes: 1 addition & 14 deletions Server/Components/App.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inject AuthenticationStateProvider AuthProvider
@inject IDataService DataService
@inject IApplicationConfig AppConfig
@inject IDataService DataService
@inject IThemeProvider ThemeProvider

<!DOCTYPE html>
Expand Down Expand Up @@ -28,19 +28,6 @@
<HeadOutlet @rendermode="RenderModeForPage" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<Routes @rendermode="RenderModeForPage" />
<script src="_framework/blazor.web.js"></script>
</body>
Expand Down
10 changes: 6 additions & 4 deletions Server/Components/AuthorizedIndex.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
@inherits AuthComponentBase

@inject NavigationManager NavManager
@inject IApplicationConfig AppConfig
@inject IDataService DataService
@inject SignInManager<RemotelyUser> SignInManager

@if (!string.IsNullOrWhiteSpace(AppConfig.MessageOfTheDay))
@if (!string.IsNullOrWhiteSpace(_settings?.MessageOfTheDay))
{
<div class="me-5">
<AlertBanner Message="@AppConfig.MessageOfTheDay" StatusClass="info" />
<AlertBanner Message="@_settings?.MessageOfTheDay" StatusClass="info" />
</div>
}

Expand All @@ -17,11 +17,12 @@
<ChatFrame />

@code {
private SettingsModel? _settings;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (User is not null &&
AppConfig.Require2FA &&
_settings?.Require2FA == true &&
!User.TwoFactorEnabled)
{
NavManager.NavigateTo("/TwoFactorRequired");
Expand All @@ -31,6 +32,7 @@

protected override async Task OnInitializedAsync()
{
_settings = await DataService.GetSettings();
await base.OnInitializedAsync();

var isAuthenticated = await AuthService.IsAuthenticated();
Expand Down
Loading

0 comments on commit 8afdd97

Please sign in to comment.