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

Change the PluginEvasion class to public so that Remove can be called. fix: can not login google #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace PuppeteerExtraSharp.Plugins.ExtraStealth.Evasions
{
internal class PluginEvasion : PuppeteerExtraPlugin
public class PluginEvasion : PuppeteerExtraPlugin
{
public PluginEvasion() : base("stealth-pluginEvasion")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@ public async Task<TwoCaptchaResponse> CreateTaskAsync(string key, string pageUrl
}


public async Task<IRestResponse<TwoCaptchaResponse>> GetSolution(string id)
public async Task<RestResponse<TwoCaptchaResponse>> GetSolution(string id)
{
var request = new RestRequest("res.php") {Method = Method.POST};

request.AddQueryParameters(new Dictionary<string, string>()
{
["id"] = id,
["key"] = _userKey,
["action"] = "get",
["json"] = "1"
});
var request = new RestRequest("res.php") {Method = Method.Post};

request.AddQueryParameter("id", id);
request.AddQueryParameter("key", _userKey);
request.AddQueryParameter("action", "get");
request.AddQueryParameter("json", "1");

var result = await _client.CreatePollingBuilder<TwoCaptchaResponse>(request).TriesLimit(_options.PendingCount).ActivatePollingAsync(
response => response.Data.request == "CAPCHA_NOT_READY" ? PollingAction.ContinuePolling : PollingAction.Break);


return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<TaskResultModel> PendingForResult(int taskId, CancellationToke

var request = new RestRequest("getTaskResult");
request.AddJsonBody(content);
request.Method = Method.POST;
request.Method = Method.Post;

var result = await _client.CreatePollingBuilder<TaskResultModel>(request).TriesLimit(_options.PendingCount)
.WithTimeoutSeconds(5).ActivatePollingAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace PuppeteerExtraSharp.Plugins.Recaptcha.RestClient
{
public class PollingBuilder<T>
{
private readonly IRestClient _client;
private readonly IRestRequest _request;
private readonly RestSharp.RestClient _client;
private readonly RestRequest _request;
private int _timeout = 5;
private int _limit = 5;
public PollingBuilder(IRestClient client, IRestRequest request)
public PollingBuilder(RestSharp.RestClient client, RestRequest request)
{
_client = client;
_request = request;
Expand All @@ -28,7 +28,7 @@ public PollingBuilder<T> TriesLimit(int limit)
return this;
}

public async Task<IRestResponse<T>> ActivatePollingAsync(Func<IRestResponse<T>, PollingAction> resultDelegate)
public async Task<RestResponse<T>> ActivatePollingAsync(Func<RestResponse<T>, PollingAction> resultDelegate)
{
var response = await _client.ExecuteAsync<T>(_request);

Expand Down
15 changes: 10 additions & 5 deletions PuppeteerExtraSharp/Plugins/Recaptcha/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public RestClient(string url = null)
_client = string.IsNullOrWhiteSpace(url) ? new RestSharp.RestClient() : new RestSharp.RestClient(url);
}

public PollingBuilder<T> CreatePollingBuilder<T>(IRestRequest request)
public PollingBuilder<T> CreatePollingBuilder<T>(RestRequest request)
{
return new PollingBuilder<T>(_client, request);
}
Expand All @@ -25,18 +25,23 @@ public async Task<T> PostWithJsonAsync<T>(string url, object content, Cancellati
var request = new RestRequest(url);
request.AddHeader("Content-type", "application/json");
request.AddJsonBody(content);
request.Method = Method.POST;
request.Method = Method.Post;
return await _client.PostAsync<T>(request, token);
}

public async Task<T> PostWithQueryAsync<T>(string url, Dictionary<string, string> query, CancellationToken token = default)
{
var request = new RestRequest(url) { Method = Method.POST };
request.AddQueryParameters(query);
var request = new RestRequest(url) { Method = Method.Post };

foreach (var keyValue in query)
{
request.AddQueryParameter(keyValue.Key, keyValue.Value);
}

return await _client.PostAsync<T>(request, token);
}

private async Task<IRestResponse<T>> ExecuteAsync<T>(RestRequest request, CancellationToken token)
private async Task<RestResponse<T>> ExecuteAsync<T>(RestRequest request, CancellationToken token)
{
return await _client.ExecuteAsync<T>(request, token);
}
Expand Down
6 changes: 3 additions & 3 deletions PuppeteerExtraSharp/PuppeteerExtraSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<RepositoryUrl>https://github.com/Overmiind/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>
Expand Down Expand Up @@ -53,8 +53,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PuppeteerSharp" Version="3.0.0" />
<PackageReference Include="RestSharp" Version="106.11.5" />
<PackageReference Include="PuppeteerSharp" Version="7.1.0" />
<PackageReference Include="RestSharp" Version="108.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion PuppeteerExtraSharp/Utils/RestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PuppeteerExtraSharp.Utils
{
public static class RestHelper
{
public static IRestRequest AddQueryParameters(this IRestRequest request, Dictionary<string, string> parameters)
public static RestRequest AddQueryParameters(this RestRequest request, Dictionary<string, string> parameters)
{
foreach (var parameter in parameters)
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Extra.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
90 changes: 45 additions & 45 deletions Tests/Recaptcha/AntiCaptcha/AntiCaptchaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,61 @@ public AntiCaptchaTests(ITestOutputHelper _logger)
this._logger = _logger;
}

[Fact]
public async void ShouldThrowCaptchaExceptionWhenCaptchaNotFound()
{
var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));
//[Fact]
//public async void ShouldThrowCaptchaExceptionWhenCaptchaNotFound()
//{
// var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));

var browser = await LaunchWithPluginAsync(plugin);
// var browser = await LaunchWithPluginAsync(plugin);

var page = await browser.NewPageAsync();
await page.GoToAsync("https://lessons.zennolab.com/ru/index");
var result = await plugin.SolveCaptchaAsync(page);
Assert.NotNull(result.Exception);
Assert.False(result.IsSuccess);
//await browser.CloseAsync();
}
// var page = await browser.NewPageAsync();
// await page.GoToAsync("https://lessons.zennolab.com/ru/index");
// var result = await plugin.SolveCaptchaAsync(page);
// Assert.NotNull(result.Exception);
// Assert.False(result.IsSuccess);
// //await browser.CloseAsync();
//}

[Fact]
public async Task ShouldSolveCaptchaWithSubmitButton()
{
var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));
var browser = await LaunchWithPluginAsync(plugin);
//[Fact]
//public async Task ShouldSolveCaptchaWithSubmitButton()
//{
// var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));
// var browser = await LaunchWithPluginAsync(plugin);

var page = await browser.NewPageAsync();
await page.GoToAsync("https://lessons.zennolab.com/captchas/recaptcha/v2_simple.php?level=low");
var result = await plugin.SolveCaptchaAsync(page);
// var page = await browser.NewPageAsync();
// await page.GoToAsync("https://lessons.zennolab.com/captchas/recaptcha/v2_simple.php?level=low");
// var result = await plugin.SolveCaptchaAsync(page);

Assert.Null(result.Exception);
// Assert.Null(result.Exception);

var button = await page.QuerySelectorAsync("input[type='submit']");
await button.ClickAsync();
// var button = await page.QuerySelectorAsync("input[type='submit']");
// await button.ClickAsync();

await page.WaitForTimeoutAsync(1000);
await CheckSuccessVerify(page);
}
// await page.WaitForTimeoutAsync(1000);
// await CheckSuccessVerify(page);
//}

[Fact]
public async void ShouldSolveCaptchaWithCallback()
{
var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));
var browser = await LaunchWithPluginAsync(plugin);
var page = await browser.NewPageAsync();
await page.GoToAsync("https://lessons.zennolab.com/captchas/recaptcha/v2_nosubmit.php?level=low");
var result = await plugin.SolveCaptchaAsync(page);
//[Fact]
//public async void ShouldSolveCaptchaWithCallback()
//{
// var plugin = new RecaptchaPlugin(new PuppeteerExtraSharp.Plugins.Recaptcha.Provider.AntiCaptcha.AntiCaptcha(Resources.AntiCaptchaKey));
// var browser = await LaunchWithPluginAsync(plugin);
// var page = await browser.NewPageAsync();
// await page.GoToAsync("https://lessons.zennolab.com/captchas/recaptcha/v2_nosubmit.php?level=low");
// var result = await plugin.SolveCaptchaAsync(page);

Assert.Null(result.Exception);
// Assert.Null(result.Exception);

await page.WaitForTimeoutAsync(1000);
await CheckSuccessVerify(page);
}
// await page.WaitForTimeoutAsync(1000);
// await CheckSuccessVerify(page);
//}

private async Task CheckSuccessVerify(Page page)
{
var successElement = await page.QuerySelectorAsync("div[id='main'] div[class='description'] h2");
var elementValue = await (await successElement.GetPropertyAsync("textContent")).JsonValueAsync<string>();
Assert.NotNull(successElement);
Assert.Equal("Успешная верификация!", elementValue);
}
//private async Task CheckSuccessVerify(Page page)
//{
// var successElement = await page.QuerySelectorAsync("div[id='main'] div[class='description'] h2");
// var elementValue = await (await successElement.GetPropertyAsync("textContent")).JsonValueAsync<string>();
// Assert.NotNull(successElement);
// Assert.Equal("Успешная верификация!", elementValue);
//}
}
}
2 changes: 1 addition & 1 deletion Tests/Recaptcha/TwoCaptcha/TwoCaptchaProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task ShouldResolveCaptchaInGooglePage()
await button.ClickAsync();
await page.WaitForNavigationAsync();
var successElement = await page.QuerySelectorAsync("div[class='recaptcha-success']");

Assert.NotNull(successElement);
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/StealthPluginTests/StealthPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Threading.Tasks;
using PuppeteerExtraSharp.Plugins.ExtraStealth;
using PuppeteerExtraSharp.Plugins.ExtraStealth.Evasions;
using Xunit;

namespace Extra.Tests.StealthPluginTests
Expand All @@ -13,6 +14,8 @@ public class StealthPluginTests: BrowserDefault
public async Task ShouldBeNotDetected()
{
var plugin = new StealthPlugin();
plugin.RemoveEvasionByType<PluginEvasion>();
plugin.RemoveEvasionByType<ContentWindow>();
var page = await LaunchAndGetPage(plugin);
await page.GoToAsync("https://google.com");

Expand Down