-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHidGuardianClient.cs
More file actions
141 lines (119 loc) · 5.1 KB
/
HidGuardianClient.cs
File metadata and controls
141 lines (119 loc) · 5.1 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp;
using Newtonsoft.Json;
namespace IMPluginInterface
{
public enum HidGuardianStatusCodes { None, Received, Accepted, Rejected, Processing, Processed, Error }
class HidGuardianClient: IDisposable
{
private uint clientPid;
private List<string> acceptedDevices = new List<string>();
private Dictionary<Guid, HidGuardianStatusCodes> requestStatuses = new Dictionary<Guid, HidGuardianStatusCodes>();
private WebSocket ws;
public HidGuardianClient(uint clientPid, string serverAddress = "ws://127.0.0.1:22408")
{
this.clientPid = clientPid;
this.ws = new WebSocket(serverAddress);
ws.OnMessage += Ws_OnMessage;
ws.Connect();
}
private void Ws_OnMessage(object sender, MessageEventArgs e)
{
if (e.IsText)
{
try
{
dynamic wsMessage = JsonConvert.DeserializeObject<dynamic>((string)e.Data);
if ((string)wsMessage.Header.Kind == "AccessRequest")
{
if (!requestStatuses.ContainsKey((Guid)wsMessage.Header.Id))
requestStatuses[(Guid)wsMessage.Header.Id] = HidGuardianStatusCodes.Received;
HidGuardianRequest wsRequest = JsonConvert.DeserializeObject<HidGuardianRequest>((string)e.Data);
if (wsRequest.ProcessId == clientPid)
if (wsRequest.HardwareIds.Intersect(acceptedDevices).Count() > 0)
{
requestStatuses[(Guid)wsMessage.Header.Id] = HidGuardianStatusCodes.Accepted;
var wsResponse = new HidGuardianRequestResponse(wsRequest.Header.Id, true);
ws.Send(JsonConvert.SerializeObject(wsResponse));
requestStatuses[(Guid)wsMessage.Header.Id] = HidGuardianStatusCodes.Processing;
}
else
{
requestStatuses[(Guid)wsMessage.Header.Id] = HidGuardianStatusCodes.Rejected;
var wsResponse = new HidGuardianRequestResponse(wsRequest.Header.Id, false);
ws.Send(JsonConvert.SerializeObject(wsResponse));
requestStatuses[(Guid)wsMessage.Header.Id] = HidGuardianStatusCodes.Processing;
}
}
else if ((string)wsMessage.Header.Kind == "Confirmation")
{
HidGuardianConfirmation wsConfirmation = JsonConvert.DeserializeObject<HidGuardianConfirmation>((string)e.Data);
if ((Guid)wsMessage.Header.Id != Guid.Empty && !requestStatuses.ContainsKey((Guid)wsMessage.Header.Id))
requestStatuses[(Guid)wsMessage.Header.Id] = wsConfirmation.Code == 200 ?
HidGuardianStatusCodes.Processed : HidGuardianStatusCodes.Error;
}
} catch
{
// Not a recignized or properly formatted request from HidGuardian server
}
}
}
public void WhitelistDevices(string[] hardwareIds)
{
acceptedDevices.AddRange(hardwareIds);
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
ws.Close();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
class Header
{
public Guid Id { get; internal set; }
public string Kind { get; internal set; }
}
class HidGuardianRequest
{
public Header Header { get; internal set; }
public List<string> HardwareIds { get; internal set; }
public string DeviceId { get; internal set; }
public string InstanceId { get; internal set; }
public int ProcessId { get; internal set; }
}
class HidGuardianRequestResponse
{
public Header Header { get; internal set; } = new Header();
public bool IsAllowed { get; internal set; }
public bool IsPermanent { get; internal set; }
internal HidGuardianRequestResponse(Guid RequestId, bool IsAllowed, bool IsPermanent = true)
{
this.Header.Id = RequestId;
this.IsAllowed = IsAllowed;
this.IsPermanent = IsPermanent;
}
}
class HidGuardianConfirmation
{
public Header Header { get; internal set; } = new Header();
public int Code { get; internal set; }
public string Message { get; internal set; }
}
}