Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/Keyden.App/AgentK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ private class KeyInfo
}
private readonly Dictionary<string, KeyInfo> KeyInfos = new();

public void AddActivity(string title, string description, string icon = "fa-circle-info", ActivityImportance importance = ActivityImportance.Normal)
{
NewActivity?.Invoke(new ActivityItem()
{
Title = title,
Description = description,
Icon = icon,
Importance = importance,
});
}

private AuthRequired QueryAuth(SshKey key, SshKeyOptions options, ClientInfo clientInfo)
{
Expand Down
137 changes: 135 additions & 2 deletions src/Keyden.App/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

using Keyden.ViewModels;
using Keyden.Views;
using Avalonia.Controls;
using System.Text.Json;
using System.Text;
using System.IO.Pipes;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;

namespace Keyden;

Expand Down Expand Up @@ -55,8 +61,122 @@ public static T GetKeyedService<T>(object? key) where
private SshAgent? Agent { get; set; }
private KeydenSettings? Settings { get; set; }

public static string AppPipePath
{
get
{
if (false&&RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return $"{Environment.UserName}/keyden";
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var uid = Unix.Getuid().ToString(CultureInfo.InvariantCulture);
var socketDirectory = $"/run/user/{uid}";

// check for legacy linux systems
if (!Directory.Exists(socketDirectory))
socketDirectory = Directory.Exists("/run") ? "/run" : "/var/run";

if (!Directory.Exists(socketDirectory))
return "keyden";

socketDirectory += "/keyden";
var socketPath = $"{socketDirectory}/keyden.sock";

if (!Directory.Exists(socketDirectory))
Directory.CreateDirectory(socketDirectory);
return socketPath;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var uid = Unix.Getuid().ToString(CultureInfo.InvariantCulture);
return $"/var/run/{uid}-keyden.sock";
}

return "keyden";
}
}

private async void MainAppPipeThread(CancellationToken ct)
{
var agentK = GetService<AgentK>();
try
{
using var pipeServer = new NamedPipeServerStream(
pipeName: AppPipePath,
direction: PipeDirection.InOut,
maxNumberOfServerInstances: NamedPipeServerStream.MaxAllowedServerInstances,
transmissionMode: PipeTransmissionMode.Byte,
options: PipeOptions.Asynchronous | PipeOptions.WriteThrough | PipeOptions.CurrentUserOnly | PipeOptions.FirstPipeInstance,
inBufferSize: 1,
outBufferSize: 1);


while (!ct.IsCancellationRequested)
{
try
{
await pipeServer.WaitForConnectionAsync(ct);
var byteRead = pipeServer.ReadByte();
if (byteRead == 0x0A)
{
agentK.AddActivity("Keyden opened", "Another Keyden instance was opened, showing main window instead.");

ShowMainWindow();
pipeServer.WriteByte(0xA0);
pipeServer.Flush();
await Task.Delay(100);
}
}
catch (OperationCanceledException) { }
catch (IOException) { }
catch (Exception ex)
{
agentK.AddActivity("Error in single instance host", ex.ToString(), "fa-circle-exclamation", ViewModels.ActivityImportance.Warning);
}
finally
{
if (pipeServer.IsConnected)
pipeServer.Disconnect();
}
}
}
catch (Exception ex)
{
agentK.AddActivity("Critical error in single instance host", ex.ToString(), "fa-circle-exclamation", ViewModels.ActivityImportance.Critical);
}
}

private CancellationTokenSource AppPipeCts = new();

public override void OnFrameworkInitializationCompleted()
{
Exception? singleInstanceEx = null;
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
{
try
{
using var client = new NamedPipeClientStream(pipeName: AppPipePath);
client.Connect(100);
if (client.IsConnected)
{
client.WriteByte(0x0A);
client.Flush();
if (client.ReadByte() == 0xA0)
Environment.Exit(0);
}
}
catch (IOException) { }
catch (TimeoutException) { }
catch (Exception ex)
{
singleInstanceEx = ex;
}
}

var collection = new ServiceCollection();

collection.AddSingleton(SystemServices);
Expand All @@ -70,18 +190,31 @@ public override void OnFrameworkInitializationCompleted()
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;

bool isAutomaticStart =
SystemServices.IsAutomaticStart ||
desktop.Args?.Contains("--hide") == true;

if (!isAutomaticStart)
ShowMainWindow();

MainAppPipeThread(AppPipeCts.Token);
desktop.Exit += DesktopAppExit;
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
singleViewPlatform.MainView = new MainView();

base.OnFrameworkInitializationCompleted();

if (singleInstanceEx is not null)
{
var agentK = GetService<AgentK>();
agentK.AddActivity("Error in single instance client", singleInstanceEx.ToString(), "fa-circle-exclamation", ViewModels.ActivityImportance.Critical);
}
}

private void DesktopAppExit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
AppPipeCts.Cancel();
}

private static SettingsWindow? SettingsWindow { get; set; }
Expand Down
3 changes: 1 addition & 2 deletions src/Keyden.Core/ISystemServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,7 +12,7 @@ public interface ISystemServices
bool IsAutomaticStart { get; }

TimeSpan UserIdleDuration { get; }
public event EventHandler<EventArgs> MachineLocked;
event EventHandler<EventArgs> MachineLocked;

Task<AuthResult> TryAuthUser(
AuthRequired authRequired,
Expand Down