-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransmissionLine.cs
563 lines (490 loc) · 18.4 KB
/
TransmissionLine.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//Copyright 2019 Volodymyr Podshyvalov
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Threading;
using System.Security.Principal;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using PipesProvider.Networking;
using PipesProvider.Networking.Routing;
namespace PipesProvider.Client
{
/// <summary>
/// A class that provides an information about the line between client and server.
/// Provides an API for simplifying transmission management.
/// Provides services for automatic handling of the client tasks.
/// </summary>
public class TransmissionLine
{
#region Enums
/// <summary>
/// Difines direction of transmission.
/// </summary>
public enum TransmissionDirection
{
/// <summary>
/// A transmission will receives messages from sender.
/// </summary>
In,
/// <summary>
/// A transmission will emmites messages.
/// </summary>
Out
}
#endregion
#region Public properties
/// <summary>
/// Unique GUID for this pipe.
/// </summary>
public string GUID
{
get
{
// Generate GUID if not found.
if (string.IsNullOrEmpty(_GUID))
{
_GUID = GenerateGUID(ServerName, ServerPipeName);
}
return _GUID;
}
}
/// <summary>
/// Buffer that contains GUID value.
/// </summary>
protected string _GUID = null;
/// <summary>
/// Name of server pipe that will be using for transmission via current processor.
/// </summary>
public string ServerPipeName
{
get;
protected set;
}
/// <summary>
/// Name of server pipe that will be using for transmission via current processor.
/// </summary>
public string ServerName
{
get;
protected set;
}
/// <summary>
/// If true then this line will be closed on the next client tick.
/// </summary>
public bool Closed
{
get;
protected set;
}
/// <summary>
/// True if async operation started and not finished.
/// </summary>
public bool Processing { get; set; }
/// <summary>
/// Is current query processing is interrupted?
/// Will disconnect a current connection with an error.
/// </summary>
public bool Interrupted
{
get
{
return _Interrupted;
}
set
{
if (!Processing)
{
// If terrmination requested.
if (value)
{
// Log about error.
Console.WriteLine("LINE INTERRUPTION IMPOSSIBLE. Line not in processing. : " + ServerName + "/" + ServerPipeName);
}
_Interrupted = false;
return;
}
if(value)
{
// Log about closing
Console.WriteLine(ServerName + "/" + ServerPipeName + " : LINE INTERRUPTED : " +
(Processing && LastQuery.Data != null ? "Intrupted query: " + LastQuery.Data + "\n": "Has no query in processing."));
}
_Interrupted = value;
}
}
/// <summary>
/// Buffer that contains interruptoin state.
/// </summary>
protected bool _Interrupted;
/// <summary>
/// Return the query that was dequeue at last.
/// </summary>
public QueryContainer LastQuery
{
get
{
return lastQuery.IsEmpty ? QueryContainer.Empty : lastQuery;
}
protected set
{
lastQuery = value;
}
}
/// <summary>
/// Token that will used to autorizing on the server.
/// </summary>
public SafeAccessTokenHandle accessToken;
/// <summary>
/// Contain logon config to remote machine access.
/// Contain RSA encryption keys data reklative to this line.
/// </summary>
public Instruction RoutingInstruction
{
get; protected set;
}
/// <summary>
/// Marker that show does logon already finished.
/// By default is true, cause default logon is anonymous.
/// </summary>
public bool LogonFinished
{ get; protected set; } = true;
/// <summary>
/// Define bihavior of the client loop.
///
/// In - will connect to target pipe as soon as possible.
/// Out - will wait for query in queue.
/// </summary>
public TransmissionDirection Direction { get; set; } = TransmissionDirection.Out;
/// <summary>
/// Ecription provider that would applied to that transmission.
/// </summary>
public Security.Encryption.Operators.IEncryptionOperator TransmissionEncryption { get; set; } =
new Security.Encryption.Operators.AESEncryptionOperator();
#endregion
#region Public fields
/// <summary>
/// A reference to the current oppened pipe.
/// </summary>
public NamedPipeClientStream pipeClient;
/// <summary>
/// This delegate will be called when a connection for query will be established.
/// </summary>
public Action<TransmissionLine> queryHandler;
#endregion
#region Protected fields
/// <summary>
/// Field that contain last dequeued query.
/// </summary>
protected QueryContainer lastQuery = QueryContainer.Empty;
/// <summary>
/// List of queries that will wait its order to access transmission via this line.
/// </summary>
protected Queue<QueryContainer> queries = new Queue<QueryContainer>();
#endregion
#region Constructors
/// <summary>
/// Create new instance of LineProcessor taht can be registread in static services.
/// Contain information about transmission between client and server.
/// </summary>
/// <param name="serverName">Name of server into the network. If local than place "."</param>
/// <param name="serverPipeName">Name of the pipe that will be used for transmitiong.</param>
/// <param name="queryProcessor">Delegate that will be called when connection will be established.</param>
/// <param name="token">Reference to token that provide authority to work with remote server.</param>
public TransmissionLine(string serverName, string serverPipeName, Action<TransmissionLine> queryProcessor, ref SafeAccessTokenHandle token)
{
// Set fields.
ServerName = serverName;
ServerPipeName = serverPipeName;
this.queryHandler = queryProcessor;
this.accessToken = token;
// Registrate at hashtable.
ClientAPI.TryToRegisterTransmissionLine(this);
}
/// <summary>
/// Create instance using routing instruction.
/// </summary>
/// <param name="instruction">Routing insturuction that contain all data about target srver.</param>
/// <param name="queryProcessor">Delegate that will be called when connection will be established.</param>
public TransmissionLine(ref Instruction instruction, Action<TransmissionLine> queryProcessor)
{
// Set fields.
RoutingInstruction = instruction;
ServerName = instruction.routingIP;
ServerPipeName = instruction.pipeName;
this.queryHandler = queryProcessor;
// Logon as requested.
TryLogonAs(instruction.logonConfig);
// Registrate at hashtable.
ClientAPI.TryToRegisterTransmissionLine(this);
}
#endregion
#region Operators
/// <summary>
/// Incremet of attempts count.
/// </summary>
/// <param name="line">Target line.</param>
/// <returns></returns>
public static TransmissionLine operator ++(TransmissionLine line)
{
line.lastQuery++;
return line;
}
#endregion
#region Queue API
/// <summary>
/// Enqueue query to order. Query will be posted to server as soon as will possible.
/// </summary>
/// <param name="query">Formated query.</param>
public TransmissionLine EnqueueQuery(UniformQueries.Query query)
{
queries.Enqueue(new QueryContainer(query, null));
return this;
}
/// <summary>
/// Enqueue query to order. Query will be posted to server as soon as will possible.
/// </summary>
/// <param name="query"></param>
public TransmissionLine EnqueueQuery(QueryContainer query)
{
queries.Enqueue(query);
return this;
}
/// <summary>
/// Try to get a new query in turn.
///
/// Will return false if query not found.
/// Will return false in case if LineProccessor has status InProgress.
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public bool TryDequeQuery(out QueryContainer query)
{
// If some query already started then reject operation.
if (Processing)
{
query = QueryContainer.Empty;
return false;
}
try
{
// Dequeue query
QueryContainer dequeuedQuery = queries.Dequeue();
// Buferize at last.
LastQuery = dequeuedQuery;
// Initialize answer.
query = dequeuedQuery;
// Mark processor as busy.
Processing = true;
Console.WriteLine("QUERY DEQUEUED: {0}", LastQuery);
// inform about success.
return true;
}
catch (Exception ex)
{
// Inform about error during request.
Console.WriteLine("LINE PROCESSOR ERROR (GUID: \"{3}\" ADDRESS: {0}/{1}): {2}",
ServerName, ServerPipeName, ex.Message, GUID);
// Drop relative data.
LastQuery = QueryContainer.Empty;
query = QueryContainer.Empty;
// Infor about failure.
return false;
}
}
/// <summary>
/// Return true if queue contain queries.
/// </summary>
public bool HasQueries
{ get { return queries.Count > 0; } }
/// <summary>
/// Inserts the query to start of queue.
/// </summary>
/// <param name="query">Query that will places on first place in queue.</param>
/// <param name="withInterruption">If true then will interupt cuerent query in processing and
/// add enqueue it to the second position. After that will enqueue all left queue's elements.</param>
public void InsertQuery(UniformQueries.Query query, bool withInterruption)
{
// Lock queue changing.
lock (queries)
{
// Add requested query on first place.
Queue<QueryContainer> queue = new Queue<QueryContainer>();
queue.Enqueue(new QueryContainer(query));
// Interuupt current if possible and required.
if (withInterruption)
{
// Does there is something computing at the time.
if (Processing)
{
// Buferrize unfinished query.
queue.Enqueue(LastQuery);
// Interrupting processing.
Interrupted = true;
}
}
// Enque all left queries from old queue.
while(queries.Count > 0)
{
queue.Enqueue(queries.Dequeue());
}
// Update queue.
queries = queue;
}
}
#endregion
#region Finilizing API
/// <summary>
/// Mark line as closed. Thread will be terminated on the next client tick.
/// </summary>
public void Close()
{
// Mark as closed.
Closed = true;
// Drop processing marker to allow loop drop waiting to async operrations.
Processing = false;
// Log about closing
Console.WriteLine("LINE CLOSED: " + ServerName + "/" + ServerPipeName);
// Remove from table.
ClientAPI.TryToUnregisterTransmissionLine(GUID);
}
/// <summary>
/// Drop meta data relative only per one session.
/// </summary>
public void DropMeta()
{
pipeClient = null;
Processing = false;
Interrupted = false;
}
#endregion
#region Remote machine LSA API
/// <summary>
/// Trying to logon using provided information.
/// In case failed - close line.
/// </summary>
/// <param name="logonMeta"></param>
/// <returns>Result of logon.</returns>
public bool TryLogonAs(Security.LogonConfig logonMeta)
{
// Disable prmition to start.
LogonFinished = false;
//Console.WriteLine("{0}/{1}: LOGON STARTED", ServerName, ServerPipeName);
// Try to logon using provided config.
bool logonResult = Security.General.TryToLogonAtRemoteDevice(
logonMeta,
out SafeAccessTokenHandle safeTokenHandle);
if (!logonResult)
{
// Log about error.
Console.WriteLine("Logon failed. Connection not possible.");
// Close line.
Close();
// inform about fail.
return false;
}
else
{
// Save token as actual.
accessToken = safeTokenHandle;
// Change marker.
LogonFinished = true;
// Log about success.
//Console.WriteLine("{0}/{1}: LOGON FINISHED {2}", ServerName, ServerPipeName, accessToken.GetHashCode());
// Inform about success.
return true;
}
}
#endregion
#region Routing instructions API
/// <summary>
/// Set routing instruction to line.
/// Provide access to auto messages encryption with control of keys expiring.
///
/// ATTENTION: Line will not change logon config or server data.
/// If you want get full sync with routing instruction then user relative constructor.
/// </summary>
/// <param name="instruction">Instruction that will ocntain valid RSA key.</param>
/// <returns></returns>
public TransmissionLine SetInstructionAsKey(ref Instruction instruction)
{
// Update data.
RoutingInstruction = instruction;
// Try to logon as requested to recive token.
//TryLogonAs(instruction.logonConfig);
return this;
}
#endregion
#region Static API
/// <summary>
/// Method that can be started as thread. Will start client loop.
/// </summary>
/// <param name="lineProcessor"></param>
public static void ThreadLoop(object lineProcessor)
{
// Drop if incorrect argument.
if (!(lineProcessor is TransmissionLine line))
{
Console.WriteLine("THREAD NOT STARTED. INVALID ARGUMENT.");
return;
}
// Change thread cuture.
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-us");
Console.WriteLine("TL THREAD STARTED: {0}", Thread.CurrentThread.Name);
// Wait until logon will finished.
while(!line.LogonFinished)
{
Thread.Sleep(5);
}
// Drop if logon was failed.
if(line.Closed)
{
return;
}
// Apply rights for connection.
WindowsIdentity.RunImpersonated(line.accessToken, () =>
{
// Start client loop.
ClientAPI.ClientLoop(
line,
PipeDirection.InOut,
PipeOptions.Asynchronous | PipeOptions.WriteThrough);
});
}
/// <summary>
/// Generate GUID of this transmission line relative to pipe params.
/// </summary>
/// <param name="serverName"></param>
/// <param name="pipeName"></param>
/// <returns></returns>
public static string GenerateGUID(string serverName, string pipeName)
{
if(string.IsNullOrEmpty(serverName))
{
Console.WriteLine("ERROR (TL GUID): Server name can't be null or empty.");
return null;
}
if (string.IsNullOrEmpty(pipeName))
{
Console.WriteLine("ERROR (TL GUID): Pipe name can't be null or empty.");
return null;
}
//return serverName.GetHashCode() + "_" + pipeName.GetHashCode();
return serverName + "." + pipeName;
}
#endregion
}
}