Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the Library #63

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 1 addition & 5 deletions PuppeteerExtraSharp/Plugins/AnonymizeUa/AnonymizeUaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

namespace PuppeteerExtraSharp.Plugins.AnonymizeUa
{
public class AnonymizeUaPlugin: PuppeteerExtraPlugin
public class AnonymizeUaPlugin() : PuppeteerExtraPlugin("anonymize-ua")
{
public AnonymizeUaPlugin(): base("anonymize-ua")
{
}

private Func<string, string> _customAction;
public void CustomizeUa(Func<string, string> uaAction)
{
Expand Down
34 changes: 18 additions & 16 deletions PuppeteerExtraSharp/Plugins/ExtraStealth/Evasions/Languages.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PuppeteerSharp;

namespace PuppeteerExtraSharp.Plugins.ExtraStealth.Evasions
{
public class Languages : PuppeteerExtraPlugin
public class Languages(StealthLanguagesOptions options = null) : PuppeteerExtraPlugin("stealth-language")
{
public StealthLanguagesOptions Options { get; }
public StealthLanguagesOptions Options { get; } = options ?? new StealthLanguagesOptions("en-US", "en");

public Languages(StealthLanguagesOptions options = null) : base("stealth-language")
public override async Task OnPageCreated(IPage page)
{
Options = options ?? new StealthLanguagesOptions("en-US", "en");
}
if (Options.Languages.Length > 0)
{
await page.SetExtraHttpHeadersAsync(new()
{
["Accept-Language"] = string.Join(',', Options.Languages),
});

List<string> langs = Options.Languages.Select(l => "\"" + l.ToString() + "\"").ToList();
await page.EvaluateExpressionOnNewDocumentAsync("Object.defineProperty(Object.getPrototypeOf(navigator), 'languages', { get: function () { '[native code]'; return '[" + string.Join(",", langs) + "]'; } });");
}

public override Task OnPageCreated(IPage page)
{
var script = Utils.GetScript("Language.js");
return Utils.EvaluateOnNewPage(page,script, Options.Languages);
await Utils.EvaluateOnNewPage(page, script, Options.Languages);
}
}

public class StealthLanguagesOptions : IPuppeteerExtraPluginOptions
public class StealthLanguagesOptions(params string[] languages) : IPuppeteerExtraPluginOptions
{
public object[] Languages { get; }

public StealthLanguagesOptions(params string[] languages)
{
Languages = languages.Cast<object>().ToArray();
}
public object[] Languages { get; } = languages.Cast<object>().ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public SourceUrl() : base("SourceUrl")
{
}

public override async Task OnPageCreated(IPage page)
public override Task OnPageCreated(IPage page)
{
var mainWordProperty =
page.MainFrame.GetType().GetProperty("MainWorld", BindingFlags.NonPublic
Expand All @@ -31,6 +31,8 @@ public override async Task OnPageCreated(IPage page)
suffixField?.SetValue(execution, "//# sourceURL=''");
}
};

return Task.CompletedTask;
}
}
}
38 changes: 19 additions & 19 deletions PuppeteerExtraSharp/Plugins/ExtraStealth/StealthPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ public StealthPlugin(params IPuppeteerExtraPluginOptions[] options) : base("stea

private List<PuppeteerExtraPlugin> GetStandardEvasions()
{
return new List<PuppeteerExtraPlugin>()
{
new WebDriver(),
// new ChromeApp(),
new ChromeSci(),
new ChromeRuntime(),
new Codec(),
new Languages(GetOptionByType<StealthLanguagesOptions>()),
new OutDimensions(),
new Permissions(),
new UserAgent(),
new Vendor(GetOptionByType<StealthVendorSettings>()),
new WebGl(GetOptionByType<StealthWebGLOptions>()),
new PluginEvasion(),
new StackTrace(),
new HardwareConcurrency(GetOptionByType<StealthHardwareConcurrencyOptions>()),
new ContentWindow(),
new SourceUrl()
};
return
[
new WebDriver(),
// new ChromeApp(),
new ChromeSci(),
new ChromeRuntime(),
new Codec(),
new Languages(GetOptionByType<StealthLanguagesOptions>()),
new OutDimensions(),
new Permissions(),
new UserAgent(),
new Vendor(GetOptionByType<StealthVendorSettings>()),
new WebGl(GetOptionByType<StealthWebGLOptions>()),
new PluginEvasion(),
new StackTrace(),
new HardwareConcurrency(GetOptionByType<StealthHardwareConcurrencyOptions>()),
new ContentWindow(),
new SourceUrl()
];
}

public override ICollection<PuppeteerExtraPlugin> GetDependencies() => _standardEvasions;
Expand Down
28 changes: 28 additions & 0 deletions PuppeteerExtraSharp/Plugins/Proxies/ProxiesPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using PuppeteerSharp;
using System.Linq;
using System.Threading.Tasks;

namespace PuppeteerExtraSharp.Plugins.Proxies
{
public class ProxiesPlugin(ProxyInfo proxy) : PuppeteerExtraPlugin("proxies")
{
private ProxyInfo Proxy { get; } = proxy;

public override void BeforeLaunch(LaunchOptions options)
{
var args = options.Args.Where(a => !a.StartsWith("--proxy-server=")).ToList();
args.Add("--proxy-server=" + Proxy.Ip + ":" + Proxy.Port);

options.Args = [..args];
}

public override async Task OnPageCreated(IPage page)
{
await page.AuthenticateAsync(new()
{
Username = Proxy.Login,
Password = Proxy.Password,
});
}
}
}
12 changes: 12 additions & 0 deletions PuppeteerExtraSharp/Plugins/Proxies/ProxyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace PuppeteerExtraSharp.Plugins.Proxies
{
#nullable enable
public class ProxyInfo(string ip, string port, string? login = null, string? password = null)
{
public string Ip { get; set; } = ip;
public string Port { get; set; } = port;
public string? Login { get; set; } = login;
public string? Password { get; set; } = password;
}
#nullable disable
}
15 changes: 15 additions & 0 deletions PuppeteerExtraSharp/Plugins/Proxies/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Quickstart

Work's with IPv4 and IPv6 proxies!

```c#
var extra = new PuppeteerExtra();
// initialize proxies plugin
var proxies = new ProxiesPlugin(new ProxyInfo("localhost", "9000"));

var browser = await extra.Use(proxies).LaunchAsync(new LaunchOptions());

var page = await browser.NewPageAsync();

await page.GoToAsync("https://gimly.su/");
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.Models;
using PuppeteerExtraSharp.Plugins.Recaptcha.RestClient;
using RestSharp;
Expand All @@ -17,7 +18,7 @@ public AntiCaptchaApi(string userKey, ProviderOptions options)
_options = options;
}

public Task<AntiCaptchaTaskResult> CreateTaskAsync(string pageUrl, string key, CancellationToken token = default)
public async Task<AntiCaptchaTaskResult> CreateTaskAsync(string pageUrl, string key, CancellationToken token = default)
{
var content = new AntiCaptchaRequest()
{
Expand All @@ -32,8 +33,8 @@ public Task<AntiCaptchaTaskResult> CreateTaskAsync(string pageUrl, string key, C



var result = _client.PostWithJsonAsync<AntiCaptchaTaskResult>("createTask", content, token);
return result;
var result = await _client.PostWithJsonAsync<JObject>("createTask", content, token);
return result.ToObject<AntiCaptchaTaskResult>();
}


Expand Down
19 changes: 9 additions & 10 deletions PuppeteerExtraSharp/Plugins/Recaptcha/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PuppeteerExtraSharp.Utils;
using RestSharp;

namespace PuppeteerExtraSharp.Plugins.Recaptcha.RestClient
{
public class RestClient
public class RestClient(string url = null)
{
private readonly RestSharp.RestClient _client;

public RestClient(string url = null)
{
_client = string.IsNullOrWhiteSpace(url) ? new RestSharp.RestClient() : new RestSharp.RestClient(url);
}
private readonly RestSharp.RestClient _client = string.IsNullOrWhiteSpace(url) ? new RestSharp.RestClient() : new RestSharp.RestClient(url);

public PollingBuilder<T> CreatePollingBuilder<T>(RestRequest request)
{
return new PollingBuilder<T>(_client, request);
}

public async Task<T> PostWithJsonAsync<T>(string url, object content, CancellationToken token)
public async Task<T> PostWithJsonAsync<T>(string url, object content, CancellationToken token) where T : JToken
{
var request = new RestRequest(url);
request.AddHeader("Content-type", "application/json");
request.AddJsonBody(content);
request.AddJsonBody(JsonConvert.SerializeObject(content));
request.Method = Method.Post;
return await _client.PostAsync<T>(request, token);

var response = await _client.PostAsync(request, token);
return JsonConvert.DeserializeObject<T>(response.Content);
}

public async Task<T> PostWithQueryAsync<T>(string url, Dictionary<string, string> query, CancellationToken token = default)
Expand Down
4 changes: 2 additions & 2 deletions PuppeteerExtraSharp/PuppeteerExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PuppeteerExtraSharp
{
public class PuppeteerExtra
{
private List<PuppeteerExtraPlugin> _plugins = new List<PuppeteerExtraPlugin>();
private List<PuppeteerExtraPlugin> _plugins = [];

public PuppeteerExtra Use(PuppeteerExtraPlugin plugin)
{
Expand Down Expand Up @@ -76,7 +76,7 @@ private void ResolveDependencies(PuppeteerExtraPlugin plugin)

private void OrderPlugins()
{
_plugins = _plugins.OrderBy(e => e.Requirements?.Contains(PluginRequirements.RunLast)).ToList();
_plugins = [.. _plugins.OrderBy(e => e.Requirements?.Contains(PluginRequirements.RunLast))];
}

private void CheckPluginRequirements(BrowserStartContext context)
Expand Down
41 changes: 35 additions & 6 deletions PuppeteerExtraSharp/PuppeteerExtraSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Version>1.3.1</Version>
<RepositoryUrl>https://github.com/Overmiind/Puppeteer-sharp-extra</RepositoryUrl>
<Version>1.4.2</Version>
<RepositoryUrl>https://github.com/VaKKuumDEV/Puppeteer-sharp-extra</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>puppeteer-extra recaptcha browser-automation browser-extension browser puppeteer netcore netcore31 stealth-client browser-testing c#</PackageTags>
<PackageProjectUrl>https://github.com/Overmiind/Puppeteer-sharp-extra</PackageProjectUrl>
<PackageProjectUrl>https://github.com/VaKKuumDEV/Puppeteer-sharp-extra</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<UserSecretsId>945328fb-3e7e-4518-99f8-ec578bf688b1</UserSecretsId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>VaKKuum.PuppeteerExtraSharpJune</Title>
<Authors>VaKKuum</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageId>VaKKuum.PuppeteerExtraSharp</PackageId>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -53,8 +58,32 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PuppeteerSharp" Version="9.0.2" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PuppeteerSharp" Version="18.0.3" />
<PackageReference Include="RestSharp" Version="111.3.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions PuppeteerExtraSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ await page.ScreenshotAsync("extra.png");
💀[Puppeteer recaptcha plugin](https://github.com/Overmiind/PuppeteerExtraSharp/tree/master/Plugins/Recaptcha)
- Solves recaptcha automatically

🌐 [Puppeteer proxies plugin](https://github.com/VaKKuumDEV/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/Proxies)
- Applies proxies settings on every page
- TODO: rotation and whitelist


✋**More plugins will be soon**
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PuppeteerExtraSharp

[![NuGet Badge](https://buildstats.info/nuget/PuppeteerExtraSharp)](https://www.nuget.org/packages/PuppeteerExtraSharp)
[![NuGet Badge](https://buildstats.info/nuget/VaKKuum.PuppeteerExtraSharp)](https://www.nuget.org/packages/VaKKuum.PuppeteerExtraSharp/)

Puppeteer extra sharp is a .NET port of the [Node.js library](https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra)
## Quickstart
Expand Down Expand Up @@ -31,20 +31,21 @@ await page.ScreenshotAsync("extra.png");
```
## Plugin list

🏴 [Puppeteer stealth plugin](https://github.com/Overmiind/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/ExtraStealth)
🏴 [Puppeteer stealth plugin](https://github.com/Overmiind/PuppeteerExtraSharp/tree/master/Plugins/ExtraStealth)
- Applies various evasion techniques to make detection of headless puppeteer harder.

📃 [Puppeteer anonymize UA plugin](https://github.com/Overmiind/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/AnonymizeUa)
📃 [Puppeteer anonymize UA plugin](https://github.com/Overmiind/PuppeteerExtraSharp/tree/master/Plugins/AnonymizeUa)
- Anonymizes the user-agent on all pages.

💀[Puppeteer recaptcha plugin](https://github.com/Overmiind/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/Recaptcha)
💀[Puppeteer recaptcha plugin](https://github.com/Overmiind/PuppeteerExtraSharp/tree/master/Plugins/Recaptcha)
- Solves recaptcha automatically

🔧[Puppeteer block resources plugin](https://github.com/Overmiind/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/BlockResources)
- Blocks images, documents etc.
🌐 [Puppeteer proxies plugin](https://github.com/VaKKuumDEV/Puppeteer-sharp-extra/tree/master/PuppeteerExtraSharp/Plugins/Proxies)
- Applies proxies settings on every page
- TODO: rotation and whitelist


✋**More plugins coming soon**
✋**More plugins will be soon**
## API

#### Use(IPuppeteerExtraPlugin)
Expand Down
Loading