-
Notifications
You must be signed in to change notification settings - Fork 1
/
PwshIPC.cs
73 lines (70 loc) · 2.37 KB
/
PwshIPC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Buffers.Text;
using System.IO.Pipes;
using System.Windows;
using System.Threading;
using System.Collections.Concurrent;
using System.Security.Principal;
using Serilog;
namespace SSHMan
{
public class MessageItem
{
public Guid Id { get; set; }
public string Data { get; set; }
public bool LastMessage { get; set; }
}
public static class PwshIPC
{
public static MessageItem Message(string message, bool exitAfterDelivery, Guid id)
{
Log.Information("IPC Message out: {msg}",message);
var item = new MessageItem()
{
Id = id,
Data = message,
LastMessage = exitAfterDelivery
};
var success = ThreadPool.QueueUserWorkItem(ProvideMessage, item);
if(!success)
{
_ = MessageBox.Show("Failed to spawn message thread", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
return item;
}
public static void ProvideMessage(object payload)
{
if(payload == null) {
throw new ArgumentNullException(nameof(payload));
}
var message = (MessageItem)payload;
var pipename = $"sshmancom-{message.Id}";
var pipeServer = new NamedPipeServerStream(pipename, PipeDirection.InOut, 1);
Log.Information("Worker started offering message on '.{path}.' Waiting for connection...", pipename);
pipeServer.WaitForConnection();
try
{
Log.Information("Client connected!");
var stream = new StreamString(pipeServer);
var bytesWritten = stream.WriteString(message.Data);
Log.Information("Transfered ipc message to {user}", pipeServer.GetImpersonationUserName());
if(message.LastMessage)
{
MainWindow.ShutdownSignal.Set();
}
}
catch (IOException e)
{
_ = MessageBox.Show($"ERROR: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
pipeServer.Dispose();
}
}
}
}