Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,7 @@ Server.Installer/Properties/launchSettings.json
!/.vscode/tasks.json
/Server/appsettings.Development.json
/Server/AppData
#AGENTS
.agents/
.claude/
.cursor/
6 changes: 3 additions & 3 deletions Agent/Services/AgentHubConnection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -569,9 +569,9 @@ private void EnsureHubConnection()
throw new InvalidOperationException("Hub connection is not established.");
}
}
private async void HeartbeatTimer_Elapsed(object? sender, ElapsedEventArgs e)
private void HeartbeatTimer_Elapsed(object? sender, ElapsedEventArgs e)
{
await SendHeartbeat();
_ = SendHeartbeat();
}

private async Task HubConnection_Reconnected(string? arg)
Expand Down
34 changes: 31 additions & 3 deletions Agent/Services/ChatClientService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Agent.Models;
Expand All @@ -19,12 +19,13 @@ public interface IChatClientService
Task SendMessage(string senderName, string message, string orgName, string orgId, bool disconnected, string senderConnectionID, HubConnection hubConnection);
}

public class ChatClientService : IChatClientService
public class ChatClientService : IChatClientService, IDisposable
{
private readonly IAppLauncher _appLauncher;
private readonly ILogger<ChatClientService> _logger;
private readonly MemoryCache _chatClients = new("ChatClients");
private readonly SemaphoreSlim _messageLock = new(1, 1);
private bool _disposed;

private readonly CacheItemPolicy _cacheItemPolicy = new()
{
Expand All @@ -44,7 +45,10 @@ public class ChatClientService : IChatClientService
chatProcess.Kill();
}
}
catch { }
catch (Exception ex)
{
_logger.LogWarning(ex, "Error removing chat client from cache.");
}
})
};

Expand Down Expand Up @@ -146,4 +150,28 @@ private async Task ReadFromStream(NamedPipeClientStream clientPipe, string sende
await hubConnection.SendAsync("Chat", string.Empty, true, senderConnectionID);
_chatClients.Remove(senderConnectionID);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_messageLock?.Dispose();
_chatClients?.Dispose();
}
_disposed = true;
}
}

~ChatClientService()
{
Dispose(false);
}
}
12 changes: 9 additions & 3 deletions Agent/Services/ExternalScriptingShell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Dtos;
using Remotely.Shared.Enums;
Expand Down Expand Up @@ -125,7 +125,7 @@ public async Task<ScriptResultDto> WriteInput(string input, TimeSpan timeout)
ShellProcess.StandardInput.Write(input + _lineEnding);
ShellProcess.StandardInput.Write("echo " + _lastInputID + _lineEnding);

var result = await Task.WhenAny(
var resultTask = Task.WhenAny(
Task.Run(() =>
{
return ShellProcess.WaitForExit((int)timeout.TotalMilliseconds);
Expand All @@ -134,7 +134,9 @@ public async Task<ScriptResultDto> WriteInput(string input, TimeSpan timeout)
{
return _outputDone.WaitOne();

})).ConfigureAwait(false).GetAwaiter().GetResult();
}));

var result = await resultTask.ConfigureAwait(false);

if (!result)
{
Expand Down Expand Up @@ -177,6 +179,10 @@ protected virtual void Dispose(bool disposing)
{
_logger.LogError(ex, "Error while disposing scripting shell process.");
}

_outputDone?.Dispose();
_writeLock?.Dispose();
_processIdleTimeout?.Dispose();
}

_disposedValue = true;
Expand Down
7 changes: 5 additions & 2 deletions Agent/Services/FileLogsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Remotely.Shared.Services;
using Remotely.Shared.Services;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -62,7 +62,10 @@ public async Task DeleteLogs(CancellationToken cancellationToken)
{
File.Delete(file);
}
catch { }
catch (Exception ex)
{
Console.WriteLine($"Error deleting log file {file}: {ex.Message}");
}
}
}
}
Expand Down
34 changes: 30 additions & 4 deletions Agent/Services/Linux/UpdaterLinux.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Utilities;
using System;
Expand All @@ -14,7 +14,7 @@
namespace Remotely.Agent.Services.Linux;


public class UpdaterLinux : IUpdater
public class UpdaterLinux : IUpdater, IDisposable
{
private readonly SemaphoreSlim _checkForUpdatesLock = new(1, 1);
private readonly IConfigService _configService;
Expand All @@ -24,6 +24,7 @@ public class UpdaterLinux : IUpdater
private readonly SemaphoreSlim _installLatestVersionLock = new(1, 1);
private readonly System.Timers.Timer _updateTimer = new(TimeSpan.FromHours(6).TotalMilliseconds);
private DateTimeOffset _lastUpdateFailure;
private bool _disposed;

public UpdaterLinux(
IConfigService configService,
Expand Down Expand Up @@ -177,9 +178,34 @@ await _updateDownloader.DownloadFile(
}
}

private async void UpdateTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
private void UpdateTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
await CheckForUpdates();
_ = Task.Run(() => CheckForUpdates());
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_updateTimer?.Dispose();
_checkForUpdatesLock?.Dispose();
_installLatestVersionLock?.Dispose();
}
_disposed = true;
}
}

~UpdaterLinux()
{
Dispose(false);
}

}
14 changes: 11 additions & 3 deletions Agent/Services/MacOS/UpdaterMac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Remotely.Agent.Services.MacOS;

public class UpdaterMac : IUpdater
public class UpdaterMac : IUpdater, IDisposable
{
private readonly string _achitecture = RuntimeInformation.OSArchitecture.ToString().ToLower();
private readonly SemaphoreSlim _checkForUpdatesLock = new(1, 1);
Expand Down Expand Up @@ -163,9 +163,17 @@ await _updateDownloader.DownloadFile(
}
}

private async void UpdateTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
private void UpdateTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
await CheckForUpdates();
_ = Task.Run(() => CheckForUpdates());
}

public void Dispose()
{
_updateTimer?.Stop();
_updateTimer?.Dispose();
_checkForUpdatesLock.Dispose();
_installLatestVersionLock.Dispose();
}

}
33 changes: 30 additions & 3 deletions Agent/Services/ScriptingShellFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Remotely.Shared.Enums;
using System;
Expand All @@ -12,10 +12,11 @@ public interface IScriptingShellFactory
IPsCoreShell GetOrCreatePsCoreShell(string senderConnectionId);
}

internal class ScriptingShellFactory : IScriptingShellFactory
internal class ScriptingShellFactory : IScriptingShellFactory, IDisposable
{
private readonly MemoryCache _sessionCache = new(new MemoryCacheOptions());
private readonly IServiceProvider _serviceProvider;
private bool _disposed;

public ScriptingShellFactory(IServiceProvider serviceProvider)
{
Expand Down Expand Up @@ -84,11 +85,37 @@ private MemoryCacheEntryOptions GetEntryOptions()
{
disposable.Dispose();
}
catch { }
catch (Exception ex)
{
Console.WriteLine($"Error disposing scripting shell: {ex.Message}");
}
}
}
});

return options;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_sessionCache?.Dispose();
}
_disposed = true;
}
}

~ScriptingShellFactory()
{
Dispose(false);
}
}
Loading