Chg - Adjusted HTTP Client to allow the usage of VPN split tunneling https://github.com/Crunchy-DL/Crunchy-Downloader/issues/57

Chg - Adjusted some debug messages
This commit is contained in:
Elwador 2024-08-12 17:10:08 +02:00
parent 3914e98dbb
commit 52369e84dc
5 changed files with 97 additions and 36 deletions

View File

@ -79,7 +79,11 @@ public class CrAuth{
if (response.IsOk){
JsonTokenToFileAndVariable(response.ResponseContent);
} else{
MessageBus.Current.SendMessage(new ToastMessage($"Failed to login - {response.ResponseContent.Substring(0,response.ResponseContent.Length < 200 ? response.ResponseContent.Length : 200)}", ToastType.Error, 10));
if (response.ResponseContent.Contains("invalid_credentials")){
MessageBus.Current.SendMessage(new ToastMessage($"Failed to login - because of invalid login credentials", ToastType.Error, 10));
} else{
MessageBus.Current.SendMessage(new ToastMessage($"Failed to login - {response.ResponseContent.Substring(0,response.ResponseContent.Length < 200 ? response.ResponseContent.Length : 200)}", ToastType.Error, 10));
}
}
if (crunInstance.Token?.refresh_token != null){

View File

@ -899,7 +899,7 @@ public class CrunchyrollManager{
tsFile = videoDownloadResult.tsFile;
if (!videoDownloadResult.Ok){
Console.Error.WriteLine($"DL Stats: {JsonConvert.SerializeObject(videoDownloadResult.Parts)}");
Console.Error.WriteLine($"Faild to download video - DL Stats: {JsonConvert.SerializeObject(videoDownloadResult.Parts)}");
dlFailed = true;
}
@ -918,7 +918,7 @@ public class CrunchyrollManager{
tsFile = audioDownloadResult.tsFile;
if (!audioDownloadResult.Ok){
Console.Error.WriteLine($"DL Stats: {JsonConvert.SerializeObject(audioDownloadResult.Parts)}");
Console.Error.WriteLine($"Faild to download audio - DL Stats: {JsonConvert.SerializeObject(audioDownloadResult.Parts)}");
dlFailed = true;
}

View File

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CRD.Downloader.Crunchyroll;
using CRD.Utils;
using CRD.Utils.CustomList;
using CRD.Utils.Structs;
using CRD.Utils.Structs.History;
@ -145,9 +147,14 @@ public class QueueManager{
Queue.Add(selected);
if (selected.Data.Count < dubLang.Count){
Console.WriteLine("Added Episode to Queue but couldn't find all selected dubs");
Console.Error.WriteLine("Added Episode to Queue but couldn't find all selected dubs - Available dubs/subs: ");
var languages = sList.EpisodeAndLanguages.Items.Select((a, index) =>
$"{(a.IsPremiumOnly ? "+ " : "")}{sList.EpisodeAndLanguages.Langs.ElementAtOrDefault(index).CrLocale ?? "Unknown"}").ToArray();
Console.Error.WriteLine($"{selected.SeasonTitle} - Season {selected.Season} - {selected.EpisodeTitle} dubs - [{string.Join(", ", languages)}] subs - [{string.Join(", ", selected.AvailableSubs ?? [])}]");
MessageBus.Current.SendMessage(new ToastMessage($"Added episode to the queue but couldn't find all selected dubs", ToastType.Warning, 2));
} else{
Console.WriteLine("Added Episode to Queue");
@ -213,7 +220,7 @@ public class QueueManager{
bool failed = false;
foreach (var crunchyEpMeta in selected.Values.ToList()){
if (crunchyEpMeta.Data?.First().Playback != null){
if (crunchyEpMeta.Data?.First() != null){
if (CrunchyrollManager.Instance.CrunOptions.History){
var historyEpisode = CrunchyrollManager.Instance.History.GetHistoryEpisodeWithDownloadDir(crunchyEpMeta.ShowId, crunchyEpMeta.SeasonId, crunchyEpMeta.Data.First().MediaId);
if (CrunchyrollManager.Instance.CrunOptions.SonarrProperties is{ SonarrEnabled: true, UseSonarrNumbering: true }){

View File

@ -4,6 +4,8 @@ using System.Net.Http;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using CRD.Downloader;
using CRD.Downloader.Crunchyroll;
@ -34,43 +36,69 @@ public class HttpClientReq{
private HttpClient client;
private HttpClientHandler handler;
private Dictionary<string, CookieCollection> cookieStore;
public HttpClientReq(){
// Initialize the HttpClientHandler
handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();
handler.UseCookies = true;
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli;
cookieStore = new Dictionary<string, CookieCollection>();
// Initialize the HttpClient with the handler
client = new HttpClient(handler);
client = new HttpClient(CreateHttpClientHandler());
// client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0");
client.DefaultRequestHeaders.UserAgent.ParseAdd("Crunchyroll/1.9.0 Nintendo Switch/18.1.0.0 UE4/4.27");
// client.DefaultRequestHeaders.UserAgent.ParseAdd("Crunchyroll/3.60.0 Android/9 okhttp/4.12.0");
}
private HttpMessageHandler CreateHttpClientHandler(){
return new SocketsHttpHandler(){
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli,
ConnectCallback = async (context, cancellationToken) => {
// Resolve IPv4 addresses only
var entry = await Dns.GetHostEntryAsync(context.DnsEndPoint.Host, AddressFamily.InterNetwork, cancellationToken).ConfigureAwait(false);
// Create an IPv4 socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.NoDelay = true;
try{
await socket.ConnectAsync(entry.AddressList, context.DnsEndPoint.Port, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, ownsSocket: true);
} catch{
socket.Dispose();
throw;
}
}
};
}
public void SetETPCookie(string refresh_token){
var cookie = new Cookie("etp_rt", refresh_token){
Domain = "crunchyroll.com",
Path = "/",
};
// var cookie = new Cookie("etp_rt", refresh_token){
// Domain = "crunchyroll.com",
// Path = "/",
// };
//
// var cookie2 = new Cookie("c_locale", "en-US"){
// Domain = "crunchyroll.com",
// Path = "/",
// };
var cookie2 = new Cookie("c_locale", "en-US"){
Domain = "crunchyroll.com",
Path = "/",
};
AddCookie("crunchyroll.com", new Cookie("etp_rt", refresh_token));
AddCookie("crunchyroll.com", new Cookie("c_locale", "en-US"));
}
handler.CookieContainer.Add(cookie);
handler.CookieContainer.Add(cookie2);
private void AddCookie(string domain, Cookie cookie){
if (!cookieStore.ContainsKey(domain)){
cookieStore[domain] = new CookieCollection();
}
cookieStore[domain].Add(cookie);
}
public async Task<(bool IsOk, string ResponseContent)> SendHttpRequest(HttpRequestMessage request){
string content = string.Empty;
try{
AttachCookies(request);
HttpResponseMessage response = await client.SendAsync(request);
content = await response.Content.ReadAsStringAsync();
@ -85,6 +113,23 @@ public class HttpClientReq{
}
}
private void AttachCookies(HttpRequestMessage request){
if (cookieStore.TryGetValue(request.RequestUri.Host, out CookieCollection cookies)){
var cookieHeader = new StringBuilder();
foreach (Cookie cookie in cookies){
if (cookieHeader.Length > 0){
cookieHeader.Append("; ");
}
cookieHeader.Append($"{cookie.Name}={cookie.Value}");
}
if (cookieHeader.Length > 0){
request.Headers.Add("Cookie", cookieHeader.ToString());
}
}
}
public static HttpRequestMessage CreateRequestMessage(string uri, HttpMethod requestMethod, bool authHeader, bool disableDrmHeader, NameValueCollection? query){
UriBuilder uriBuilder = new UriBuilder(uri);
@ -99,7 +144,6 @@ public class HttpClientReq{
}
if (disableDrmHeader){
}
@ -114,7 +158,6 @@ public class HttpClientReq{
public HttpClient GetHttpClient(){
return client;
}
}
public static class Api{

View File

@ -88,6 +88,13 @@ public partial class AddDownloadPageViewModel : ViewModelBase{
}
private async Task UpdateSearch(string value){
if (string.IsNullOrEmpty(value)){
SearchPopupVisible = false;
RaisePropertyChanged(nameof(SearchVisible));
SearchItems.Clear();
return;
}
var searchResults = await CrunchyrollManager.Instance.CrSeries.Search(value, CrunchyrollManager.Instance.CrunOptions.HistoryLang);
var searchItems = searchResults?.Data?.First().Items;