remove dependency on VRChat.API
This commit is contained in:
parent
db4c283210
commit
5e19348a11
4 changed files with 57 additions and 47 deletions
|
|
@ -1,12 +0,0 @@
|
||||||
using System.Net;
|
|
||||||
using VRChat.API.Client;
|
|
||||||
|
|
||||||
namespace VRCAuthProxy;
|
|
||||||
|
|
||||||
public class ApiClientWithCookies : ApiClient
|
|
||||||
{
|
|
||||||
public CookieContainer GetCookieContainer()
|
|
||||||
{
|
|
||||||
return CookieContainer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +1,19 @@
|
||||||
// See https://aka.ms/new-console-template for more information
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Web;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using OtpNet;
|
using OtpNet;
|
||||||
using VRCAuthProxy;
|
using VRCAuthProxy;
|
||||||
using VRChat.API.Api;
|
using VRCAuthProxy.types;
|
||||||
using VRChat.API.Client;
|
|
||||||
using VRChat.API.Model;
|
|
||||||
using HttpMethod = System.Net.Http.HttpMethod;
|
using HttpMethod = System.Net.Http.HttpMethod;
|
||||||
|
using User = VRCAuthProxy.types.User;
|
||||||
|
|
||||||
var httpClient = new HttpClient();
|
var apiAccounts = new List<HttpClient>();
|
||||||
|
|
||||||
var apiAccounts = new List<ApiClientWithCookies>();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -25,7 +26,6 @@ void RotateAccount()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String UserAgent = "VRCAuthProxy V1.0.0 (https://github.com/PrideVRCommunity/VRCAuthProxy)";
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
@ -34,19 +34,31 @@ Config.Instance.Accounts.ForEach(async account =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var cookieContainer = new CookieContainer();
|
||||||
|
var handler = new HttpClientHandler
|
||||||
|
{
|
||||||
|
CookieContainer = cookieContainer
|
||||||
|
};
|
||||||
|
var httpClient = new HttpClient(handler)
|
||||||
|
{
|
||||||
|
BaseAddress = new Uri("https://api.vrchat.cloud/api/1")
|
||||||
|
|
||||||
|
};
|
||||||
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "VRCAuthProxy V1.0.0 (https://github.com/PrideVRCommunity/VRCAuthProxy)");
|
||||||
|
|
||||||
Console.WriteLine($"Creating API for {account.username}");
|
Console.WriteLine($"Creating API for {account.username}");
|
||||||
// clone base config
|
|
||||||
var config = new Configuration();
|
string encodedUsername = HttpUtility.UrlEncode(account.username);
|
||||||
config.UserAgent = UserAgent;
|
string encodedPassword = HttpUtility.UrlEncode(account.password);
|
||||||
config.BasePath = "https://api.vrchat.cloud/api/1";
|
|
||||||
config.Username = account.username;
|
|
||||||
config.Password = account.password;
|
|
||||||
|
|
||||||
// create api client
|
// Create Basic auth string
|
||||||
var api = new ApiClientWithCookies();
|
string authString = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{encodedUsername}:{encodedPassword}"));
|
||||||
var authApi = new AuthenticationApi(api, api, config);
|
|
||||||
var authResp = authApi.GetCurrentUserWithHttpInfo();
|
// Add basic auth header
|
||||||
if (authResp.RawContent.Contains("totp"))
|
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($"TOTP required for {account.username}");
|
Console.WriteLine($"TOTP required for {account.username}");
|
||||||
if (account.totpSecret == null)
|
if (account.totpSecret == null)
|
||||||
|
|
@ -64,8 +76,13 @@ Config.Instance.Accounts.ForEach(async account =>
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var verifyRes = authApi.Verify2FA(new TwoFactorAuthCode(code));
|
var verifyReq = new HttpRequestMessage(HttpMethod.Post, "/api/1/auth/twofactorauth/totp/verify");
|
||||||
if (verifyRes == null || verifyRes.Verified == false)
|
// 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}");
|
Console.WriteLine($"Failed to verify TOTP for {account.username}");
|
||||||
return;
|
return;
|
||||||
|
|
@ -73,13 +90,13 @@ Config.Instance.Accounts.ForEach(async account =>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var curUser = authApi.GetCurrentUser();
|
var curUserResp = await httpClient.GetAsync("/api/1/auth/user");
|
||||||
if (curUser == null) throw new Exception("Failed to get current user");
|
var curUser = await curUserResp.Content.ReadFromJsonAsync<User>();
|
||||||
Console.WriteLine($"Logged in as {curUser.DisplayName}");
|
Console.WriteLine($"Logged in as {curUser.displayName}");
|
||||||
apiAccounts.Add(api);
|
apiAccounts.Add(httpClient);
|
||||||
} catch (ApiException e)
|
} catch (HttpRequestException e)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Failed to create API for {account.username}: {e.Message}, {e.ErrorCode}, {e}");
|
Console.WriteLine($"Failed to create API for {account.username}: {e.Message}, {e.StatusCode}, {e}");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -102,8 +119,6 @@ app.Use(async (context, next) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
var account = apiAccounts.First();
|
var account = apiAccounts.First();
|
||||||
var requestOpts = new RequestOptions();
|
|
||||||
requestOpts.Operation = context.Request.Method;
|
|
||||||
var path = context.Request.Path.ToString().Replace("/api/1", "") + context.Request.QueryString;
|
var path = context.Request.Path.ToString().Replace("/api/1", "") + context.Request.QueryString;
|
||||||
|
|
||||||
var message = new HttpRequestMessage
|
var message = new HttpRequestMessage
|
||||||
|
|
@ -112,9 +127,6 @@ app.Use(async (context, next) =>
|
||||||
Method = new HttpMethod(context.Request.Method)
|
Method = new HttpMethod(context.Request.Method)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add common headers to request message
|
|
||||||
message.Headers.Add("Cookie", account.GetCookieContainer().GetCookieHeader(new Uri("https://api.vrchat.cloud")));
|
|
||||||
|
|
||||||
// Handle request body for methods that support content (POST, PUT, DELETE)
|
// Handle request body for methods that support content (POST, PUT, DELETE)
|
||||||
if (context.Request.ContentLength > 0 || context.Request.Headers.ContainsKey("Transfer-Encoding"))
|
if (context.Request.ContentLength > 0 || context.Request.Headers.ContainsKey("Transfer-Encoding"))
|
||||||
{
|
{
|
||||||
|
|
@ -149,7 +161,7 @@ app.Use(async (context, next) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
|
var response = await account.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
|
||||||
|
|
||||||
// Copy response status code and headers
|
// Copy response status code and headers
|
||||||
context.Response.StatusCode = (int)response.StatusCode;
|
context.Response.StatusCode = (int)response.StatusCode;
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,8 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8"/>
|
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
|
||||||
<PackageReference Include="Otp.NET" Version="1.4.0"/>
|
<PackageReference Include="Otp.NET" Version="1.4.0" />
|
||||||
<PackageReference Include="VRChat.API" Version="1.18.8"/>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
11
VRCAuthProxy/types/API.cs
Normal file
11
VRCAuthProxy/types/API.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
namespace VRCAuthProxy.types;
|
||||||
|
|
||||||
|
public struct TotpVerifyResponse
|
||||||
|
{
|
||||||
|
public bool verified { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct User
|
||||||
|
{
|
||||||
|
public string displayName { get; set; }
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue