add account rotating on 401 and 429
This commit is contained in:
parent
93d3cae034
commit
aefed18612
1 changed files with 27 additions and 8 deletions
|
|
@ -24,9 +24,9 @@ var apiAccounts = new List<HttpClientCookieContainer>();
|
||||||
// Push the first account to the end of the list
|
// Push the first account to the end of the list
|
||||||
void RotateAccount()
|
void RotateAccount()
|
||||||
{
|
{
|
||||||
var account = Config.Instance.Accounts.First();
|
var account = apiAccounts.First();
|
||||||
Config.Instance.Accounts.Remove(account);
|
apiAccounts.Remove(account);
|
||||||
Config.Instance.Accounts.Add(account);
|
apiAccounts.Add(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -172,12 +172,9 @@ app.Use(async (context, next) =>
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Proxy all requests starting with /api/1 to the VRChat API
|
async Task DoRequest(HttpContext context, bool retriedAlready = false)
|
||||||
app.Use(async (context, next) =>
|
|
||||||
{
|
{
|
||||||
if (context.Request.Path.StartsWithSegments("/api/1"))
|
if (apiAccounts.Count == 0)
|
||||||
{
|
|
||||||
if (apiAccounts.Count == 0)
|
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = 500;
|
context.Response.StatusCode = 500;
|
||||||
await context.Response.WriteAsync("No accounts available");
|
await context.Response.WriteAsync("No accounts available");
|
||||||
|
|
@ -229,6 +226,20 @@ app.Use(async (context, next) =>
|
||||||
// Send the request
|
// Send the request
|
||||||
var response = await account.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
|
var response = await account.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
|
||||||
|
|
||||||
|
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.TooManyRequests)
|
||||||
|
{
|
||||||
|
if (retriedAlready)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 500;
|
||||||
|
Console.Error.WriteLine($"Failed to authenticate: {response.StatusCode}");
|
||||||
|
await context.Response.WriteAsync("Failed to authenticate");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Console.Error.WriteLine($"Retrying request due to {response.StatusCode}");
|
||||||
|
RotateAccount();
|
||||||
|
await DoRequest(context, true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Copy response status code and headers
|
// Copy response status code and headers
|
||||||
context.Response.StatusCode = (int)response.StatusCode;
|
context.Response.StatusCode = (int)response.StatusCode;
|
||||||
foreach (var header in response.Headers)
|
foreach (var header in response.Headers)
|
||||||
|
|
@ -249,6 +260,14 @@ app.Use(async (context, next) =>
|
||||||
memoryStream.Position = 0;
|
memoryStream.Position = 0;
|
||||||
await memoryStream.CopyToAsync(context.Response.Body);
|
await memoryStream.CopyToAsync(context.Response.Body);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Proxy all requests starting with /api/1 to the VRChat API
|
||||||
|
app.Use(async (context, next) =>
|
||||||
|
{
|
||||||
|
if (context.Request.Path.StartsWithSegments("/api/1"))
|
||||||
|
{
|
||||||
|
await DoRequest(context);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue