Skip to content

Commit dc8c3ce

Browse files
committed
Refactor cookie handling & FlareSolverr
1 parent e8d6af4 commit dc8c3ce

17 files changed

+308
-282
lines changed

SmartImage.Lib/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Flurl.Http;
1414
using Flurl.Http.Content;
1515
using Kantan.Net.Utilities;
16+
using Kantan.Net.Web;
1617
using SmartImage.Lib.Clients;
1718
using SmartImage.Lib.Images;
1819
using SmartImage.Lib.Results;
@@ -27,14 +28,14 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2728

2829
// todo
2930

30-
public sealed class Ascii2DEngine : WebSearchEngine, ICookieReceiver
31+
public sealed class Ascii2DEngine : WebSearchEngine, ICookiesReceiver
3132
{
3233

3334
public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
3435
{
35-
Timeout = TimeSpan.FromSeconds(30);
36-
MaxSize = 10000000;
37-
CookieJar = new CookieJar();
36+
Timeout = TimeSpan.FromSeconds(30);
37+
MaxSize = 10000000;
38+
Jar = new CookieJar();
3839
}
3940

4041
protected override string NodesSelector => Serialization.S_Ascii2D_Images2;
@@ -85,7 +86,8 @@ protected override Url GetRawUrl(SearchQuery query)
8586

8687
public override void Dispose() { }
8788

88-
public CookieJar CookieJar { get; }
89+
public CookieJar Jar { get; }
90+
8991

9092
protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, SearchQuery query,
9193
CancellationToken token = default)
@@ -122,7 +124,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
122124

123125

124126
foreach (FlareSolverrCookie cookie in cookies) {
125-
CookieJar.AddOrReplace(new FlurlCookie(cookie.Name, cookie.Value, fsr.Solution.Url) { });
127+
Jar.AddOrReplace(new FlurlCookie(cookie.Name, cookie.Value, fsr.Solution.Url) { });
126128
}
127129

128130
var res = await Client.Request(newUrl)
@@ -131,7 +133,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
131133
x.HttpVersion = "2.0";
132134
})
133135
.AllowAnyHttpStatus()
134-
.WithCookies(CookieJar)
136+
.WithCookies(Jar)
135137
.WithTimeout(Timeout)
136138
/*.OnError(s =>
137139
{
@@ -260,12 +262,12 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode nx, SearchR
260262
return ValueTask.FromResult(sri);
261263
}
262264

263-
public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, CancellationToken ct = default)
265+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct)
264266
{
265-
var ck = provider.Jar.Where(x => x.Domain.Contains("ascii2d"));
266-
267+
var ck = Jar.Where(static x => x.Domain.Contains("ascii2d"));
268+
267269
foreach (var c in ck) {
268-
CookieJar.AddOrReplace(c);
270+
Jar.AddOrReplace(c);
269271
}
270272

271273
return true;

SmartImage.Lib/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using AngleSharp.Dom;
1111
using AngleSharp.Html.Parser;
1212
using AngleSharp.XPath;
13+
using CliWrap;
1314
using Flurl.Http;
1415
using Flurl.Http.Content;
1516
using Kantan.Net;
@@ -26,7 +27,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2627
/// <see cref="SearchEngineOptions.EHentai" />
2728
/// </summary>
2829
/// <remarks>Handles both ExHentai and E-Hentai</remarks>
29-
public sealed class EHentaiEngine : WebSearchEngine, IConfigurable, ICookieReceiver, INotifyPropertyChanged
30+
public sealed class EHentaiEngine : WebSearchEngine, ISearchConfigReceiver, ICookiesReceiver, INotifyPropertyChanged
3031
{
3132

3233
private const string HOST_EH = ".e-hentai.org";
@@ -75,10 +76,12 @@ static EHentaiEngine() { }
7576

7677
public EHentaiEngine(bool useExHentai = true) : base(EHentaiBase)
7778
{
78-
m_client = new HttpClient(m_clientHandler);
79-
IsLoggedIn = false;
79+
m_client = new HttpClient(m_clientHandler);
80+
IsLoggedIn = false;
81+
8082
m_cookies = new();
8183
UseExHentai = useExHentai;
84+
Jar = new CookieJar() { };
8285
}
8386

8487
/*
@@ -91,10 +94,10 @@ public EHentaiEngine(bool useExHentai = true) : base(EHentaiBase)
9194
* https://gitlab.com/NekoInverter/EhViewer/-/blob/master/app/src/main/java/com/hippo/ehviewer/client/EhCookieStore.java
9295
*/
9396

94-
public async ValueTask ApplyAsync(SearchConfig cfg)
97+
public async ValueTask ApplyConfigAsync(SearchConfig cfg)
9598
{
9699
/*if (this is { IsLoggedIn: true }/* && !(Username != cfg.EhUsername && Password != cfg.EhPassword)#1#) {
97-
Debug.WriteLine($"{Name} is already logged in", nameof(ApplyAsync));
100+
Debug.WriteLine($"{Name} is already logged in", nameof(ApplyConfigAsync));
98101
99102
return;
100103
}*/
@@ -108,7 +111,10 @@ public async ValueTask ApplyAsync(SearchConfig cfg)
108111
*/
109112

110113

111-
public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, CancellationToken ct = default)
114+
public CookieJar Jar { get; }
115+
116+
117+
public async ValueTask<bool> ApplyCookiesAsync(ICookiesProvider provider, CancellationToken ct = default)
112118
{
113119
Trace.WriteLine($"Applying cookies to {Name}");
114120

@@ -119,11 +125,10 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
119125
return false;
120126
}*/
121127

122-
var ok = await provider.LoadCookiesAsync(Browser.Firefox, ct);
123-
var cookies = provider.Jar;
128+
var cookies = await provider.LoadCookiesAsync(ct);
124129

125-
var fcc = cookies.Where(x =>
126-
{
130+
foreach (var cookie in cookies) {
131+
var x = cookie.AsCookie();
127132
bool a = false;
128133

129134
if (UseExHentai) {
@@ -136,14 +141,13 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
136141
// return b;
137142
a |= b;
138143

139-
return a;
140-
});
141-
142-
foreach (var cookie in fcc) {
143-
m_cookies.Add(
144-
new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain) { });
144+
if (a) {
145+
// m_cookies.Add(new Cookie(x.Name, x.Value, x.Path, x.Domain) { });
146+
Jar.AddOrReplace(new FlurlCookie(x.Name,x.Value, UseExHentai ? ExHentaiBase : EHentaiBase));
147+
}
145148
}
146149

150+
147151
var res2 = await GetSessionAsync();
148152

149153
/*var res2 = await EHentaiBase.WithCookies(m_clientHandler.CookieContainer)
@@ -154,8 +158,11 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
154158
.WithAutoRedirect(true)
155159
.GetAsync();*/
156160

161+
/*
157162
m_clientHandler.CookieContainer.Add(m_cookies);
158163
m_client.Timeout = Timeout;
164+
*/
165+
159166

160167
return IsLoggedIn = res2.ResponseMessage.IsSuccessStatusCode;
161168

@@ -164,7 +171,8 @@ public async ValueTask<bool> ApplyCookiesAsync(ICookieProvider provider, Cancell
164171
private Task<IFlurlResponse> GetSessionAsync()
165172
{
166173
return (UseExHentai ? ExHentaiBase : EHentaiBase)
167-
.WithCookies(m_cookies)
174+
// .WithCookies(m_cookies)
175+
.WithCookies(Jar)
168176
.WithTimeout(Timeout)
169177
.WithHeaders(new
170178
{
@@ -227,17 +235,19 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
227235

228236
Debug.WriteLine($"{LookupUrl}", nameof(GetDocumentAsync));
229237

230-
var req = new HttpRequestMessage(HttpMethod.Post, LookupUrl)
238+
var req2 = new FlurlRequest(LookupUrl)
231239
{
240+
CookieJar = Jar,
241+
Verb = HttpMethod.Post,
232242
Content = data,
233243
Headers =
234244
{
235245
{ "User-Agent", HttpUtilities.UserAgent }
236-
},
237-
246+
}
238247
};
239248

240-
var res = await m_client.SendAsync(req, token);
249+
var res1 = await Client.SendAsync(req2, cancellationToken: token);
250+
var res = res1.ResponseMessage;
241251

242252
// Debug.WriteLine($"{res.StatusCode}");
243253

@@ -300,9 +310,9 @@ protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchRe
300310

301311
public override void Dispose()
302312
{
303-
m_client.Dispose();
304-
m_clientHandler.Dispose();
305-
m_cookies.Clear();
313+
// m_client.Dispose();
314+
// m_clientHandler.Dispose();
315+
Jar.Clear();
306316

307317
IsLoggedIn = false;
308318
}

SmartImage.Lib/Engines/Impl/Search/SauceNaoEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
namespace SmartImage.Lib.Engines.Impl.Search;
3333

34-
public sealed class SauceNaoEngine : BaseSearchEngine, IConfigurable, IDisposable
34+
public sealed class SauceNaoEngine : BaseSearchEngine, ISearchConfigReceiver, IDisposable
3535
{
3636

3737
private const string URL_BASE = "https://saucenao.com/";
@@ -472,7 +472,7 @@ private async ValueTask GetAPIResultsAsync(SearchQuery url, SearchResult sr)
472472
return;
473473
}
474474

475-
public ValueTask ApplyAsync(SearchConfig cfg)
475+
public ValueTask ApplyConfigAsync(SearchConfig cfg)
476476
{
477477
Authentication = cfg.SauceNaoKey;
478478
return ValueTask.CompletedTask;

SmartImage.Lib/Images/ImageScanner.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ static ImageScanner()
6262

6363
Cookies = new CookieJar();
6464

65-
6665

6766
}
6867

@@ -73,7 +72,7 @@ static ImageScanner()
7372

7473

7574
/*
76-
* TODO: ImageScanner, SmartCookiesProvider, and FlareSolverrr
75+
* TODO: ImageScanner, AutoCookiesProvider, and FlareSolverrr
7776
*/
7877

7978
/*public static IFlurlRequest BuildRequest(params object[] urlSeg)
@@ -322,33 +321,6 @@ public UniSimilarity(UniImage image, double similarity)
322321

323322
}
324323

325-
public static HttpMessageHandler GetMostInnerHandler(this HttpMessageHandler self)
326-
{
327-
return self is DelegatingHandler handler
328-
? handler.InnerHandler.GetMostInnerHandler()
329-
: self;
330-
}
331-
332-
public static IFlurlRequest AddChromeImpersonation(this IFlurlRequest req)
333-
{
334-
return req.WithHeaders(new
335-
{
336-
sec_ch_ua = "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"",
337-
sec_ch_ua_mobile = "?0",
338-
sec_ch_ua_platform = "Windows",
339-
Upgrade_Insecure_Requests = "1",
340-
User_Agent =
341-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
342-
Accept =
343-
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
344-
Sec_Fetch_Site = "none",
345-
Sec_Fetch_Mode = "navigate",
346-
Sec_Fetch_User = "?1",
347-
Sec_Fetch_Dest = "document",
348-
Accept_Encoding = "gzip, deflate, br",
349-
Accept_Language = "en-US,en;q=0.9"
350-
});
351-
}
352324

353325
public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks, SearchQuery query,
354326
CancellationToken ct = default)

0 commit comments

Comments
 (0)