summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.Helpers/PipeServer.cs
blob: 36c40c31ff5d395f32280c014fd906f30341dd06 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.IO;
using System.IO.Pipes;
using System.Security.AccessControl;
using System.Security.Principal;
using Assinador.Model.Common;
using Gestor.Application.Actions;
using Newtonsoft.Json;

namespace Gestor.Application.Helpers;

public class PipeServer : IDisposable
{
	private string _pipeName;

	private NamedPipeServerStream Pipe { get; set; }

	public bool CreateServer(string name)
	{
		_pipeName = name;
		return Create();
	}

	private bool Create()
	{
		bool flag = true;
		try
		{
			new NamedPipeClientStream(".", _pipeName, PipeDirection.Out, PipeOptions.Asynchronous).Connect(1000);
		}
		catch (TimeoutException)
		{
			flag = false;
		}
		catch (Exception)
		{
			return false;
		}
		if (flag)
		{
			return true;
		}
		try
		{
			PipeSecurity pipeSecurity = new PipeSecurity();
			SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
			securityIdentifier.Translate(typeof(NTAccount));
			pipeSecurity.SetAccessRule(new PipeAccessRule(securityIdentifier, PipeAccessRights.ReadWrite, AccessControlType.Allow));
			Pipe = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1, 1, pipeSecurity);
			Pipe.BeginWaitForConnection(WaitForConnectionCallBack, Pipe);
		}
		catch (Exception)
		{
			return false;
		}
		return true;
	}

	private void WaitForConnectionCallBack(IAsyncResult iar)
	{
		try
		{
			NamedPipeServerStream namedPipeServerStream = (NamedPipeServerStream)iar.AsyncState;
			namedPipeServerStream.EndWaitForConnection(iar);
			using (StreamReader streamReader = new StreamReader(Pipe))
			{
				string text = streamReader.ReadLine();
				if (text != null && text.IndexOf("exit", StringComparison.InvariantCultureIgnoreCase) > -1)
				{
					Dispose();
				}
				Handle(text);
			}
			namedPipeServerStream.Close();
			namedPipeServerStream = null;
			Create();
		}
		catch
		{
		}
	}

	public void Dispose()
	{
		Pipe.Dispose();
	}

	private void Handle(string message)
	{
		if (message != null)
		{
			PipeMessageResult obj = JsonConvert.DeserializeObject<PipeMessageResult>(message);
			Gestor.Application.Actions.Actions.AcessarHoster?.Invoke(obj);
		}
	}
}