mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Send all cookies to the websocket connection as the clearance token is now needed
This commit is contained in:
@@ -53,15 +53,6 @@ public class ChatBot
|
|||||||
_kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!,
|
_kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!,
|
||||||
settings[BuiltIn.Keys.Proxy].Value, _cancellationToken);
|
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<int>()
|
|
||||||
});
|
|
||||||
|
|
||||||
if (_kfTokenService.GetXfSessionCookie() == null)
|
if (_kfTokenService.GetXfSessionCookie() == null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -75,6 +66,15 @@ public class ChatBot
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<int>()
|
||||||
|
});
|
||||||
|
|
||||||
_logger.Debug("Creating bot command instance");
|
_logger.Debug("Creating bot command instance");
|
||||||
_botCommands = new BotCommands(this, _cancellationToken);
|
_botCommands = new BotCommands(this, _cancellationToken);
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ public class ChatBot
|
|||||||
_kfTokenService.SaveCookies().Wait(_cancellationToken);
|
_kfTokenService.SaveCookies().Wait(_cancellationToken);
|
||||||
// Shouldn't be null if we've just refreshed the token
|
// 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
|
// 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.");
|
_logger.Info("Retrieved fresh token. Reconnecting.");
|
||||||
KfClient.Disconnect();
|
KfClient.Disconnect();
|
||||||
KfClient.StartWsClient().Wait(_cancellationToken);
|
KfClient.StartWsClient().Wait(_cancellationToken);
|
||||||
|
|||||||
@@ -166,6 +166,11 @@ public class KfTokenService
|
|||||||
return cookie?.Value;
|
return cookie?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, string> GetCookies()
|
||||||
|
{
|
||||||
|
return _cookies.GetAllCookies().ToDictionary(cookie => cookie.Name, cookie => cookie.Value);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task SaveCookies()
|
public async Task SaveCookies()
|
||||||
{
|
{
|
||||||
_logger.Debug("Saving cookies");
|
_logger.Debug("Saving cookies");
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ public class ChatClient
|
|||||||
_config = config;
|
_config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateToken(string newToken)
|
public void UpdateCookies(Dictionary<string, string> cookies)
|
||||||
{
|
{
|
||||||
_config.XfSessionToken = newToken;
|
_config.Cookies = cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StartWsClient()
|
public async Task StartWsClient()
|
||||||
@@ -80,13 +80,16 @@ public class ChatClient
|
|||||||
clientWs.Options.Proxy = new WebProxy(_config.Proxy);
|
clientWs.Options.Proxy = new WebProxy(_config.Proxy);
|
||||||
}
|
}
|
||||||
// Guest mode
|
// Guest mode
|
||||||
if (_config.XfSessionToken == null)
|
if (_config.Cookies.Keys.Count == 0)
|
||||||
{
|
{
|
||||||
return clientWs;
|
return clientWs;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cookieContainer = new CookieContainer();
|
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;
|
clientWs.Options.Cookies = cookieContainer;
|
||||||
|
|
||||||
return clientWs;
|
return clientWs;
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ namespace KfChatDotNetWsClient.Models;
|
|||||||
|
|
||||||
public class ChatClientConfigModel
|
public class ChatClientConfigModel
|
||||||
{
|
{
|
||||||
// XF session token. Sent as a cookie to auth the user
|
public required Dictionary<string, string> Cookies { get; set; } = new();
|
||||||
public string? XfSessionToken { get; set; }
|
|
||||||
// Currently wss://kiwifarms.net/chat.ws
|
// Currently wss://kiwifarms.net/chat.ws
|
||||||
public required Uri WsUri { get; set; }
|
public required Uri WsUri { get; set; }
|
||||||
public int ReconnectTimeout { get; set; } = 30;
|
public int ReconnectTimeout { get; set; } = 30;
|
||||||
|
|||||||
Reference in New Issue
Block a user