/
aprogrammer
/
reverse-proxy-ms-yarp
Обзор
Документация
Войти
/
aprogrammer
/
reverse-proxy-ms-yarp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/ReverseProxy.Direct.Sample/Program.cs
110 строк
5 KB
Miha Zupan
Use consistent handler defaults in docs and samples (#2451)
02 апр 2024, 13:13
Не верифицирован
02 апр 2024, 13:13
83c3607
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Diagnostics; using System.Net; using System.Net.Http; using System.Threading.Tasks; using System.Threading; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Yarp.ReverseProxy.Forwarder; using Yarp.ReverseProxy.Transforms; var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpForwarder(); var app = builder.Build(); // Configure our own HttpMessageInvoker for outbound calls for proxy operations var httpClient = new HttpMessageInvoker(new SocketsHttpHandler { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, UseCookies = false, EnableMultipleHttp2Connections = true, ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current), ConnectTimeout = TimeSpan.FromSeconds(15), }); // Setup our own request transform class var transformer = new CustomTransformer(); // or HttpTransformer.Default; var requestOptions = new ForwarderRequestConfig { ActivityTimeout = TimeSpan.FromSeconds(100) }; app.UseRouting(); // When using IHttpForwarder for direct forwarding you are responsible for routing, destination discovery, load balancing, affinity, etc.. // For an alternate example that includes those features see BasicYarpSample. app.Map("/test/{**catch-all}", async (HttpContext httpContext, IHttpForwarder forwarder) => { var error = await forwarder.SendAsync(httpContext, "https://example.com", httpClient, requestOptions, static (context, proxyRequest) => { // Customize the query string: var queryContext = new QueryTransformContext(context.Request); queryContext.Collection.Remove("param1"); queryContext.Collection["area"] = "xx2"; // Assign the custom uri. Be careful about extra slashes when concatenating here. RequestUtilities.MakeDestinationAddress is a safe default. proxyRequest.RequestUri = RequestUtilities.MakeDestinationAddress("https://example.com", context.Request.Path, queryContext.QueryString); // Suppress the original request header, use the one from the destination Uri. proxyRequest.Headers.Host = null; return default; }); // Check if the proxy operation was successful if (error != ForwarderError.None) { var errorFeature = httpContext.Features.Get<IForwarderErrorFeature>(); var exception = errorFeature.Exception; } }); app.MapForwarder("/sample/{id}", "https://httpbin.org", "/anything/{id}"); app.MapForwarder("/sample/anything/{id}", "https://httpbin.org", b => b.AddPathRemovePrefix("/sample")); // When using extension methods for registering IHttpForwarder providing configuration, transforms, and HttpMessageInvoker is optional (defaults will be used). app.MapForwarder("/{**catch-all}", "https://example.com", requestOptions, transformer, httpClient); app.Run(); /// <summary> /// Custom request transformation /// </summary> internal class CustomTransformer : HttpTransformer { /// <summary> /// A callback that is invoked prior to sending the proxied request. All HttpRequestMessage /// fields are initialized except RequestUri, which will be initialized after the /// callback if no value is provided. The string parameter represents the destination /// URI prefix that should be used when constructing the RequestUri. The headers /// are copied by the base implementation, excluding some protocol headers like HTTP/2 /// pseudo headers (":authority"). /// </summary> /// <param name="httpContext">The incoming request.</param> /// <param name="proxyRequest">The outgoing proxy request.</param> /// <param name="destinationPrefix">The uri prefix for the selected destination server which can be used to create /// the RequestUri.</param> public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken) { // Copy all request headers await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken); // Customize the query string: var queryContext = new QueryTransformContext(httpContext.Request); queryContext.Collection.Remove("param1"); queryContext.Collection["area"] = "xx2"; // Assign the custom uri. Be careful about extra slashes when concatenating here. RequestUtilities.MakeDestinationAddress is a safe default. proxyRequest.RequestUri = RequestUtilities.MakeDestinationAddress("https://example.com", httpContext.Request.Path, queryContext.QueryString); // Suppress the original request header, use the one from the destination Uri. proxyRequest.Headers.Host = null; } }