if rotating account does not work, re login

This commit is contained in:
Lillith Rose 2025-02-22 12:54:55 -05:00
parent aefed18612
commit 9a409bcab0

View file

@ -35,8 +35,10 @@ var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseWebSockets();
Config.Instance.Accounts.ForEach(async account =>
void LogInAllAccounts()
{
Config.Instance.Accounts.ForEach(async account =>
{
try
{
var cookieContainer = new CookieContainer();
@ -99,11 +101,14 @@ Config.Instance.Accounts.ForEach(async account =>
var curUser = await curUserResp.Content.ReadFromJsonAsync<User>();
Console.WriteLine($"Logged in as {curUser.displayName}");
apiAccounts.Add(httpClient);
} catch (HttpRequestException e)
}
catch (HttpRequestException e)
{
Console.WriteLine($"Failed to create API for {account.username}: {e.Message}, {e.StatusCode}, {e}");
}
});
});
}
LogInAllAccounts();
app.MapGet("/", () => $"Logged in with {apiAccounts.Count} accounts");
app.MapGet("/rotate", () =>
@ -172,7 +177,7 @@ app.Use(async (context, next) =>
}
});
async Task DoRequest(HttpContext context, bool retriedAlready = false)
async Task DoRequest(HttpContext context, bool retriedAlready = false, bool reAuthedAlready = false)
{
if (apiAccounts.Count == 0)
{
@ -230,9 +235,16 @@ async Task DoRequest(HttpContext context, bool retriedAlready = false)
{
if (retriedAlready)
{
if (reAuthedAlready)
{
Console.Error.WriteLine($"Failed to re-authenticate all accounts");
context.Response.StatusCode = 500;
Console.Error.WriteLine($"Failed to authenticate: {response.StatusCode}");
await context.Response.WriteAsync("Failed to authenticate");
await context.Response.WriteAsync("Failed to re-authenticate all accounts");
return;
}
// re-login all accounts and try again
LogInAllAccounts();
await DoRequest(context, true, true);
return;
}
Console.Error.WriteLine($"Retrying request due to {response.StatusCode}");