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

Allow using a Proxy #236

Merged
merged 12 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ src/packages
*.ide
/.vs/slnx.sqlite
/src/Coypu.Tests/Coypu.Tests.nuget.props
/src/Coypu.Tests
/src/Coypu.NUnit/Coypu.NUnit.nuget.props
/src/Coypu.NUnit
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.targets
/src/Coypu.Drivers.Tests/Coypu.Drivers.Tests.nuget.props
/src/Coypu.Drivers.Tests
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.targets
/src/Coypu.AcceptanceTests/Coypu.AcceptanceTests.nuget.props
/src/Coypu.AcceptanceTests/project.lock.json
Expand Down
7 changes: 7 additions & 0 deletions src/Coypu.Tests/Coypu.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
<PackageReference Include="WebDriverManager" Version="2.17.2" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Coypu\Coypu.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Update="proxysettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions src/Coypu.Tests/ProxyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Coypu.Drivers;
using Coypu.Drivers.Playwright;
using Coypu.Drivers.Selenium;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using WebDriverManager.DriverConfigs.Impl;

namespace Coypu.Tests;

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class ProxyTests
{
private WebApplication _app;
private string _proxyServer;

[SetUp]
public void SetUp()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddJsonFile("proxysettings.json");

builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

_app = builder.Build();

_app.UseRouting();
_app.MapReverseProxy();

_app.RunAsync();

_proxyServer = _app.Services.GetRequiredService<IServer>()
.Features.Get<IServerAddressesFeature>()
.Addresses
.First();
}

[TearDown]
public async Task TearDown()
{
await _app.DisposeAsync();
}

[TestCase(typeof(PlaywrightDriver))]
[TestCase(typeof(SeleniumWebDriver))]
public void Driver_Uses_Proxy(Type driverType)
{
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
var sessionConfiguration = new SessionConfiguration
{
AcceptInsecureCertificates = true,
Proxy = new DriverProxy
{
Server = _proxyServer,
},
Browser = Browser.Chrome,
Driver = driverType
};

using var browser = new BrowserSession(sessionConfiguration);

// Proxy turns this example.com into github.com
browser.Visit("http://www.example.com");

// So we then assert we can find the GitHub Octo Icon
var icon = browser.FindCss(".octicon-mark-github");
Copy link
Collaborator

@sbowler sbowler Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned this could break easily if they update their website. Not sure what would be a good alternative though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have updated to render a page from localhost instead so we have full control


Assert.That(icon.Exists(), Is.True);
}
}
41 changes: 41 additions & 0 deletions src/Coypu.Tests/proxysettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"route1" : {
"ClusterId": "cluster1",
"Match": {
"Path": "{**catch-all}",
"Hosts": ["www.example.com"]
}
},
"route2" : {
"ClusterId": "cluster2",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"cluster1": {
"Destinations": {
"destination1": {
"Address": "https://github.com/"
}
}
},
"cluster2": {
"Destinations": {
"destination1": {
"Address": "https://*/"
}
}
}
}
}
}
40 changes: 40 additions & 0 deletions src/Coypu/DriverProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;

namespace Coypu
{
/// <summary>
/// Proxy information for a Driver
/// </summary>
public class DriverProxy
{
/// <summary>
/// The Username for the proxy
/// </summary>
public string Username { get; set; }

/// <summary>
/// The Password for the proxy
/// </summary>
public string Password { get; set; }

/// <summary>
/// The Server of the proxy
/// </summary>
public string Server { get; set; }

/// <summary>
/// Use proxy for SSL
/// </summary>
public bool Ssl { get; set; } = true;

/// <summary>
/// Use type of proxy
/// </summary>
public DriverProxyType Type { get; set; } = DriverProxyType.Http;

/// <summary>
/// Domains to bypass
/// </summary>
public IEnumerable<string> BypassAddresses { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/Coypu/DriverProxyType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Coypu
{
public enum DriverProxyType
{
Socks,
Http
}
}
32 changes: 26 additions & 6 deletions src/Coypu/Drivers/Playwright/PlaywrightDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text.RegularExpressions;
using Cookie = System.Net.Cookie;
using Microsoft.Playwright;
using System.Collections.Immutable;

#pragma warning disable 1591

Expand All @@ -27,20 +26,41 @@ public PlaywrightDriver(SessionConfiguration sessionConfiguration)
_browser = sessionConfiguration.Browser;
_headless = sessionConfiguration.Headless;
var browserType = PlaywrightBrowserType(_browser, _playwright); // TODO: map browser to playwright browser type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointless whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run a dotnet format whitespace

_playwrightBrowser = browserType.LaunchAsync(
new BrowserTypeLaunchOptions
{
Headless = _headless,
Channel = PlaywrightBrowserChannel(_browser),
{
Proxy = MapProxy(sessionConfiguration.Proxy),
Headless = _headless,
Channel = PlaywrightBrowserChannel(_browser),
}
).Sync();
NewContext(sessionConfiguration);
}

private Proxy MapProxy(DriverProxy proxy)
{
if (proxy is null)
{
return null;
}

return new Proxy
{
Username = proxy.Username,
Password = proxy.Password,
Server = proxy.Server,
Bypass = proxy.BypassAddresses == null ? null : string.Join(',', proxy.BypassAddresses)
};
}

private void NewContext(SessionConfiguration sessionConfiguration)
{
var options = new BrowserNewPageOptions();
var options = new BrowserNewPageOptions
{
IgnoreHTTPSErrors = sessionConfiguration.AcceptInsecureCertificates
};

if (!string.IsNullOrEmpty(sessionConfiguration.AppHost) && !string.IsNullOrEmpty(sessionConfiguration.UserInfo))
{
if (!string.IsNullOrEmpty(sessionConfiguration.UserInfo)) {
Expand Down
Loading