2019-12-07 00:13:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
2019-12-07 00:13:16 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
using System.Threading;
|
2019-12-07 00:13:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
using Wabbajack.Common;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
using Xilium.CefGlue;
|
2019-12-07 00:13:16 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.LibCefHelpers
|
|
|
|
|
{
|
|
|
|
|
public static class Helpers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We bundle the cef libs inside the .exe, we need to extract them before loading any wpf code that requires them
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void ExtractLibs()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists("cefglue.7z") && File.Exists("libcef.dll")) return;
|
|
|
|
|
|
|
|
|
|
using (var fs = File.OpenWrite("cefglue.7z"))
|
|
|
|
|
using (var rs = Assembly.GetExecutingAssembly().GetManifestResourceStream("Wabbajack.Lib.LibCefHelpers.cefglue.7z"))
|
|
|
|
|
{
|
|
|
|
|
rs.CopyTo(fs);
|
|
|
|
|
Utils.Log("Extracting libCef files");
|
|
|
|
|
}
|
|
|
|
|
using (var wq = new WorkQueue(1))
|
|
|
|
|
FileExtractor.ExtractAll(wq, "cefglue.7z", ".");
|
|
|
|
|
|
|
|
|
|
}
|
2019-12-08 17:00:22 +00:00
|
|
|
|
public static HttpClient GetClient(IEnumerable<Cookie> cookies, string referer)
|
|
|
|
|
{
|
|
|
|
|
var container = ToCookieContainer(cookies);
|
|
|
|
|
var handler = new HttpClientHandler { CookieContainer = container };
|
|
|
|
|
var client = new HttpClient(handler);
|
|
|
|
|
client.DefaultRequestHeaders.Referrer = new Uri(referer);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CookieContainer ToCookieContainer(IEnumerable<Cookie> cookies)
|
|
|
|
|
{
|
|
|
|
|
var container = new CookieContainer();
|
|
|
|
|
cookies
|
|
|
|
|
.Do(cookie =>
|
|
|
|
|
{
|
|
|
|
|
container.Add(new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return container;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 05:40:57 +00:00
|
|
|
|
public static async Task<Cookie[]> GetCookies(string domainEnding)
|
|
|
|
|
{
|
|
|
|
|
var manager = CefCookieManager.GetGlobal(null);
|
|
|
|
|
var visitor = new CookieVisitor();
|
|
|
|
|
if (!manager.VisitAllCookies(visitor))
|
|
|
|
|
return new Cookie[0];
|
|
|
|
|
var cc = await visitor.Task;
|
|
|
|
|
|
|
|
|
|
return (await visitor.Task).Where(c => c.Domain.EndsWith(domainEnding)).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 17:00:22 +00:00
|
|
|
|
|
2019-12-07 05:40:57 +00:00
|
|
|
|
private class CookieVisitor : CefCookieVisitor
|
|
|
|
|
{
|
|
|
|
|
TaskCompletionSource<List<Cookie>> _source = new TaskCompletionSource<List<Cookie>>();
|
|
|
|
|
public Task<List<Cookie>> Task => _source.Task;
|
|
|
|
|
|
|
|
|
|
public List<Cookie> Cookies { get; } = new List<Cookie>();
|
|
|
|
|
protected override bool Visit(CefCookie cookie, int count, int total, out bool delete)
|
|
|
|
|
{
|
|
|
|
|
Cookies.Add(new Cookie
|
|
|
|
|
{
|
|
|
|
|
Name = cookie.Name,
|
|
|
|
|
Value = cookie.Value,
|
|
|
|
|
Domain = cookie.Domain,
|
|
|
|
|
Path = cookie.Path
|
|
|
|
|
});
|
|
|
|
|
if (count == total)
|
|
|
|
|
_source.SetResult(Cookies);
|
|
|
|
|
delete = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
_source.SetResult(Cookies);
|
|
|
|
|
}
|
2019-12-08 17:00:22 +00:00
|
|
|
|
|
|
|
|
|
|
2019-12-07 05:40:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Cookie
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public string Value { get; set; }
|
|
|
|
|
public string Domain { get; set; }
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
}
|
2019-12-07 00:13:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|