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;
|
|
|
|
using TwitchChatTTS.OBS.Socket.Context;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class HelloHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-16 20:19:31 -04:00
|
|
|
private ILogger _logger { get; }
|
2024-03-12 14:05:27 -04:00
|
|
|
public int OperationCode { get; set; } = 0;
|
2024-06-16 20:19:31 -04:00
|
|
|
private HelloContext _context { get; }
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public HelloHandler(ILogger logger, HelloContext context)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_context = context;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not HelloMessage obj || obj == null)
|
|
|
|
return;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Verbose("OBS websocket password: " + _context.Password);
|
|
|
|
if (obj.Authentication == null || string.IsNullOrWhiteSpace(_context.Password))
|
|
|
|
{
|
|
|
|
await sender.Send(1, new IdentifyMessage(obj.RpcVersion, string.Empty, 1023 | 262144));
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var salt = obj.Authentication.Salt;
|
|
|
|
var challenge = obj.Authentication.Challenge;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Verbose("Salt: " + salt);
|
|
|
|
_logger.Verbose("Challenge: " + challenge);
|
|
|
|
|
|
|
|
string secret = _context.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-03-15 08:27:35 -04:00
|
|
|
await sender.Send(1, new IdentifyMessage(obj.RpcVersion, hash, 1023 | 262144));
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|