feat: An Initias Redis Store Implementation
- **docker-compose.yml** - Added Redis service configuration to support token storage. - Set up health checks and volume for Redis persistence. - Configured VRCAuthProxy service to depend on Redis. - **HttpClientCookieContainer.cs** - Added `Username` property to support user-specific token management. - **Program.cs** - Integrated Redis for storing and retrieving authentication tokens. - Updated login and token rotation logic to utilize Redis. - Improved async/await usage for better reliability. - **VRCAuthProxy.csproj** - Added `StackExchange.Redis` package for Redis connectivity. - Corrected `Otp.NET` package reference. - **API.cs** - Updated `TotpVerifyResponse` and `User` classes to be nullable-aware. - **RedisService.cs** - Implemented Redis service for managing authentication tokens. - Added methods for saving, retrieving, and deleting tokens.
This commit is contained in:
parent
eb4349031b
commit
30d631d246
6 changed files with 180 additions and 61 deletions
|
|
@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Builder;
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using OtpNet;
|
||||
using VRCAuthProxy;
|
||||
using VRCAuthProxy.Services;
|
||||
using VRCAuthProxy.types;
|
||||
using HttpMethod = System.Net.Http.HttpMethod;
|
||||
using User = VRCAuthProxy.types.User;
|
||||
|
|
@ -18,102 +19,128 @@ using User = VRCAuthProxy.types.User;
|
|||
string userAgent = "VRCAuthProxy V1.0.0 (https://github.com/PrideVRCommunity/VRCAuthProxy)";
|
||||
|
||||
var apiAccounts = new List<HttpClientCookieContainer>();
|
||||
|
||||
|
||||
var redisConnection = Environment.GetEnvironmentVariable("REDIS_CONNECTION") ?? "localhost:6379";
|
||||
var redisService = new RedisService(redisConnection);
|
||||
|
||||
// Push the first account to the end of the list
|
||||
void RotateAccount()
|
||||
async Task RotateAccount()
|
||||
{
|
||||
var account = apiAccounts.First();
|
||||
apiAccounts.Remove(account);
|
||||
apiAccounts.Add(account);
|
||||
|
||||
// Store updated cookies in Redis
|
||||
var cookies = account.CookieContainer.GetAllCookies().Cast<Cookie>()
|
||||
.ToDictionary(c => c.Name, c => c.Value);
|
||||
await redisService.SaveAuthToken(account.Username, cookies);
|
||||
}
|
||||
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var app = builder.Build();
|
||||
app.UseWebSockets();
|
||||
|
||||
void LogInAllAccounts()
|
||||
async Task LogInAllAccounts()
|
||||
{
|
||||
Config.Instance.Accounts.ForEach(async account =>
|
||||
foreach (var account in Config.Instance.Accounts)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cookieContainer = new CookieContainer();
|
||||
|
||||
// Try to restore cookies from Redis
|
||||
var storedCookies = await redisService.GetAuthToken(account.username);
|
||||
if (storedCookies != null)
|
||||
{
|
||||
foreach (var cookie in storedCookies)
|
||||
{
|
||||
cookieContainer.Add(new Uri("https://api.vrchat.cloud"), new Cookie(cookie.Key, cookie.Value));
|
||||
}
|
||||
}
|
||||
|
||||
var handler = new HttpClientHandler
|
||||
{
|
||||
CookieContainer = cookieContainer
|
||||
};
|
||||
var httpClient = new HttpClientCookieContainer(handler)
|
||||
{
|
||||
BaseAddress = new Uri("https://api.vrchat.cloud/api/1")
|
||||
|
||||
BaseAddress = new Uri("https://api.vrchat.cloud/api/1"),
|
||||
Username = account.username
|
||||
};
|
||||
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
|
||||
|
||||
Console.WriteLine($"Creating API for {account.username}");
|
||||
|
||||
string encodedUsername = HttpUtility.UrlEncode(account.username);
|
||||
string encodedPassword = HttpUtility.UrlEncode(account.password);
|
||||
|
||||
// Create Basic auth string
|
||||
string authString = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{encodedUsername}:{encodedPassword}"));
|
||||
|
||||
// Add basic auth header
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "/api/1/auth/user");
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authString);
|
||||
var authResp = await httpClient.SendAsync(request);
|
||||
if ((await authResp.Content.ReadAsStringAsync()).Contains("totp"))
|
||||
// If we don't have stored cookies or they're invalid, perform login
|
||||
var curUserResp = await httpClient.GetAsync("/api/1/auth/user");
|
||||
if (!curUserResp.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine($"TOTP required for {account.username}");
|
||||
if (account.totpSecret == null)
|
||||
string encodedUsername = HttpUtility.UrlEncode(account.username);
|
||||
string encodedPassword = HttpUtility.UrlEncode(account.password);
|
||||
|
||||
// Create Basic auth string
|
||||
string authString = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{encodedUsername}:{encodedPassword}"));
|
||||
|
||||
// Add basic auth header
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "/api/1/auth/user");
|
||||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authString);
|
||||
var authResp = await httpClient.SendAsync(request);
|
||||
|
||||
if ((await authResp.Content.ReadAsStringAsync()).Contains("totp"))
|
||||
{
|
||||
Console.WriteLine($"No TOTP secret found for {account.username}");
|
||||
return;
|
||||
}
|
||||
|
||||
// totp constructor needs a byte array decoded from the base32 secret
|
||||
var totp = new Totp(Base32Encoding.ToBytes(account.totpSecret.Replace(" ", "")));
|
||||
var code = totp.ComputeTotp();
|
||||
if (code == null)
|
||||
{
|
||||
Console.WriteLine($"Failed to generate TOTP for {account.username}");
|
||||
return;
|
||||
}
|
||||
|
||||
var verifyReq = new HttpRequestMessage(HttpMethod.Post, "/api/1/auth/twofactorauth/totp/verify");
|
||||
// set content type
|
||||
verifyReq.Content = new StringContent($"{{\"code\":\"{code}\"}}", Encoding.UTF8, "application/json");
|
||||
var verifyResp = await httpClient.SendAsync(verifyReq);
|
||||
var verifyRes = await verifyResp.Content.ReadFromJsonAsync<TotpVerifyResponse>();
|
||||
|
||||
if (verifyRes.verified == false)
|
||||
{
|
||||
Console.WriteLine($"Failed to verify TOTP for {account.username}");
|
||||
return;
|
||||
Console.WriteLine($"TOTP required for {account.username}");
|
||||
if (account.totpSecret == null)
|
||||
{
|
||||
Console.WriteLine($"No TOTP secret found for {account.username}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var totp = new Totp(Base32Encoding.ToBytes(account.totpSecret.Replace(" ", "")));
|
||||
var code = totp.ComputeTotp();
|
||||
if (code == null)
|
||||
{
|
||||
Console.WriteLine($"Failed to generate TOTP for {account.username}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var verifyReq = new HttpRequestMessage(HttpMethod.Post, "/api/1/auth/twofactorauth/totp/verify");
|
||||
verifyReq.Content = new StringContent($"{{\"code\":\"{code}\"}}", Encoding.UTF8, "application/json");
|
||||
var verifyResp = await httpClient.SendAsync(verifyReq);
|
||||
var verifyRes = await verifyResp.Content.ReadFromJsonAsync<TotpVerifyResponse>();
|
||||
|
||||
if (verifyRes?.verified != true)
|
||||
{
|
||||
Console.WriteLine($"Failed to verify TOTP for {account.username}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
curUserResp = await httpClient.GetAsync("/api/1/auth/user");
|
||||
}
|
||||
|
||||
var curUserResp = await httpClient.GetAsync("/api/1/auth/user");
|
||||
var curUser = await curUserResp.Content.ReadFromJsonAsync<User>();
|
||||
Console.WriteLine($"Logged in as {curUser.displayName}");
|
||||
Console.WriteLine($"Logged in as {curUser?.displayName}");
|
||||
|
||||
// Store cookies in Redis
|
||||
var cookies = cookieContainer.GetAllCookies().Cast<Cookie>()
|
||||
.ToDictionary(c => c.Name, c => c.Value);
|
||||
await redisService.SaveAuthToken(account.username, cookies);
|
||||
|
||||
apiAccounts.Add(httpClient);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
Console.WriteLine($"Failed to create API for {account.username}: {e.Message}, {e.StatusCode}, {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
LogInAllAccounts();
|
||||
|
||||
// Initialize services
|
||||
await LogInAllAccounts();
|
||||
|
||||
app.MapGet("/", () => $"Logged in with {apiAccounts.Count} accounts");
|
||||
app.MapGet("/rotate", () =>
|
||||
app.MapGet("/rotate", async () =>
|
||||
{
|
||||
RotateAccount();
|
||||
await RotateAccount();
|
||||
return "Rotated account";
|
||||
});
|
||||
|
||||
|
|
@ -243,12 +270,12 @@ async Task DoRequest(HttpContext context, bool retriedAlready = false, bool reAu
|
|||
return;
|
||||
}
|
||||
// re-login all accounts and try again
|
||||
LogInAllAccounts();
|
||||
await LogInAllAccounts();
|
||||
await DoRequest(context, true, true);
|
||||
return;
|
||||
}
|
||||
Console.Error.WriteLine($"Retrying request due to {response.StatusCode}");
|
||||
RotateAccount();
|
||||
await RotateAccount();
|
||||
await DoRequest(context, true);
|
||||
return;
|
||||
}
|
||||
|
|
@ -287,6 +314,4 @@ app.Use(async (context, next) =>
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
app.Run();
|
||||
Loading…
Add table
Add a link
Reference in a new issue