Added groups & permissions. Fixed TTS user creation. Better connection handling. Fixed 7tv reconnection.

This commit is contained in:
Tom
2024-07-16 04:48:55 +00:00
parent 9fb966474f
commit e6b3819356
45 changed files with 947 additions and 567 deletions

View File

@@ -2,16 +2,19 @@ using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Serilog;
using TwitchChatTTS.OBS.Socket.Data;
using TwitchChatTTS.OBS.Socket.Manager;
namespace TwitchChatTTS.OBS.Socket.Handlers
{
public class EventMessageHandler : IWebSocketHandler
{
private readonly OBSManager _manager;
private readonly ILogger _logger;
public int OperationCode { get; } = 5;
public EventMessageHandler(ILogger logger)
public EventMessageHandler(OBSManager manager, ILogger logger)
{
_manager = manager;
_logger = logger;
}
@@ -23,28 +26,23 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
switch (message.EventType)
{
case "StreamStateChanged":
case "RecordStateChanged":
if (sender is not OBSSocketClient client)
return;
string? raw_state = message.EventData["outputState"].ToString();
string? state = raw_state?.Substring(21).ToLower();
client.Live = message.EventData["outputActive"].ToString() == "True";
_manager.Streaming = message.EventData["outputActive"].ToString().ToLower() == "true";
_logger.Warning("Stream " + (state != null && state.EndsWith("ing") ? "is " : "has ") + state + ".");
if (client.Live == false && state != null && !state.EndsWith("ing"))
if (_manager.Streaming == false && state != null && !state.EndsWith("ing"))
{
OnStreamEnd();
// Stream ended
}
break;
default:
_logger.Debug(message.EventType + " EVENT: " + string.Join(" | ", message.EventData?.Select(x => x.Key + "=" + x.Value?.ToString()) ?? new string[0]));
_logger.Debug(message.EventType + " EVENT: " + string.Join(" | ", message.EventData?.Select(x => x.Key + "=" + x.Value?.ToString()) ?? Array.Empty<string>()));
break;
}
}
private void OnStreamEnd()
{
}
}
}

View File

@@ -4,19 +4,18 @@ using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Serilog;
using TwitchChatTTS.OBS.Socket.Data;
using TwitchChatTTS.OBS.Socket.Context;
namespace TwitchChatTTS.OBS.Socket.Handlers
{
public class HelloHandler : IWebSocketHandler
{
private readonly HelloContext _context;
private readonly Configuration _configuration;
private readonly ILogger _logger;
public int OperationCode { get; } = 0;
public HelloHandler(HelloContext context, ILogger logger)
public HelloHandler(Configuration configuration, ILogger logger)
{
_context = context;
_configuration = configuration;
_logger = logger;
}
@@ -24,9 +23,10 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
{
if (data is not HelloMessage message || message == null)
return;
_logger.Verbose("OBS websocket password: " + _context.Password);
if (message.Authentication == null || string.IsNullOrWhiteSpace(_context.Password))
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))
{
await sender.Send(1, new IdentifyMessage(message.RpcVersion, string.Empty, 1023 | 262144));
return;
@@ -37,7 +37,7 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
_logger.Verbose("Salt: " + salt);
_logger.Verbose("Challenge: " + challenge);
string secret = _context.Password + salt;
string secret = password + salt;
byte[] bytes = Encoding.UTF8.GetBytes(secret);
string hash = null;
using (var sha = SHA256.Create())

View File

@@ -23,7 +23,7 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
if (data is not IdentifiedMessage message || message == null)
return;
sender.Connected = true;
_manager.Connected = true;
_logger.Information("Connected to OBS via rpc version " + message.NegotiatedRpcVersion + ".");
try
@@ -34,6 +34,8 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
{
_logger.Error(e, "Failed to load OBS group info upon OBS identification.");
}
await _manager.UpdateStreamingState();
}
}
}

View File

@@ -42,10 +42,7 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
switch (request.RequestType)
{
case "GetOutputStatus":
if (sender is not OBSSocketClient client)
return;
_logger.Debug($"Fetched stream's live status [live: {client.Live}][obs request id: {message.RequestId}]");
_logger.Debug($"Fetched stream's live status [live: {_manager.Streaming}][obs request id: {message.RequestId}]");
break;
case "GetSceneItemId":
{
@@ -227,6 +224,24 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
_logger.Debug($"Received response from OBS for sleeping [sleep: {sleepMillis}][obs request id: {message.RequestId}]");
break;
}
case "GetStreamStatus":
{
if (message.ResponseData == null)
{
_logger.Warning($"OBS Response is null [obs request id: {message.RequestId}]");
return;
}
if (!message.ResponseData.TryGetValue("outputActive", out object? outputActive) || outputActive == null)
{
_logger.Warning($"Failed to fetch the scene item visibility [obs request id: {message.RequestId}]");
return;
}
_manager.Streaming = outputActive?.ToString()!.ToLower() == "true";
requestData.ResponseValues = message.ResponseData;
_logger.Information($"OBS is currently {(_manager.Streaming ? "" : "not ")}streaming.");
break;
}
default:
_logger.Warning($"OBS Request Response not being processed [type: {request.RequestType}][{string.Join(Environment.NewLine, message.ResponseData?.Select(kvp => kvp.Key + " = " + kvp.Value?.ToString()) ?? [])}]");
break;