From e7c309582acd3e40e2fccf134e9b183ab3d678ca Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Sun, 8 Feb 2026 20:12:41 -0600 Subject: [PATCH] Send all cookies to the websocket connection as the clearance token is now needed --- KfChatDotNetBot/ChatBot.cs | 20 +++++++++---------- KfChatDotNetBot/Services/KfTokenService.cs | 5 +++++ KfChatDotNetWsClient/ChatClient.cs | 11 ++++++---- .../Models/ChatClientConfigModel.cs | 3 +-- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/KfChatDotNetBot/ChatBot.cs b/KfChatDotNetBot/ChatBot.cs index f79c553..52efcdc 100644 --- a/KfChatDotNetBot/ChatBot.cs +++ b/KfChatDotNetBot/ChatBot.cs @@ -53,15 +53,6 @@ public class ChatBot _kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!, settings[BuiltIn.Keys.Proxy].Value, _cancellationToken); - KfClient = new ChatClient(new ChatClientConfigModel - { - WsUri = new Uri(settings[BuiltIn.Keys.KiwiFarmsWsEndpoint].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsWsEndpoint} cannot be null")), - XfSessionToken = _kfTokenService.GetXfSessionCookie(), - CookieDomain = settings[BuiltIn.Keys.KiwiFarmsDomain].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsDomain} cannot be null"), - Proxy = settings[BuiltIn.Keys.Proxy].Value, - ReconnectTimeout = settings[BuiltIn.Keys.KiwiFarmsWsReconnectTimeout].ToType() - }); - if (_kfTokenService.GetXfSessionCookie() == null) { try @@ -74,6 +65,15 @@ public class ChatBot _logger.Error(e); } } + + KfClient = new ChatClient(new ChatClientConfigModel + { + WsUri = new Uri(settings[BuiltIn.Keys.KiwiFarmsWsEndpoint].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsWsEndpoint} cannot be null")), + Cookies = _kfTokenService.GetCookies(), + CookieDomain = settings[BuiltIn.Keys.KiwiFarmsDomain].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsDomain} cannot be null"), + Proxy = settings[BuiltIn.Keys.Proxy].Value, + ReconnectTimeout = settings[BuiltIn.Keys.KiwiFarmsWsReconnectTimeout].ToType() + }); _logger.Debug("Creating bot command instance"); _botCommands = new BotCommands(this, _cancellationToken); @@ -121,7 +121,7 @@ public class ChatBot _kfTokenService.SaveCookies().Wait(_cancellationToken); // Shouldn't be null if we've just refreshed the token // It's only null if a logon has never been attempted since the cookie DB entry was created - KfClient.UpdateToken(_kfTokenService.GetXfSessionCookie()!); + KfClient.UpdateCookies(_kfTokenService.GetCookies()); _logger.Info("Retrieved fresh token. Reconnecting."); KfClient.Disconnect(); KfClient.StartWsClient().Wait(_cancellationToken); diff --git a/KfChatDotNetBot/Services/KfTokenService.cs b/KfChatDotNetBot/Services/KfTokenService.cs index 41f9e2b..739ae4f 100644 --- a/KfChatDotNetBot/Services/KfTokenService.cs +++ b/KfChatDotNetBot/Services/KfTokenService.cs @@ -166,6 +166,11 @@ public class KfTokenService return cookie?.Value; } + public Dictionary GetCookies() + { + return _cookies.GetAllCookies().ToDictionary(cookie => cookie.Name, cookie => cookie.Value); + } + public async Task SaveCookies() { _logger.Debug("Saving cookies"); diff --git a/KfChatDotNetWsClient/ChatClient.cs b/KfChatDotNetWsClient/ChatClient.cs index a11302e..30ec421 100644 --- a/KfChatDotNetWsClient/ChatClient.cs +++ b/KfChatDotNetWsClient/ChatClient.cs @@ -39,9 +39,9 @@ public class ChatClient _config = config; } - public void UpdateToken(string newToken) + public void UpdateCookies(Dictionary cookies) { - _config.XfSessionToken = newToken; + _config.Cookies = cookies; } public async Task StartWsClient() @@ -80,13 +80,16 @@ public class ChatClient clientWs.Options.Proxy = new WebProxy(_config.Proxy); } // Guest mode - if (_config.XfSessionToken == null) + if (_config.Cookies.Keys.Count == 0) { return clientWs; } var cookieContainer = new CookieContainer(); - cookieContainer.Add(new Cookie("xf_session", _config.XfSessionToken, "/", _config.CookieDomain)); + foreach (var key in _config.Cookies.Keys) + { + cookieContainer.Add(new Cookie(key, _config.Cookies[key], "/", _config.CookieDomain)); + } clientWs.Options.Cookies = cookieContainer; return clientWs; diff --git a/KfChatDotNetWsClient/Models/ChatClientConfigModel.cs b/KfChatDotNetWsClient/Models/ChatClientConfigModel.cs index 965f9fb..132f137 100644 --- a/KfChatDotNetWsClient/Models/ChatClientConfigModel.cs +++ b/KfChatDotNetWsClient/Models/ChatClientConfigModel.cs @@ -2,8 +2,7 @@ namespace KfChatDotNetWsClient.Models; public class ChatClientConfigModel { - // XF session token. Sent as a cookie to auth the user - public string? XfSessionToken { get; set; } + public required Dictionary Cookies { get; set; } = new(); // Currently wss://kiwifarms.net/chat.ws public required Uri WsUri { get; set; } public int ReconnectTimeout { get; set; } = 30;