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
190 lines
6.4 KiB
C#
190 lines
6.4 KiB
C#
using System.Text;
|
|
|
|
namespace Febe.Desktop.Core;
|
|
|
|
/// <summary>
|
|
/// «Avvia Febe all'accensione del computer» (spec §12): a value under <c>HKCU\Software\Microsoft\Windows\CurrentVersion\Run</c>,
|
|
/// per user, no administrator rights. Febe then starts with <c>--nascosto</c>: only the icon in the notification area,
|
|
/// the window stays closed. Each Febe has its own value: <c>Febe</c> for the usual one (Data folder recorded or
|
|
/// default), <c>Febe-…</c> with <c>--dati</c> for one started on a Data folder of its own (a second account), so the
|
|
/// switch of one never starts, shows or removes the other.
|
|
/// </summary>
|
|
public static class Autostart
|
|
{
|
|
/// <summary>The registry key, under HKEY_CURRENT_USER.</summary>
|
|
public const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
|
|
|
|
/// <summary>The name of the value of the usual Febe.</summary>
|
|
public const string ValueName = "Febe";
|
|
|
|
/// <summary>
|
|
/// The name of the value: <see cref="ValueName"/> without a Data folder of its own, otherwise «Febe-» and the
|
|
/// identity of the folder (the same as the single-instance guard, so the same folder always has the same value).
|
|
/// </summary>
|
|
public static string ValueNameFor(string? fixedDataFolder) =>
|
|
fixedDataFolder is null ? ValueName : InstanceIdentity.For(fixedDataFolder);
|
|
|
|
/// <summary>
|
|
/// The command written in the value: the quoted executable, <c>--nascosto</c> and, for a Febe started with
|
|
/// <c>--dati</c>, the same Data folder.
|
|
/// </summary>
|
|
public static string Command(string executable, string? fixedDataFolder = null)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(executable);
|
|
if (executable.Contains('"', StringComparison.Ordinal))
|
|
{
|
|
throw new ArgumentException("Percorso dell'eseguibile non valido.", nameof(executable));
|
|
}
|
|
|
|
if (fixedDataFolder is null)
|
|
{
|
|
return $"\"{executable}\" --nascosto";
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(fixedDataFolder) || fixedDataFolder.Contains('"', StringComparison.Ordinal))
|
|
{
|
|
throw new ArgumentException("Percorso della cartella dei dati non valido.", nameof(fixedDataFolder));
|
|
}
|
|
|
|
return $"\"{executable}\" --nascosto --dati {Quote(fixedDataFolder)}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when <paramref name="value"/> starts this executable on this Data folder: <paramref name="fixedDataFolder"/>
|
|
/// for a Febe started with <c>--dati</c>, none for the usual one. A value left by another copy of Febe, or written
|
|
/// for another Data folder, is not ours.
|
|
/// </summary>
|
|
public static bool PointsTo(string? value, string executable, string? fixedDataFolder = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(executable))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var arguments = Split(value.Trim());
|
|
if (arguments is not [var target, .. var rest] || !SamePath(target, executable))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string? dataFolder = null;
|
|
for (var i = 0; i < rest.Count; i++)
|
|
{
|
|
if (string.Equals(rest[i], "--dati", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (i + 1 >= rest.Count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
dataFolder = rest[++i];
|
|
}
|
|
}
|
|
|
|
return fixedDataFolder is null ? dataFolder is null : dataFolder is not null && SamePath(dataFolder, fixedDataFolder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The words of a command line as Windows programs read them: blanks separate, quotes group, backslashes are plain
|
|
/// except before a quote (2n backslashes and a quote: n backslashes and a quote that groups; 2n+1: n and a literal
|
|
/// quote). Null for an unfinished quote.
|
|
/// </summary>
|
|
internal static List<string>? Split(string commandLine)
|
|
{
|
|
var words = new List<string>();
|
|
var word = new StringBuilder();
|
|
var inWord = false;
|
|
var quoted = false;
|
|
for (var i = 0; i < commandLine.Length; i++)
|
|
{
|
|
var c = commandLine[i];
|
|
if (c == '\\')
|
|
{
|
|
var start = i;
|
|
while (i < commandLine.Length && commandLine[i] == '\\')
|
|
{
|
|
i++;
|
|
}
|
|
|
|
var count = i - start;
|
|
if (i < commandLine.Length && commandLine[i] == '"')
|
|
{
|
|
word.Append('\\', count / 2);
|
|
if (count % 2 == 1)
|
|
{
|
|
word.Append('"');
|
|
}
|
|
else
|
|
{
|
|
quoted = !quoted;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
word.Append('\\', count);
|
|
i--;
|
|
}
|
|
|
|
inWord = true;
|
|
continue;
|
|
}
|
|
|
|
if (c == '"')
|
|
{
|
|
quoted = !quoted;
|
|
inWord = true;
|
|
continue;
|
|
}
|
|
|
|
if (!quoted && char.IsWhiteSpace(c))
|
|
{
|
|
if (inWord)
|
|
{
|
|
words.Add(word.ToString());
|
|
word.Clear();
|
|
inWord = false;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
word.Append(c);
|
|
inWord = true;
|
|
}
|
|
|
|
if (quoted)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (inWord)
|
|
{
|
|
words.Add(word.ToString());
|
|
}
|
|
|
|
return words;
|
|
}
|
|
|
|
/// <summary>A quoted argument: backslashes before the closing quote are doubled (<c>"D:\\"</c> reads back as <c>D:\</c>).</summary>
|
|
private static string Quote(string argument)
|
|
{
|
|
var trailing = argument.Length - argument.TrimEnd('\\').Length;
|
|
return "\"" + argument + new string('\\', trailing) + "\"";
|
|
}
|
|
|
|
private static bool SamePath(string a, string b) =>
|
|
string.Equals(Normalize(a), Normalize(b), StringComparison.OrdinalIgnoreCase);
|
|
|
|
private static string Normalize(string path)
|
|
{
|
|
try
|
|
{
|
|
return Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
|
|
}
|
|
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
}
|