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(); }
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); }
/// <summary> /// Configure data portal client to use LocalProxy. /// </summary> /// <param name="config">CslaDataPortalConfiguration object</param> /// <param name="options">Data portal proxy options</param> public static CslaDataPortalConfiguration UseLocalProxy(this CslaDataPortalConfiguration config, Action <LocalProxyOptions> options) { var existingOptions = config.Services.Where(i => i.ServiceType.Equals(typeof(IDataPortalProxy))).FirstOrDefault(); if (existingOptions?.ImplementationInstance is not LocalProxyOptions proxyOptions) { proxyOptions = new LocalProxyOptions(); } options?.Invoke(proxyOptions); config.Services.AddTransient <IDataPortalProxy, LocalProxy>(); config.Services.AddTransient((p) => proxyOptions); return(config); }