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;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
using CefSharp;
|
2019-12-27 00:41:33 +00:00
|
|
|
|
using CefSharp.OffScreen;
|
2019-12-07 00:13:16 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2020-05-29 22:51:41 +00:00
|
|
|
|
using Cookie = CefSharp.Cookie;
|
2019-12-07 00:13:16 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.LibCefHelpers
|
|
|
|
|
{
|
|
|
|
|
public static class Helpers
|
|
|
|
|
{
|
2020-06-26 17:08:30 +00:00
|
|
|
|
public static Wabbajack.Lib.Http.Client GetClient(IEnumerable<Cookie> cookies, string referer)
|
2019-12-08 17:00:22 +00:00
|
|
|
|
{
|
2020-06-26 17:08:30 +00:00
|
|
|
|
var client = new Wabbajack.Lib.Http.Client();
|
2020-02-14 22:27:48 +00:00
|
|
|
|
client.Headers.Add(("Referrer", referer));
|
|
|
|
|
client.Cookies.AddRange(cookies.Select(cookie => new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)));
|
|
|
|
|
return client;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 05:30:31 +00:00
|
|
|
|
public static async Task<Cookie[]> GetCookies(string domainEnding = "")
|
2019-12-07 05:40:57 +00:00
|
|
|
|
{
|
2019-12-26 23:26:53 +00:00
|
|
|
|
var manager = Cef.GetGlobalCookieManager();
|
2019-12-07 05:40:57 +00:00
|
|
|
|
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-26 23:26:53 +00:00
|
|
|
|
private class CookieVisitor : ICookieVisitor
|
2019-12-07 05:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
TaskCompletionSource<List<Cookie>> _source = new TaskCompletionSource<List<Cookie>>();
|
|
|
|
|
public Task<List<Cookie>> Task => _source.Task;
|
|
|
|
|
|
|
|
|
|
public List<Cookie> Cookies { get; } = new List<Cookie>();
|
2019-12-26 23:26:53 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_source.SetResult(Cookies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
|
2019-12-07 05:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
Cookies.Add(new Cookie
|
|
|
|
|
{
|
|
|
|
|
Name = cookie.Name,
|
|
|
|
|
Value = cookie.Value,
|
|
|
|
|
Domain = cookie.Domain,
|
|
|
|
|
Path = cookie.Path
|
|
|
|
|
});
|
|
|
|
|
if (count == total)
|
|
|
|
|
_source.SetResult(Cookies);
|
2019-12-26 23:26:53 +00:00
|
|
|
|
deleteCookie = false;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonName("HttpCookie")]
|
2019-12-07 05:40:57 +00:00
|
|
|
|
public class Cookie
|
|
|
|
|
{
|
2020-04-10 01:29:53 +00:00
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
public string Value { get; set; } = string.Empty;
|
|
|
|
|
public string Domain { get; set; } = string.Empty;
|
|
|
|
|
public string Path { get; set; } = string.Empty;
|
2019-12-07 05:40:57 +00:00
|
|
|
|
}
|
2019-12-27 00:41:33 +00:00
|
|
|
|
|
2021-06-14 03:31:09 +00:00
|
|
|
|
public static Func<IBrowser, IFrame, string, IRequest, IResourceHandler>? SchemeHandler { get; set; }
|
|
|
|
|
|
2019-12-27 00:41:33 +00:00
|
|
|
|
public static void Init()
|
|
|
|
|
{
|
2021-02-04 12:53:14 +00:00
|
|
|
|
if (Inited || Cef.IsInitialized) return;
|
2020-02-06 05:30:31 +00:00
|
|
|
|
Inited = true;
|
|
|
|
|
CefSettings settings = new CefSettings();
|
2020-05-29 22:51:41 +00:00
|
|
|
|
settings.CachePath = Consts.CefCacheLocation.ToString();
|
2020-07-28 23:52:45 +00:00
|
|
|
|
settings.JavascriptFlags = "--noexpose_wasm";
|
2021-06-14 03:31:09 +00:00
|
|
|
|
settings.RegisterScheme(new CefCustomScheme()
|
|
|
|
|
{
|
|
|
|
|
SchemeName = "wabbajack",
|
|
|
|
|
SchemeHandlerFactory = new SchemeHandlerFactor()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2020-02-06 05:30:31 +00:00
|
|
|
|
Cef.Initialize(settings);
|
2019-12-27 00:41:33 +00:00
|
|
|
|
}
|
2020-02-06 05:30:31 +00:00
|
|
|
|
|
2021-06-14 03:31:09 +00:00
|
|
|
|
private class SchemeHandlerFactor : ISchemeHandlerFactory
|
|
|
|
|
{
|
|
|
|
|
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (SchemeHandler != null && schemeName == "wabbajack")
|
|
|
|
|
{
|
|
|
|
|
return SchemeHandler!(browser, frame, schemeName, request);
|
|
|
|
|
}
|
|
|
|
|
return new ResourceHandler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 05:30:31 +00:00
|
|
|
|
public static bool Inited { get; set; }
|
2020-05-29 22:51:41 +00:00
|
|
|
|
|
|
|
|
|
public static void ClearCookies()
|
|
|
|
|
{
|
|
|
|
|
var manager = Cef.GetGlobalCookieManager();
|
|
|
|
|
var visitor = new CookieDeleter();
|
|
|
|
|
manager.VisitAllCookies(visitor);
|
|
|
|
|
}
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
public static async Task DeleteCookiesWhere(Func<Cookie,bool> filter)
|
|
|
|
|
{
|
|
|
|
|
var manager = Cef.GetGlobalCookieManager();
|
|
|
|
|
var visitor = new CookieDeleter(filter);
|
|
|
|
|
manager.VisitAllCookies(visitor);
|
|
|
|
|
}
|
2020-05-29 22:51:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CookieDeleter : ICookieVisitor
|
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
private Func<Helpers.Cookie, bool>? _filter;
|
|
|
|
|
|
|
|
|
|
public CookieDeleter(Func<Helpers.Cookie, bool>? filter = null)
|
|
|
|
|
{
|
|
|
|
|
_filter = filter;
|
|
|
|
|
}
|
2020-05-29 22:51:41 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
|
|
|
|
|
{
|
2020-12-31 06:44:42 +00:00
|
|
|
|
if (_filter == null)
|
|
|
|
|
{
|
|
|
|
|
deleteCookie = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var conv = new Helpers.Cookie
|
|
|
|
|
{
|
|
|
|
|
Name = cookie.Name, Domain = cookie.Domain, Value = cookie.Value, Path = cookie.Path
|
|
|
|
|
};
|
|
|
|
|
if (_filter(conv))
|
|
|
|
|
deleteCookie = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 22:51:41 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-12-27 00:41:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ModuleInitializer
|
|
|
|
|
{
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
2020-02-24 13:04:54 +00:00
|
|
|
|
var es = Assembly.GetEntryAssembly();
|
|
|
|
|
if (es != null && es.Location != null && Path.GetFileNameWithoutExtension(es.Location) == "Wabbajack")
|
|
|
|
|
Helpers.Init();
|
2019-12-27 00:41:33 +00:00
|
|
|
|
}
|
2019-12-07 00:13:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|