-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProxyBase.cs
242 lines (205 loc) · 7.01 KB
/
ProxyBase.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Threading;
namespace KeePassNatMsgProxy
{
public abstract class ProxyBase
{
protected const string ProxyName = "kpxc_server";
private const int BufferSize = 1024 * 1024;
private Thread _readThread;
private Thread _writeThread;
private bool _active;
private readonly object _writeLock;
/// <summary>
/// Initialize a new instance of ProxyBase.
/// </summary>
protected ProxyBase()
{
_writeLock = new object();
}
/// <summary>
/// Create a thread and run the proxy.
/// </summary>
/// <exception cref="KeePassNatMsgProxy.ProxyException">Thrown when any kind of error occurs while create and run the proxy.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<optimization>")]
public void Run()
{
_active = true;
// Connect proxy to target process.
try
{
Connect();
}
catch (Exception)
{
return;
}
// Create the reader thread.
try
{
_readThread = new Thread(ClientReadThread)
{
Name = nameof(ClientReadThread)
};
_readThread.Start();
}
catch (ArgumentNullException anex)
{
throw new ProxyException("Argument null error while creating Reader thread.", anex);
}
catch (ThreadStateException tsex)
{
throw new ProxyException("Thread state error while creating Reader thread.", tsex);
}
catch (OutOfMemoryException oomex)
{
throw new ProxyException("Out of memory error while creating Reader thread.", oomex);
}
catch (Exception ex)
{
throw new ProxyException("Error while creating Reader thread.", ex);
}
// Create the reader thread.
try
{
_writeThread = new Thread(ClientWriteThread)
{
Name = nameof(ClientWriteThread)
};
_writeThread.Start();
}
catch (ArgumentNullException anex)
{
throw new ProxyException("Argument null error while creating Writer thread.", anex);
}
catch (ThreadStateException tsex)
{
throw new ProxyException("Thread state error while creating Writer thread.", tsex);
}
catch (OutOfMemoryException oomex)
{
throw new ProxyException("Out of memory error while creating Writer thread.", oomex);
}
catch (Exception ex)
{
throw new ProxyException("Error while creating Writer thread.", ex);
}
// Wait until Writer ends.
try
{
_writeThread.Join();
}
catch (ThreadStateException tsex)
{
throw new ProxyException("Thread state error while Writer thread ends.", tsex);
}
catch (ThreadInterruptedException tiex)
{
throw new ProxyException("Writer thread received interrupion call.", tiex);
}
catch (Exception ex)
{
throw new ProxyException("Error while Writer thread ends.", ex);
}
// Close connection to target process.
Close();
// Wait until Reader ends.
try
{
_readThread.Join();
}
catch (ThreadStateException tsex)
{
throw new ProxyException("Thread state error while Reader thread ends.", tsex);
}
catch (ThreadInterruptedException tiex)
{
throw new ProxyException("Reader thread received interrupion call.", tiex);
}
catch (Exception ex)
{
throw new ProxyException("Error while Reader thread ends.", ex);
}
}
protected abstract void Connect();
protected abstract void Close();
protected abstract bool IsClientConnected { get; }
protected abstract void ClientWrite(byte[] data);
protected abstract int ClientRead(byte[] buffer, int offset, int length);
private void ClientWriteThread()
{
do
{
var data = ConsoleRead();
if (data != null)
{
ClientWrite(data);
}
else
{
_active = false;
}
} while (_active && IsClientConnected);
}
private void ClientReadThread()
{
while (_active && IsClientConnected)
{
try
{
var buffer = new byte[BufferSize];
var bytes = ClientRead(buffer, 0, buffer.Length);
if (bytes > 0)
{
var data = new byte[bytes];
Array.Copy(buffer, data, bytes);
ConsoleWrite(data);
}
}
catch (Exception ex)
{
throw new ProxyException("Exception while reading.", ex);
}
}
}
private static byte[] ConsoleRead()
{
var stdin = Console.OpenStandardInput();
var readBytes = new byte[4];
var read = stdin.Read(readBytes, 0, readBytes.Length);
if (read < readBytes.Length) return null;
var length = BitConverter.ToInt32(readBytes, 0);
var data = new byte[length];
read = stdin.Read(data, 0, data.Length);
if (read > 0)
{
if (read < data.Length)
{
var newData = new byte[read];
Array.Copy(data, newData, newData.Length);
return newData;
}
return data;
}
return null;
}
private void ConsoleWrite(byte[] data)
{
lock (_writeLock)
{
try
{
var stdout = Console.OpenStandardOutput();
var bytes = BitConverter.GetBytes(data.Length);
stdout.Write(bytes, 0, bytes.Length);
stdout.Write(data, 0, data.Length);
stdout.Flush();
}
catch (Exception ex)
{
throw new ProxyException("Exception while writing.", ex);
}
}
}
}
}