2024-03-12 14:05:27 -04:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Text;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket.Data;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class HelloHandler : IWebSocketHandler
|
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
private readonly Configuration _configuration;
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 0;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
public HelloHandler(Configuration configuration, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
_configuration = configuration;
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger = logger;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-12 14:05:27 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not HelloMessage message || message == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-07-16 00:48:55 -04:00
|
|
|
|
|
|
|
string? password = string.IsNullOrWhiteSpace(_configuration.Obs?.Password) ? null : _configuration.Obs.Password.Trim();
|
|
|
|
_logger.Verbose("OBS websocket password: " + password);
|
|
|
|
if (message.Authentication == null || string.IsNullOrWhiteSpace(password))
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
await sender.Send(1, new IdentifyMessage(message.RpcVersion, string.Empty, 1023 | 262144));
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
var salt = message.Authentication.Salt;
|
|
|
|
var challenge = message.Authentication.Challenge;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Verbose("Salt: " + salt);
|
|
|
|
_logger.Verbose("Challenge: " + challenge);
|
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
string secret = password + salt;
|
2024-03-12 14:05:27 -04:00
|
|
|
byte[] bytes = Encoding.UTF8.GetBytes(secret);
|
|
|
|
string hash = null;
|
2024-06-16 20:19:31 -04:00
|
|
|
using (var sha = SHA256.Create())
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
bytes = sha.ComputeHash(bytes);
|
|
|
|
hash = Convert.ToBase64String(bytes);
|
|
|
|
|
|
|
|
secret = hash + challenge;
|
|
|
|
bytes = Encoding.UTF8.GetBytes(secret);
|
|
|
|
bytes = sha.ComputeHash(bytes);
|
|
|
|
hash = Convert.ToBase64String(bytes);
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Verbose("Final hash: " + hash);
|
2024-06-24 18:11:36 -04:00
|
|
|
await sender.Send(1, new IdentifyMessage(message.RpcVersion, hash, 1023 | 262144));
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|