[CHANGE] Cookie login fixed

This commit is contained in:
max
2025-09-05 23:36:51 +02:00
parent 55322f8792
commit f334c87fbb
7 changed files with 83 additions and 19 deletions

View File

@@ -71,14 +71,12 @@ namespace Manager.App.Components.Dialogs
var name = parts[0].Trim();
var value = parts[1].Trim();
// Escape invalid characters
var safeName = Uri.EscapeDataString(name);
var safeValue = Uri.EscapeDataString(value);
var cookie = new Cookie(safeName, safeValue);
var cookie = new Cookie(name, value);
if (!string.IsNullOrEmpty(domain))
cookie.Domain = domain;
cookie.Expires = DateTime.Now.AddDays(1);
cookie.Path = "/";
collection.Add(cookie);
}
@@ -105,7 +103,7 @@ namespace Manager.App.Components.Dialogs
private async Task ValidateAccount()
{
_isLoading = true;
await Client.GetStateAsync();
await Client.BuildClientAsync();
_isLoading = false;
}

View File

@@ -8,6 +8,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
AppContext.SetSwitch("System.Net.Http.EnableActivityPropagation", false);
/* Manager */
builder.SetupLogging();

View File

@@ -44,7 +44,7 @@ public class ClientManager : BackgroundService
var ytClient = new YouTubeClient();
//ytClient.CookieContainer = container;
ytClient.UserAgent = accountEntity.UserAgent;
await ytClient.GetStateAsync();
await ytClient.BuildClientAsync();
return ytClient;
}

View File

@@ -0,0 +1,8 @@
namespace Manager.YouTube.Models;
public class AccountMenuInfo
{
public string? AccountId { get; set; }
public string? AccountHandle { get; set; }
public string? ImageUrl { get; set; }
}

View File

@@ -1,8 +1,10 @@
using System.Net;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using DotBased.Monads;
using Manager.YouTube.Models;
using Manager.YouTube.Models.Innertube;
using Manager.YouTube.Parsers;
using Manager.YouTube.Util;
@@ -20,12 +22,8 @@ public static class NetworkService
Method = HttpMethod.Get,
RequestUri = new Uri(Origin)
};
httpRequest.Headers.IfModifiedSince = new DateTimeOffset(DateTime.UtcNow);
httpRequest.Headers.Clear();
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
httpRequest.Headers.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpRequest.Headers.Connection.Add("keep-alive");
httpRequest.Headers.Add("DNT", "1");
httpRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
var http = client.GetHttpClient();
if (http == null)
@@ -59,7 +57,48 @@ public static class NetworkService
return clientState == null ? ResultError.Fail("Unable to parse client state!") : clientState;
}
public static async Task<Result> GetCurrentAccountInfoAsync(YouTubeClient client)
public static async Task<Result<string[]>> GetDatasyncIds(YouTubeClient client)
{
if (client.ClientState is not { LoggedIn: true } || client.CookieContainer.Count == 0)
{
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
}
var httpClient = client.GetHttpClient();
if (httpClient == null)
{
return ResultError.Fail("Unable to get http client!");
}
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri($"{Origin}/getDatasyncIdsEndpoint")
};
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
httpRequest.Headers.Add("Origin", Origin);
var response = await httpClient.SendAsync(httpRequest);
if (!response.IsSuccessStatusCode)
{
var responseResult = await response.Content.ReadAsStringAsync();
return ResultError.Fail(responseResult);
}
var responseContent = await response.Content.ReadAsStringAsync();
var datasyncIdsJson = JsonNode.Parse(responseContent.Replace(")]}'", ""));
var isLoggedOut = datasyncIdsJson?["responseContext"]?["mainAppWebResponseContext"]?["loggedOut"]
.Deserialize<bool>() ?? true;
if (!isLoggedOut)
{
return datasyncIdsJson?["datasyncIds"].Deserialize<string[]>() ?? [];
}
return ResultError.Fail("Failed to get datasyncIds!");
}
public static async Task<Result<AccountMenuInfo>> GetCurrentAccountInfoAsync(YouTubeClient client)
{
if (client.ClientState is not { LoggedIn: true })
{

View File

@@ -9,11 +9,11 @@ public static class AuthenticationUtilities
{
private const string HeaderScheme = "SAPISIDHASH";
// Dave Thomas @ https://stackoverflow.com/a/32065323/9948300
// Dave Thomas & windy for updated answer @ https://stackoverflow.com/a/32065323/9948300
public static AuthenticationHeaderValue? GetSapisidHashHeader(string datasyncId, string sapisid, string origin)
{
var strHash = GetSapisidHash(datasyncId, sapisid, origin);
return new AuthenticationHeaderValue(HeaderScheme, strHash);
return strHash == null ? null : new AuthenticationHeaderValue(HeaderScheme, strHash);
}
public static string? GetSapisidHash(string datasyncId, string sapisid, string origin, string? time = null)

View File

@@ -8,9 +8,10 @@ public sealed class YouTubeClient : IDisposable
public string Id { get; private set; } = "";
public string AccountName => ClientState?.UserAccountName ?? "";
public string? UserAgent { get; set; }
public CookieContainer CookieContainer { get; } = new();
public CookieContainer CookieContainer { get; } = new() { Capacity = 100, PerDomainCapacity = 50 };
public ClientState? ClientState { get; private set; }
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"] ?? CookieContainer.GetAllCookies()["__Secure-3PAPISID"];
public List<string> DatasyncIds { get; set; } = [];
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
public HttpClient? GetHttpClient() => _httpClient;
private HttpClient? _httpClient;
@@ -31,9 +32,10 @@ public sealed class YouTubeClient : IDisposable
CookieContainer = CookieContainer
};
_httpClient = new HttpClient(clientHandler);
_httpClient.DefaultRequestHeaders.Clear();
}
public async Task GetStateAsync()
public async Task BuildClientAsync()
{
if (ClientState == null || !ClientState.LoggedIn)
{
@@ -44,7 +46,23 @@ public sealed class YouTubeClient : IDisposable
}
ClientState = state.Value;
}
if (string.IsNullOrWhiteSpace(ClientState.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
{
var datasyncResult = await NetworkService.GetDatasyncIds(this);
if (!datasyncResult.IsSuccess)
{
return;
}
foreach (var id in datasyncResult.Value)
{
if (DatasyncIds.Contains(id))
continue;
DatasyncIds.Add(id);
}
}
var accountInfo = await NetworkService.GetCurrentAccountInfoAsync(this);
}