Piattaforme: istanza unica dopo lo spostamento dei dati, avvio sicuro del contenitore, rilascio con prove e immagine, accesso senza reindirizzamenti esterni (pkg-1, pkg-2, pkg-3, pkg-4, pkg-5, pkg-6, websec-2, web-8, websec-3)
Registro di casa 192.168.30.23:3000 (HTTP) nel modello Unraid, in compose e nel rilascio con buildx; profilo seccomp fuori da /dati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
36 files changed
+1683
-152
No files matched your search
@@ -26,9 +26,13 @@ internal sealed class DesktopApplication : Application
|
||||
private readonly DesktopFolders _folders;
|
||||
private readonly IDataFolderLocationStore _locations;
|
||||
private readonly bool _dataFolderFixed;
|
||||
private readonly SingleInstanceGuard _instance;
|
||||
|
||||
// The single-instance guards this Febe holds: the one of the folder it started on (taken by Program, disposed there)
|
||||
// and, after a move, the one of the new folder — which every later launch finds recorded.
|
||||
private readonly List<SingleInstanceGuard> _instances;
|
||||
private readonly CancellationTokenSource _lifetime = new();
|
||||
private string _dataFolder;
|
||||
private DataFolderLock? _dataLock;
|
||||
private ImageSource? _iconImage;
|
||||
private TrayIcon? _tray;
|
||||
private DesktopPlatform? _platform;
|
||||
@@ -44,7 +48,7 @@ internal sealed class DesktopApplication : Application
|
||||
_locations = locations;
|
||||
_dataFolder = dataFolder;
|
||||
_dataFolderFixed = dataFolderFixed;
|
||||
_instance = instance;
|
||||
_instances = [instance];
|
||||
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
||||
DispatcherUnhandledException += OnDispatcherUnhandledException;
|
||||
TaskScheduler.UnobservedTaskException += (_, e) => e.SetObserved();
|
||||
@@ -85,13 +89,34 @@ internal sealed class DesktopApplication : Application
|
||||
{
|
||||
moved = await MoveDataFolderAsync().ConfigureAwait(true);
|
||||
_dataFolder = moved.DataFolder;
|
||||
if (moved.TargetGuard is SingleInstanceGuard guard)
|
||||
{
|
||||
_instances.Add(guard);
|
||||
}
|
||||
|
||||
if (moved.Message is { } message)
|
||||
{
|
||||
_tray.Notify("Cartella dei dati", message, moved.Outcome != DataFolderMoveOutcome.Moved);
|
||||
}
|
||||
}
|
||||
|
||||
var native = new WindowsShell(Dispatcher, () => _main, () => _tray);
|
||||
// The folder itself is locked too: the guards are named after its path, the lock is the folder (spec §12).
|
||||
if (DataFolderLock.TryAcquire(_dataFolder, out _dataLock) == DataFolderLockOutcome.HeldByAnother)
|
||||
{
|
||||
if (!_options.StartHidden)
|
||||
{
|
||||
MessageBox.Show(
|
||||
$"Febe è già aperto con questa cartella dei dati ({_dataFolder}): cercalo nell'area di notifica, accanto all'orologio.",
|
||||
"Febe",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Information);
|
||||
}
|
||||
|
||||
await ExitAsync().ConfigureAwait(true);
|
||||
return;
|
||||
}
|
||||
|
||||
var native = new WindowsShell(Dispatcher, () => _main, () => _tray, _dataFolderFixed ? _dataFolder : null);
|
||||
var shell = new DesktopShell(Dispatcher, _iconImage, native);
|
||||
_platform = new DesktopPlatform(_options, shell, _locations, _dataFolderFixed);
|
||||
_app = await FebeApp.StartAsync(_options.AppOptions(_dataFolder, _folders), _platform, new FebeAppHooks(), _lifetime.Token).ConfigureAwait(true);
|
||||
@@ -124,13 +149,17 @@ internal sealed class DesktopApplication : Application
|
||||
return;
|
||||
}
|
||||
|
||||
_instance.Listen(request => Dispatcher.InvokeAsync(() =>
|
||||
foreach (var instance in _instances)
|
||||
{
|
||||
if (request == InstanceRequest.Show)
|
||||
instance.Listen(request => Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
ShowMain();
|
||||
}
|
||||
}));
|
||||
if (request == InstanceRequest.Show)
|
||||
{
|
||||
ShowMain();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
_ = Task.Run(() => FollowEngineAsync(_lifetime.Token));
|
||||
|
||||
if (!_options.StartHidden || _app.Problems.Count > 0)
|
||||
@@ -298,7 +327,7 @@ internal sealed class DesktopApplication : Application
|
||||
bar.Value = p.FilesDone;
|
||||
});
|
||||
var current = _dataFolder;
|
||||
return await Task.Run(() => DataFolderMove.ApplyPending(_locations, current, progress)).ConfigureAwait(true);
|
||||
return await Task.Run(() => DataFolderMove.ApplyPending(_locations, current, progress, claimTarget: SingleInstanceGuard.TryAcquire)).ConfigureAwait(true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -335,6 +364,21 @@ internal sealed class DesktopApplication : Application
|
||||
_tray = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Everything has stopped: the lock of the Data folder and the guard of a moved folder go last. The guard of the
|
||||
/// folder Febe started on belongs to Program, which disposes it when the application returns.
|
||||
/// </summary>
|
||||
protected override void OnExit(ExitEventArgs e)
|
||||
{
|
||||
_dataLock?.Dispose();
|
||||
foreach (var instance in _instances.Skip(1))
|
||||
{
|
||||
instance.Dispose();
|
||||
}
|
||||
|
||||
base.OnExit(e);
|
||||
}
|
||||
|
||||
/// <summary>Windows is shutting down or Alberto logs off: stop in order while the messages still flow (S3).</summary>
|
||||
private void OnSessionEnding(object sender, SessionEndingCancelEventArgs e)
|
||||
{
|
||||
|
||||
@@ -89,6 +89,8 @@ internal sealed class DesktopShell(Dispatcher ui, ImageSource? icon, INativeShel
|
||||
Win32.SetWindowPos(window, IntPtr.Zero, x, y, 0, 0, Win32.SwpNoSize | Win32.SwpNoZOrder | Win32.SwpNoActivate);
|
||||
}).Task;
|
||||
|
||||
public bool EndWithFebe(Process process) => ProcessJob.Add(process);
|
||||
|
||||
private static IntPtr? MainWindowOf(int processId)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace Febe.Desktop.Native;
|
||||
|
||||
/// <summary>
|
||||
/// A job object of Windows with «kill on job close»: the processes put in it end when Febe.exe ends, however it ends
|
||||
/// (Esci, a crash, «Termina attività»), because Windows closes the job's only handle with the process. The processes
|
||||
/// they start join the job too. Used for Edge, the last fallback of S2 (spec §6.6): without it an Edge left behind keeps
|
||||
/// the profile and the debugging port 9222, and every later start of Febe finds the port «occupata».
|
||||
/// </summary>
|
||||
internal static class ProcessJob
|
||||
{
|
||||
private const int JobObjectExtendedLimitInformation = 9;
|
||||
private const uint JobObjectLimitKillOnJobClose = 0x00002000;
|
||||
|
||||
private static readonly Lazy<SafeFileHandle?> Job = new(Create, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
|
||||
/// <summary>Puts <paramref name="process"/> in the job; false when Windows refuses (the process then lives on its own).</summary>
|
||||
public static bool Add(Process process)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(process);
|
||||
if (Job.Value is not { IsInvalid: false } job)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return AssignProcessToJobObject(job, process.Handle);
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidOperationException or System.ComponentModel.Win32Exception or NotSupportedException)
|
||||
{
|
||||
// The process is already gone.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The job, created once and never closed: closing it would end the processes inside it.</summary>
|
||||
private static SafeFileHandle? Create()
|
||||
{
|
||||
var job = CreateJobObject(IntPtr.Zero, null);
|
||||
if (job.IsInvalid)
|
||||
{
|
||||
job.Dispose();
|
||||
return null;
|
||||
}
|
||||
|
||||
var limits = new ExtendedLimitInformation { BasicLimitInformation = new BasicLimitInformation { LimitFlags = JobObjectLimitKillOnJobClose } };
|
||||
var size = Marshal.SizeOf<ExtendedLimitInformation>();
|
||||
var buffer = Marshal.AllocHGlobal(size);
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(limits, buffer, fDeleteOld: false);
|
||||
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, buffer, (uint)size))
|
||||
{
|
||||
job.Dispose();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
}
|
||||
|
||||
return job;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
private static extern SafeFileHandle CreateJobObject(IntPtr attributes, string? name);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool SetInformationJobObject(SafeFileHandle job, int infoClass, IntPtr info, uint length);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool AssignProcessToJobObject(SafeFileHandle job, IntPtr process);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct BasicLimitInformation
|
||||
{
|
||||
public long PerProcessUserTimeLimit;
|
||||
public long PerJobUserTimeLimit;
|
||||
public uint LimitFlags;
|
||||
public UIntPtr MinimumWorkingSetSize;
|
||||
public UIntPtr MaximumWorkingSetSize;
|
||||
public uint ActiveProcessLimit;
|
||||
public UIntPtr Affinity;
|
||||
public uint PriorityClass;
|
||||
public uint SchedulingClass;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct IoCounters
|
||||
{
|
||||
public ulong ReadOperationCount;
|
||||
public ulong WriteOperationCount;
|
||||
public ulong OtherOperationCount;
|
||||
public ulong ReadTransferCount;
|
||||
public ulong WriteTransferCount;
|
||||
public ulong OtherTransferCount;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct ExtendedLimitInformation
|
||||
{
|
||||
public BasicLimitInformation BasicLimitInformation;
|
||||
public IoCounters IoInfo;
|
||||
public UIntPtr ProcessMemoryLimit;
|
||||
public UIntPtr JobMemoryLimit;
|
||||
public UIntPtr PeakProcessMemoryUsed;
|
||||
public UIntPtr PeakJobMemoryUsed;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@ namespace Febe.Desktop;
|
||||
|
||||
/// <summary>
|
||||
/// One Febe at a time for a Data folder (spec §12): a named mutex in the session namespace, and a named pipe (current
|
||||
/// user only) through which a second launch asks the first to show its window, then exits.
|
||||
/// user only) through which a second launch asks the first to show its window, then exits. What counts is that the
|
||||
/// mutex exists, not which thread owns it: a guard can be taken on any thread (the move of the Data folder takes the
|
||||
/// guard of the new folder on a worker thread) and lives until it is disposed or the process ends.
|
||||
/// </summary>
|
||||
internal sealed class SingleInstanceGuard : IDisposable
|
||||
{
|
||||
@@ -24,7 +26,8 @@ internal sealed class SingleInstanceGuard : IDisposable
|
||||
/// <summary>The guard of the first instance, or null when another Febe already runs on this Data folder.</summary>
|
||||
public static SingleInstanceGuard? TryAcquire(string dataFolder)
|
||||
{
|
||||
var mutex = new Mutex(initiallyOwned: true, InstanceIdentity.MutexName(dataFolder), out var created);
|
||||
// Not owned: ownership belongs to a thread, while the guard belongs to the process.
|
||||
var mutex = new Mutex(initiallyOwned: false, InstanceIdentity.MutexName(dataFolder), out var created);
|
||||
if (!created)
|
||||
{
|
||||
mutex.Dispose();
|
||||
@@ -69,15 +72,6 @@ internal sealed class SingleInstanceGuard : IDisposable
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_mutex.ReleaseMutex();
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
// Released on another thread already: Windows releases it when the process ends.
|
||||
}
|
||||
|
||||
_mutex.Dispose();
|
||||
_stop.Dispose();
|
||||
}
|
||||
|
||||
@@ -12,9 +12,11 @@ namespace Febe.Desktop;
|
||||
/// «Mostra nella cartella» (<c>explorer.exe /select,"…"</c>), addresses in the default browser (shell execute), Windows
|
||||
/// notifications (balloon tips of the notification-area icon: the alerts of spec §13.3 arrive here through
|
||||
/// <see cref="NativeShellAlertChannel"/>, and «Invia una prova») and start with the computer
|
||||
/// (<c>HKCU\…\Run</c>, value <c>Febe</c>). Paths and addresses never come from the page: the web layer resolves them.
|
||||
/// (<c>HKCU\…\Run</c>, value <c>Febe</c>, or <c>Febe-…</c> with <c>--dati</c> for a Febe started on a Data folder of
|
||||
/// its own: <paramref name="fixedDataFolder"/>, see <see cref="Autostart"/>). Paths and addresses never come from the
|
||||
/// page: the web layer resolves them.
|
||||
/// </summary>
|
||||
internal sealed class WindowsShell(Dispatcher ui, Func<Window?> owner, Func<TrayIcon?> tray) : INativeShell
|
||||
internal sealed class WindowsShell(Dispatcher ui, Func<Window?> owner, Func<TrayIcon?> tray, string? fixedDataFolder) : INativeShell
|
||||
{
|
||||
public bool CanPickFolder => true;
|
||||
|
||||
@@ -85,8 +87,8 @@ internal sealed class WindowsShell(Dispatcher ui, Func<Window?> owner, Func<Tray
|
||||
public Task<bool> GetAutostartAsync(CancellationToken ct)
|
||||
{
|
||||
using var key = Registry.CurrentUser.OpenSubKey(Autostart.RunKey, writable: false);
|
||||
var value = key?.GetValue(Autostart.ValueName) as string;
|
||||
return Task.FromResult(Environment.ProcessPath is { } exe && Autostart.PointsTo(value, exe));
|
||||
var value = key?.GetValue(Autostart.ValueNameFor(fixedDataFolder)) as string;
|
||||
return Task.FromResult(Environment.ProcessPath is { } exe && Autostart.PointsTo(value, exe, fixedDataFolder));
|
||||
}
|
||||
|
||||
public Task SetAutostartAsync(bool enabled, CancellationToken ct)
|
||||
@@ -95,11 +97,11 @@ internal sealed class WindowsShell(Dispatcher ui, Func<Window?> owner, Func<Tray
|
||||
if (enabled)
|
||||
{
|
||||
var exe = Environment.ProcessPath ?? throw new InvalidOperationException("Percorso di Febe.exe sconosciuto.");
|
||||
key.SetValue(Autostart.ValueName, Autostart.Command(exe), RegistryValueKind.String);
|
||||
key.SetValue(Autostart.ValueNameFor(fixedDataFolder), Autostart.Command(exe, fixedDataFolder), RegistryValueKind.String);
|
||||
}
|
||||
else
|
||||
{
|
||||
key.DeleteValue(Autostart.ValueName, throwOnMissingValue: false);
|
||||
key.DeleteValue(Autostart.ValueNameFor(fixedDataFolder), throwOnMissingValue: false);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
Reference in new issue
Block a user