generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philippe Birbaum
authored
Jul 17, 2021
1 parent
8de252a
commit 4ee5cd8
Showing
11 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Boost.Web.Proxy | ||
{ | ||
public interface ILocalProxyServer : IDisposable | ||
{ | ||
Task<string> StartAsync(LocalProxyOptions options, CancellationToken cancellationToken); | ||
Task StopAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Boost.Web | ||
{ | ||
public class LocalProxyOptions | ||
{ | ||
public int Port { get; set; } | ||
|
||
public string DestinationAddress { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Boost.Infrastructure; | ||
using Boost.Web; | ||
using Boost.Web.Proxy; | ||
using McMaster.Extensions.CommandLineUtils; | ||
|
||
namespace Boost.Commands | ||
{ | ||
[Command( | ||
Name = "lp", | ||
FullName = "Local proxy", | ||
Description = "Starts a local proxy server")] | ||
public class LocalProxyCommand : CommandBase | ||
{ | ||
private readonly ILocalProxyServer _proxyServer; | ||
|
||
public LocalProxyCommand(ILocalProxyServer proxyServer) | ||
{ | ||
_proxyServer = proxyServer; | ||
} | ||
|
||
[Option("--port <PORT>", Description = "Webserver port")] | ||
public int Port { get; set; } = 5001; | ||
|
||
[Argument(0, "DestinationAddress", ShowInHelpText = true)] | ||
public string DestinationAddress { get; set; } = default!; | ||
|
||
public async Task OnExecute(IConsole console) | ||
{ | ||
console.WriteLine("Starting local proxy"); | ||
var port = NetworkExtensions.GetAvailablePort(Port); | ||
|
||
if (port != Port) | ||
{ | ||
console.WriteLine($"Port {Port} is allready in use.", ConsoleColor.Yellow); | ||
var useOther = Prompt.GetYesNo($"Start proxy on port: {port}", true); | ||
|
||
if (useOther) | ||
{ | ||
Port = port; | ||
} | ||
else | ||
{ | ||
return; | ||
} | ||
} | ||
var options = new LocalProxyOptions | ||
{ | ||
Port = Port, | ||
DestinationAddress = DestinationAddress | ||
}; | ||
|
||
string url = await _proxyServer.StartAsync( | ||
options, | ||
CommandAborded); | ||
|
||
ProcessHelpers.OpenBrowser(url); | ||
|
||
var stopMessage = "Press 'q' or 'esc' to stop"; | ||
console.WriteLine(stopMessage); | ||
|
||
while (true) | ||
{ | ||
ConsoleKeyInfo key = Console.ReadKey(); | ||
if (key.KeyChar == 'q' || key.Key == ConsoleKey.Escape) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
console.ClearLine(); | ||
console.WriteLine("Unknown command", ConsoleColor.Red); | ||
Console.WriteLine(stopMessage); | ||
} | ||
} | ||
|
||
console.WriteLine("Stopping proxy...."); | ||
await _proxyServer.StopAsync(); | ||
|
||
_proxyServer.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using Yarp.ReverseProxy.Abstractions; | ||
|
||
namespace Boost.Web.Proxy | ||
{ | ||
public class LocalProxyServer : ILocalProxyServer, IDisposable | ||
{ | ||
private IHost _host = default!; | ||
|
||
public async Task<string> StartAsync( | ||
LocalProxyOptions options, | ||
CancellationToken cancellationToken) | ||
{ | ||
var url = $"https://localhost:{options.Port}"; | ||
|
||
_host = Host.CreateDefaultBuilder() | ||
.UseSerilog() | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseUrls(url); | ||
webBuilder.UseStartup<Startup>(); | ||
}) | ||
.ConfigureServices((ctx, services) => | ||
{ | ||
ProxyRoute[]? routes = new[] | ||
{ | ||
new ProxyRoute() | ||
{ | ||
RouteId = "route1", | ||
ClusterId = "cluster1", | ||
Match = new RouteMatch | ||
{ | ||
Path = "{**catch-all}" | ||
} | ||
} | ||
}; | ||
|
||
Cluster[] clusters = new[] | ||
{ | ||
new Cluster() | ||
{ | ||
Id = "cluster1", | ||
Destinations = new Dictionary<string, Destination>(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
{ "destination1", new Destination() { Address = options.DestinationAddress} } | ||
} | ||
} | ||
}; | ||
|
||
services.AddReverseProxy() | ||
.LoadFromMemory(routes, clusters); | ||
}) | ||
|
||
.Build(); | ||
|
||
await _host.StartAsync(cancellationToken); | ||
|
||
return url; | ||
} | ||
|
||
public Task StopAsync() | ||
{ | ||
return _host.StopAsync(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_host is { }) | ||
{ | ||
_host.Dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Primitives; | ||
using Yarp.ReverseProxy.Abstractions; | ||
using Yarp.ReverseProxy.Service; | ||
|
||
namespace Boost.Web.Proxy | ||
{ | ||
public static class InMemoryConfigProviderExtensions | ||
{ | ||
public static IReverseProxyBuilder LoadFromMemory( | ||
this IReverseProxyBuilder builder, | ||
IReadOnlyList<ProxyRoute> routes, | ||
IReadOnlyList<Cluster> clusters) | ||
{ | ||
builder.Services.AddSingleton<IProxyConfigProvider>( | ||
new InMemoryConfigProvider(routes, clusters)); | ||
|
||
return builder; | ||
} | ||
} | ||
public class InMemoryConfigProvider : IProxyConfigProvider | ||
{ | ||
private volatile InMemoryConfig _config; | ||
|
||
public InMemoryConfigProvider( | ||
IReadOnlyList<ProxyRoute> routes, | ||
IReadOnlyList<Cluster> clusters) | ||
{ | ||
_config = new InMemoryConfig(routes, clusters); | ||
} | ||
|
||
public IProxyConfig GetConfig() => _config; | ||
|
||
public void Update(IReadOnlyList<ProxyRoute> routes, IReadOnlyList<Cluster> clusters) | ||
{ | ||
InMemoryConfig oldConfig = _config; | ||
_config = new InMemoryConfig(routes, clusters); | ||
oldConfig.SignalChange(); | ||
} | ||
|
||
private class InMemoryConfig : IProxyConfig | ||
{ | ||
private readonly CancellationTokenSource _cts = new CancellationTokenSource(); | ||
|
||
public InMemoryConfig(IReadOnlyList<ProxyRoute> routes, IReadOnlyList<Cluster> clusters) | ||
{ | ||
Routes = routes; | ||
Clusters = clusters; | ||
ChangeToken = new CancellationChangeToken(_cts.Token); | ||
} | ||
|
||
public IReadOnlyList<ProxyRoute> Routes { get; } | ||
|
||
public IReadOnlyList<Cluster> Clusters { get; } | ||
|
||
public IChangeToken ChangeToken { get; } | ||
|
||
internal void SignalChange() | ||
{ | ||
_cts.Cancel(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Boost.Web.Proxy | ||
{ | ||
public class Startup | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapReverseProxy(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters