-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketClient.cs
516 lines (445 loc) · 19.4 KB
/
WebSocketClient.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
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using SuchByte.MacroDeck.Logging;
using dichternebel.YaSB.StreamerBot;
namespace dichternebel.YaSB
{
public class WebSocketClient
{
private ClientWebSocket _webSocket;
private string _serverUrl;
private readonly CancellationTokenSource _cts;
private string AuthenticationMessage { get; set; }
public bool IsAuthenticated { get; private set; }
public static WebSocketClient Instance { get; private set; }
public WebSocketClient(string serverUrl)
{
Instance ??= this;
_serverUrl = serverUrl;
_webSocket = new ClientWebSocket();
_cts = new CancellationTokenSource();
}
public async Task StartConnectionAsync()
{
await Task.Run(async () =>
{
while (!_cts.IsCancellationRequested)
{
try
{
await ReceiveMessageAsync();
if (_webSocket.State != WebSocketState.Open)
{
IsAuthenticated = false;
Main.Model.IsConnectedToStreamerBot = false;
await ConnectToServerAsync();
}
}
catch (Exception ex)
{
// Macro Deck is terminating the plugin when an error is logged so we are writing warnings for connection losses
if (ex.Source == "System.Text.Json" || ex.Source == "System.Net.WebSockets.Client")
{
MacroDeckLogger.Warning(Main.Instance, ex.Message);
}
else
{
MacroDeckLogger.Error(Main.Instance, ex.Message);
}
Main.Model.IsConnectedToStreamerBot = false;
await Task.Delay(5000); // Wait 5 seconds before trying to reconnect
}
}
});
}
private async Task ConnectToServerAsync()
{
if (_webSocket.State != WebSocketState.None)
{
_webSocket.Dispose();
_webSocket = new ClientWebSocket();
}
MacroDeckLogger.Info(Main.Instance, "Connecting to WebSocket server...");
await _webSocket.ConnectAsync(new Uri(_serverUrl), _cts.Token);
}
public async Task GetEventsAsync()
{
try
{
var request = new
{
request = RequestType.GetEvents.ToString(),
id = "dichternebel-yasb-get-events",
authentication = IsAuthenticated ? AuthenticationMessage : null
};
string payload = JsonSerializer.Serialize(request);
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, "GetEvents request sent successfully.");
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance, $"Failed to get events from WebSocket server: {ex.Message}");
}
}
public async Task GetActionsAsync()
{
try
{
var request = new
{
request = RequestType.GetActions.ToString(),
id = "dichternebel-yasb-get-actions",
authentication = IsAuthenticated ? AuthenticationMessage : null
};
string payload = JsonSerializer.Serialize(request);
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, "GetActions request sent successfully.");
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance, $"Failed to get actions from WebSocket server: {ex.Message}");
}
}
public async Task UnSubscribeFromServerAsync(string eventIdentifier)
{
try
{
var eventsDictionary = Helper.ExplodeEventKey(eventIdentifier);
if (eventsDictionary == null || !eventsDictionary.Any()) return;
var request = new
{
request = RequestType.UnSubscribe.ToString(),
id = "dichternebel-yasb-unsubscribe",
events = eventsDictionary,
authentication = IsAuthenticated ? AuthenticationMessage : null
};
string payload = JsonSerializer.Serialize(request);
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, $"Unsubscription request sent successfully:\n{payload}");
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance, $"Failed to unsubscribe from WebSocket server: {ex.Message}");
}
}
public async Task SubscribeToServerAsync()
{
try
{
var eventsDictionary = Main.Model?.EventSubscriptionDictionary;
if (eventsDictionary == null || !eventsDictionary.Any()) return;
var request = new
{
request = RequestType.Subscribe.ToString(),
id = "dichternebel-yasb-subscribe",
events = eventsDictionary,
authentication = IsAuthenticated ? AuthenticationMessage : null
};
string payload = JsonSerializer.Serialize(request);
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, $"Subscription request sent successfully:\n{payload}");
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance, $"Failed to subscribe to WebSocket server: {ex.Message}");
}
}
private async Task GetGlobalsAsync(bool persisted)
{
var request = new
{
request = RequestType.GetGlobals.ToString(),
id = "dichternebel-yasb-get-globals",
persisted,
authentication = IsAuthenticated ? AuthenticationMessage : null
};
string payload = JsonSerializer.Serialize(request, new JsonSerializerOptions
{
WriteIndented = false
});
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, $"GetGlobals request sent successfully:\n{payload}");
}
public async Task GetGlobalsAsync()
{
try
{
var eventsDictionary = Main.Model?.EventSubscriptionDictionary;
if (eventsDictionary == null || !eventsDictionary.Any()) return;
if (eventsDictionary.TryGetValue("Misc", out string[] miscEvents))
{
if (!miscEvents.Contains("GlobalVariableUpdated")) return;
}
else
{
return;
}
await GetGlobalsAsync(true);
await GetGlobalsAsync(false);
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance, $"Failed to GetGlobals from WebSocket server: {ex.Message}");
}
}
private async Task ReceiveMessageAsync()
{
MacroDeckLogger.Info(Main.Instance, "Listening...");
var buffer = new byte[1024 * 4];
var messageBuilder = new StringBuilder();
while (_webSocket.State == WebSocketState.Open)
{
WebSocketReceiveResult result;
do
{
result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), _cts.Token);
var messageChunk = Encoding.UTF8.GetString(buffer, 0, result.Count);
messageBuilder.Append(messageChunk);
}
while (!result.EndOfMessage);
string completeMessage = messageBuilder.ToString();
// Parse JSON
var jsonDocument = JsonDocument.Parse(completeMessage);
var responseMessage = ParseJsonToMessage(jsonDocument);
Main.Model.StreamerBotMessage = responseMessage;
// Clear the StringBuilder for the next message
messageBuilder.Clear();
}
}
private ResponseMessage ParseJsonToMessage(JsonDocument jsonDocument)
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var prettyJson = JsonSerializer.Serialize(
jsonDocument,
new JsonSerializerOptions { WriteIndented = true }
);
//MacroDeckLogger.Trace(Main.Instance, $"Received message:\n{prettyJson}");
var root = jsonDocument.RootElement;
// Check if it's a "hello" response
if (root.TryGetProperty("info", out var infoElement))
{
var info = JsonSerializer.Deserialize<Info>(infoElement.GetRawText(), options);
if (root.TryGetProperty("authentication", out var authElement))
{
var auth = JsonSerializer.Deserialize<Authentication>(authElement.GetRawText(), options);
var salt = auth.Salt;
var challenge = auth.Challenge;
// Ensure proper decoding of Unicode escape sequences
auth.Salt = System.Net.WebUtility.HtmlDecode(salt);
auth.Challenge = System.Net.WebUtility.HtmlDecode(challenge);
return new ResponseMessage { ResponseType = ResponseType.Hello, Info = info, Authentication = auth };
}
return new ResponseMessage { ResponseType = ResponseType.Hello, Info = info };
}
// Check if it's an authentication response
if (root.TryGetProperty("id", out var authIdentifierElement))
{
var identifier = JsonSerializer.Deserialize<string>(authIdentifierElement.GetRawText());
if (identifier == "dichternebel-yasb-authentication")
{
if (root.TryGetProperty("status", out var statusElement))
{
var status = JsonSerializer.Deserialize<string>(statusElement.GetRawText());
IsAuthenticated = status == "ok";
}
else
{
IsAuthenticated = false;
}
return new ResponseMessage { ResponseType = ResponseType.Authentication };
}
}
// Check if it's a get action list response
if (root.TryGetProperty("actions", out var actionsElement))
{
if (root.TryGetProperty("id", out var identifierElement))
{
var identifier = JsonSerializer.Deserialize<string>(identifierElement.GetRawText());
if (identifier == "dichternebel-yasb-get-actions")
{
var actions = JsonSerializer.Deserialize<List<StreamerBot.Action>>(actionsElement.GetRawText(), options);
return new ResponseMessage { ResponseType = ResponseType.Actions, Actions = actions, RawMessage = prettyJson };
}
}
}
// Check if it's a get event list response
if (root.TryGetProperty("events", out var eventsElement))
{
if (root.TryGetProperty("id", out var identifierElement))
{
var identifier = JsonSerializer.Deserialize<string>(identifierElement.GetRawText());
if (identifier == "dichternebel-yasb-get-events")
{
var eventDictionary = JsonSerializer.Deserialize<Dictionary<string, string[]>>(eventsElement.GetRawText(), options);
return new ResponseMessage { ResponseType = ResponseType.Events, Events = eventDictionary, RawMessage = prettyJson };
}
}
}
// Check if it's a get globals response
if (root.TryGetProperty("variables", out var globalsElement))
{
if (root.TryGetProperty("id", out var identifierElement))
{
var identifier = JsonSerializer.Deserialize<string>(identifierElement.GetRawText());
if (identifier == "dichternebel-yasb-get-globals")
{
var globalDictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(globalsElement.GetRawText(), options);
var eventInfo = new StreamerBot.EventInfo();
eventInfo.Source = "Misc";
eventInfo.Type = "GlobalVariableUpdated";
var eventData = new EventData
{
KeyValuePairs = new Dictionary<string, object>()
};
foreach (var item in globalDictionary)
{
eventData.KeyValuePairs.Add(item.Key, item.Value);
}
return new ResponseMessage
{
ResponseType = ResponseType.Globals,
Event = new BotEvent
{
EventInfo = eventInfo,
Data = eventData
}
};
}
}
}
// Check if it's an event being fired
if (root.TryGetProperty("event", out var eventElement)
&& root.TryGetProperty("data", out var dataElement)
&& root.TryGetProperty("timeStamp", out var timeStampElement))
{
var timeStamp = JsonSerializer.Deserialize<DateTime>(timeStampElement.GetRawText(), options);
var eventInfo = JsonSerializer.Deserialize<EventInfo>(eventElement.GetRawText(), options);
var eventData = new EventData
{
KeyValuePairs = new Dictionary<string, object>()
};
foreach (var property in dataElement.EnumerateObject())
{
eventData.KeyValuePairs.Add(property.Name, property.Value);
}
return new ResponseMessage
{
ResponseType = ResponseType.Event,
Event = new BotEvent
{
TimeStamp = timeStamp,
EventInfo = eventInfo,
Data = eventData
},
RawMessage = prettyJson
};
}
return new ResponseMessage { ResponseType = ResponseType.Unknown, RawMessage = prettyJson };
}
public async Task SendMessageAsync(string actionId, string actionName, string actionArgument = "")
{
object args =new { };
try
{
args = string.IsNullOrEmpty(actionArgument) ? new { } : JsonSerializer.Deserialize<object>(actionArgument);
}
catch (Exception ex)
{
MacroDeckLogger.Warning(Main.Instance,$"Error serializing argument:\n{ex.Message}");
}
var request = new
{
request = RequestType.DoAction.ToString(),
id = "dichternebel-yasb-do-action",
action = new
{
id = actionId,
name = actionName
},
args
};
string payload = JsonSerializer.Serialize(request, new JsonSerializerOptions
{
WriteIndented = false
});
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, $"DoAction({actionName}) request sent successfully.");
}
public async Task SwitchHostAsync(string serverUrl)
{
_serverUrl = serverUrl;
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Switching host", _cts.Token);
}
public async Task AuthenticateAsync(string password, string salt, string challenge)
{
// Compute SHA-256 hash of password and salt
string secret = ComputeSha256Hash($"{password}{salt}");
// Compute SHA-256 hash of secret and challenge
AuthenticationMessage = ComputeSha256Hash($"{secret}{challenge}");
var request = new
{
request = RequestType.Authenticate.ToString(),
id = "dichternebel-yasb-authentication",
authentication = AuthenticationMessage
};
string payload = JsonSerializer.Serialize(request);
var bytes = Encoding.UTF8.GetBytes(payload);
await _webSocket.SendAsync(
new ArraySegment<byte>(bytes),
WebSocketMessageType.Text,
true,
_cts.Token);
MacroDeckLogger.Info(Main.Instance, "Authentication request sent successfully.");
}
private static string ComputeSha256Hash(string input)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
return Convert.ToBase64String(bytes);
}
}
public void Dispose()
{
_cts.Cancel();
_webSocket.Dispose();
Main.Model.IsConnectedToStreamerBot = false;
}
}
}