-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathService.cs
More file actions
574 lines (508 loc) · 23.4 KB
/
Service.cs
File metadata and controls
574 lines (508 loc) · 23.4 KB
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
564
565
566
567
568
569
570
571
572
573
574
/* Copyright (C) 2014-2016, Manuel Meitinger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using RDPCOMAPILib;
namespace Aufbauwerk.Tools.KioskControl
{
[DataContract]
public class Connection
{
[DataMember]
public string DomainName { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string MachineName { get; set; }
[DataMember]
public string ConnectionString { get; set; }
[DataMember]
public string AttendeeName { get; set; }
}
[ServiceContract]
public interface IContract
{
[WebGet(UriTemplate = "/")]
[OperationContract]
Stream HtmlViewer();
[WebGet(UriTemplate = "/View")]
[OperationContract]
Stream View();
[WebGet(UriTemplate = "/Interact")]
[OperationContract]
Stream Interact();
[WebGet(UriTemplate = "/Connect", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Connection Connect();
[WebGet(UriTemplate = "/ConnectToClient?ConnectionString={connectionString}", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
void ConnectToClient(string connectionString);
[WebGet(UriTemplate = "/CreateVirtualChannel?ChannelName={channelName}", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
void CreateVirtualChannel(string channelName);
}
public class CorsSupportAttribute : Attribute, IServiceBehavior
{
class Inspector : IDispatchMessageInspector
{
public readonly static Inspector Instance = new Inspector();
Inspector() { }
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
return request.Properties["httpRequest"];
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
var origin = ((HttpRequestMessageProperty)correlationState).Headers["Origin"];
var headers = ((HttpResponseMessageProperty)reply.Properties["httpResponse"]).Headers;
headers["Access-Control-Allow-Origin"] = origin ?? "*";
headers["Access-Control-Allow-Credentials"] = "true";
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(Inspector.Instance);
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[CorsSupport]
public class Service : IContract
{
class UserData
{
class PendingConnection
{
public Timer TimeoutTimer { get; set; }
public CTRL_LEVEL InitialControlLevel { get; set; }
}
readonly static object dataLock = new object();
readonly static Dictionary<string, UserData> byConnectionString = new Dictionary<string, UserData>();
readonly static Dictionary<SecurityIdentifier, UserData> bySid = new Dictionary<SecurityIdentifier, UserData>();
readonly static HashSet<string> issuedAttendeeNames = new HashSet<string>();
readonly static RandomNumberGenerator rng = RandomNumberGenerator.Create();
public static UserData FromAttendee(IRDPSRAPIAttendee attendee)
{
if (attendee == null)
throw new ArgumentNullException("attendee");
// get the attendee's invitation
var invitation = attendee.Invitation;
try
{
// find the user data that corresponds to the given connection string
UserData data;
lock (dataLock)
if (!byConnectionString.TryGetValue(invitation.ConnectionString, out data))
return null;
// remove a pending connection or ensure the attendee is connected
PendingConnection pendingConnection;
lock (data.connectionLock)
{
if (!data.pendingConnections.TryGetValue(attendee.RemoteName, out pendingConnection))
return data.connectedAttendeeIds.Contains(attendee.Id) ? data : null;
data.pendingConnections.Remove(attendee.RemoteName);
data.connectedAttendeeIds.Add(attendee.Id);
}
pendingConnection.TimeoutTimer.Dispose();
// set the initial control level
if (pendingConnection.InitialControlLevel != CTRL_LEVEL.CTRL_LEVEL_INVALID)
attendee.ControlLevel = pendingConnection.InitialControlLevel;
// enable all virtual channels
IRDPSRAPIVirtualChannel[] channels;
lock (data.grantedVirtualChannels)
{
channels = new IRDPSRAPIVirtualChannel[data.grantedVirtualChannels.Values.Count];
data.grantedVirtualChannels.Values.CopyTo(channels, 0);
}
foreach (var channel in channels)
{
try { channel.SetAccess(attendee.Id, CHANNEL_ACCESS_ENUM.CHANNEL_ACCESS_ENUM_SENDRECEIVE); }
catch { }
}
// return the data
return data;
}
finally { Marshal.ReleaseComObject(invitation); }
}
public static UserData FromContext(RDPSession session)
{
if (session == null)
throw new ArgumentNullException("session");
// get the current identity and effective permissions
var identity = OperationContext.Current.ServiceSecurityContext.WindowsIdentity;
var effectivePermissions = Security.GetEffectivePermissions(identity.Token);
var sid = identity.User;
lock (dataLock)
{
// try to get an existing user data object
UserData data;
if (!bySid.TryGetValue(sid, out data))
{
// create the invitation and user data
var inviationManager = session.Invitations;
try
{
var invitation = inviationManager.CreateInvitation(null, effectivePermissions.ToString(), "", 0);
try { data = new UserData(invitation.ConnectionString, effectivePermissions); }
finally { Marshal.ReleaseComObject(invitation); }
}
finally { Marshal.ReleaseComObject(inviationManager); }
// add it to the dictionaries
byConnectionString.Add(data.ConnectionString, data);
bySid.Add(sid, data);
}
else
// update the user's effective permissions
data.EffectivePermissions = effectivePermissions;
return data;
}
}
readonly object connectionLock = new object();
readonly Dictionary<string, PendingConnection> pendingConnections = new Dictionary<string, PendingConnection>();
readonly HashSet<int> connectedAttendeeIds = new HashSet<int>();
readonly Dictionary<string, IRDPSRAPIVirtualChannel> grantedVirtualChannels = new Dictionary<string, IRDPSRAPIVirtualChannel>();
UserData(string connectionString, SessionRights effectivePermissions)
{
ConnectionString = connectionString;
EffectivePermissions = effectivePermissions;
}
void ConnectionTimeout(object attendeeName)
{
// remove the pending connection if it still exists
PendingConnection pendingConnection;
lock (connectionLock)
{
if (!pendingConnections.TryGetValue((string)attendeeName, out pendingConnection))
return;
pendingConnections.Remove((string)attendeeName);
}
pendingConnection.TimeoutTimer.Dispose();
}
public string ConnectionString
{
get;
private set;
}
public SessionRights EffectivePermissions
{
get;
private set;
}
public void GrantChannelAccess(IRDPSRAPIVirtualChannel channel)
{
if (channel == null)
throw new ArgumentNullException("channel");
lock (grantedVirtualChannels)
{
// do nothing if already granted
if (grantedVirtualChannels.ContainsKey(channel.Name))
{
Marshal.ReleaseComObject(channel);
return;
}
// add the channel
grantedVirtualChannels.Add(channel.Name, channel);
}
// grant channel access to all established connections (best effort)
int[] attendeeIds;
lock (connectionLock)
{
attendeeIds = new int[connectedAttendeeIds.Count];
connectedAttendeeIds.CopyTo(attendeeIds, 0);
}
foreach (var attendeeId in attendeeIds)
{
try { channel.SetAccess(attendeeId, CHANNEL_ACCESS_ENUM.CHANNEL_ACCESS_ENUM_SENDRECEIVE); }
catch { }
}
}
public string CreateAttendee(RDPSession session, CTRL_LEVEL initialControlLevel = CTRL_LEVEL.CTRL_LEVEL_INVALID)
{
if (session == null)
throw new ArgumentNullException("session");
// create a strong random hex string including the user name
var userName = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
var buffer = new byte[64];
var builder = new StringBuilder(userName.Length + buffer.Length * 2 + 2);
TryAgain:
rng.GetBytes(buffer);
builder.Length = 0;
if (userName.IndexOf('\\') == -1)
builder.Append('\\');
builder.Append(userName);
builder.Append('\\');
for (int i = 0; i < buffer.Length; i++)
builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture));
var attendeeName = builder.ToString();
// make sure it doesn't exist
lock (issuedAttendeeNames)
{
if (issuedAttendeeNames.Contains(attendeeName))
goto TryAgain;
issuedAttendeeNames.Add(attendeeName);
}
// create a new pending connection and return the attendee name
lock (connectionLock)
{
pendingConnections.Add(attendeeName, new PendingConnection()
{
TimeoutTimer = new Timer(ConnectionTimeout, attendeeName, Properties.Settings.Default.ConnectionTimeout, TimeSpan.Zero),
InitialControlLevel = initialControlLevel,
});
}
return attendeeName;
}
public bool DestroyAttendee(IRDPSRAPIAttendee attendee)
{
if (attendee == null)
throw new ArgumentNullException("attendee");
// either remove a pending or established connction
PendingConnection pendingConnection;
lock (connectionLock)
{
if (!pendingConnections.TryGetValue(attendee.RemoteName, out pendingConnection))
return connectedAttendeeIds.Remove(attendee.Id);
pendingConnections.Remove(attendee.RemoteName);
}
pendingConnection.TimeoutTimer.Dispose();
return true;
}
}
readonly RDPSession session;
public Service(RDPSession session)
{
if (session == null)
throw new ArgumentNullException("session");
// store the session
this.session = session;
// hook the session events
session.OnAttendeeConnected += OnAttendeeConnected;
session.OnControlLevelChangeRequest += OnControlLevelChangeRequest;
session.OnAttendeeDisconnected += OnAttendeeDisconnected;
}
void OnAttendeeConnected(object pAttendee)
{
var attendee = (IRDPSRAPIAttendee)pAttendee;
try
{
// get the user data
var data = UserData.FromAttendee(attendee);
// terminate connections of unknown attendees
if (data == null)
attendee.TerminateConnection();
}
finally { Marshal.ReleaseComObject(attendee); }
}
void OnControlLevelChangeRequest(object pAttendee, CTRL_LEVEL RequestedLevel)
{
var attendee = (IRDPSRAPIAttendee)pAttendee;
try
{
// get the user data and check the known control levels
var data = UserData.FromAttendee(attendee);
if (data != null)
{
switch (RequestedLevel)
{
case CTRL_LEVEL.CTRL_LEVEL_NONE:
if ((data.EffectivePermissions & SessionRights.Connect) == 0)
return;
break;
case CTRL_LEVEL.CTRL_LEVEL_VIEW:
if ((data.EffectivePermissions & SessionRights.View) == 0)
return;
break;
case CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE:
if ((data.EffectivePermissions & SessionRights.Interact) == 0)
return;
break;
default:
return;
}
// set the requested control level
attendee.ControlLevel = RequestedLevel;
}
}
finally { Marshal.ReleaseComObject(attendee); }
}
void OnAttendeeDisconnected(object pDisconnectInfo)
{
var info = (IRDPSRAPIAttendeeDisconnectInfo)pDisconnectInfo;
try
{
// get the attendee
var attendee = info.Attendee;
try
{
// get the user data and destroy the current attendee
var data = UserData.FromAttendee(attendee);
if (data != null)
data.DestroyAttendee(attendee);
}
finally { Marshal.ReleaseComObject(attendee); }
}
finally { Marshal.ReleaseComObject(info); }
}
string EscapeString(string s)
{
// convert the string to UTF8 and percent-escape all bytes
var buffer = Encoding.UTF8.GetBytes(s);
var builder = new StringBuilder(buffer.Length * 3);
for (int i = 0; i < buffer.Length; i++)
{
builder.Append('%');
builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture));
}
return builder.ToString();
}
void DisableCaching()
{
// restrict caching in the response headers
WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.CacheControl] = "max-age=0, no-cache, no-store";
WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.Pragma] = "no-cache";
}
void DemandPermission(SessionRights rights)
{
// throw an access denied permission if the rights aren't granted
if (!Security.HasPermission(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Token, rights))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
throw new UnauthorizedAccessException();
}
}
Stream IContract.View()
{
// ensure view permissions and return the viewer
DemandPermission(SessionRights.View);
return HtmlViewer(CTRL_LEVEL.CTRL_LEVEL_VIEW);
}
Stream IContract.Interact()
{
// ensure interact permissions and return the viewer
DemandPermission(SessionRights.Interact);
return HtmlViewer(CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE);
}
public Stream HtmlViewer()
{
// get the highest control level and return the viewer
var permission = Security.GetEffectivePermissions(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Token);
return HtmlViewer
(
(permission & SessionRights.Interact) != 0 ? CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE :
(permission & SessionRights.View) != 0 ? CTRL_LEVEL.CTRL_LEVEL_VIEW :
(permission & SessionRights.Connect) != 0 ? CTRL_LEVEL.CTRL_LEVEL_NONE :
CTRL_LEVEL.CTRL_LEVEL_INVALID
);
}
public Stream HtmlViewer(CTRL_LEVEL controlLevel)
{
// ensure connect rights and return the viewer
DemandPermission(SessionRights.Connect);
DisableCaching();
var data = UserData.FromContext(session);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=UTF-8";
return new MemoryStream
(
Encoding.UTF8.GetBytes
(
string.Format
(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" +
"<html>\n" +
" <head>\n" +
" <title>{0}\\{1} @ {2}</title>\n" +
" <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>\n" +
" </head>\n" +
" <body onload=\"try{{RDPViewer.DisconnectedText=decodeURIComponent('{5}');RDPViewer.Connect(decodeURIComponent('{3}'),decodeURIComponent('{4}'),'');}}catch(e){{alert(e.message);}}\" style=\"margin:0;padding:0;\">\n" +
" <object id=\"RDPViewer\" classid=\"clsid:32be5ed2-5c86-480f-a914-0ff8885a1b3f\" width=\"100%\" height=\"100%\" style=\"border:0;margin:0;padding:0;\"></object>\n" +
" </body>\n" +
"</html>",
Environment.UserDomainName,
Environment.UserName,
Environment.MachineName,
EscapeString(data.ConnectionString),
EscapeString(data.CreateAttendee(session, controlLevel)),
EscapeString(Properties.Settings.Default.DisconnectedText)
)
)
);
}
public Connection Connect()
{
// check the permission and return the connection
DemandPermission(SessionRights.Connect);
DisableCaching();
var data = UserData.FromContext(session);
return new Connection()
{
DomainName = Environment.UserDomainName,
UserName = Environment.UserName,
MachineName = Environment.MachineName,
ConnectionString = data.ConnectionString,
AttendeeName = data.CreateAttendee(session),
};
}
public void ConnectToClient(string connectionString)
{
if (connectionString == null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
throw new ArgumentNullException("connectionString");
}
// check the permission and connect to the client
DemandPermission(SessionRights.ConnectToClient);
session.ConnectToClient(connectionString);
}
public void CreateVirtualChannel(string channelName)
{
if (channelName == null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
throw new ArgumentNullException("connectionString");
}
// check the permission and get the channel manager
DemandPermission(SessionRights.CreateVirtualChannel);
var manager = session.VirtualChannelManager;
try
{
// get or create the channel
IRDPSRAPIVirtualChannel channel;
try { channel = manager[channelName]; }
catch { channel = manager.CreateVirtualChannel(channelName, CHANNEL_PRIORITY.CHANNEL_PRIORITY_MED, 0); }
// grant access to the client
UserData.FromContext(session).GrantChannelAccess(channel);
}
finally { Marshal.ReleaseComObject(manager); }
}
}
}