All Projects → twitchax → Aspnetcore.proxy

twitchax / Aspnetcore.proxy

Licence: mit
ASP.NET Core Proxies made easy.

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Aspnetcore.proxy

Spring Cloud Gateway
A Gateway built on Spring Framework 5.x and Spring Boot 2.x providing routing and more.
Stars: ✭ 3,305 (+1312.39%)
Mutual labels:  api-gateway, proxy
Proxykit
A toolkit to create code-first HTTP reverse proxies on ASP.NET Core
Stars: ✭ 1,063 (+354.27%)
Mutual labels:  aspnetcore, proxy
Annon.api
Configurable API gateway that acts as a reverse proxy with a plugin system.
Stars: ✭ 306 (+30.77%)
Mutual labels:  api-gateway, proxy
GatewayService
GatewayService (Ocelot).
Stars: ✭ 19 (-91.88%)
Mutual labels:  aspnetcore, api-gateway
Tree Gateway
This is a full featured and free API Gateway
Stars: ✭ 160 (-31.62%)
Mutual labels:  api-gateway, proxy
Manba
HTTP API Gateway
Stars: ✭ 3,000 (+1182.05%)
Mutual labels:  api-gateway, proxy
Graphqldockerproxy
A generic Graphql API for Docker and Kubernetes
Stars: ✭ 38 (-83.76%)
Mutual labels:  api-gateway, proxy
demo-serverless-aspnetcore
ASP.Net Core 3.1 on AWS Lambda demo
Stars: ✭ 22 (-90.6%)
Mutual labels:  aspnetcore, api-gateway
Dubbo Go Pixiu
Based on the proxy gateway service of dubbo-go, it solves the problem that the external protocol calls the internal Dubbo cluster. At present, it supports HTTP and gRPC[developing].
Stars: ✭ 124 (-47.01%)
Mutual labels:  api-gateway, proxy
Websockets
WebSockets Inception
Stars: ✭ 120 (-48.72%)
Mutual labels:  aspnetcore, proxy
Service Proxy
API gateway for REST and SOAP written in Java.
Stars: ✭ 355 (+51.71%)
Mutual labels:  api-gateway, proxy
Aws Lambda Fastify
Insipired by aws-serverless-express to work with Fastify with inject functionality.
Stars: ✭ 190 (-18.8%)
Mutual labels:  api-gateway, proxy
Esp V2
A service proxy that provides API management capabilities using Google Service Infrastructure.
Stars: ✭ 120 (-48.72%)
Mutual labels:  api-gateway, proxy
Janus
An API Gateway written in Go
Stars: ✭ 2,249 (+861.11%)
Mutual labels:  api-gateway, proxy
Goku Api Gateway
A Powerful HTTP API Gateway in pure golang!Goku API Gateway (中文名:悟空 API 网关)是一个基于 Golang开发的微服务网关,能够实现高性能 HTTP API 转发、服务编排、多租户管理、API 访问权限控制等目的,拥有强大的自定义插件系统可以自行扩展,并且提供友好的图形化配置界面,能够快速帮助企业进行 API 服务治理、提高 API 服务的稳定性和安全性。
Stars: ✭ 2,773 (+1085.04%)
Mutual labels:  api-gateway, proxy
Dgate
an API Gateway based on Vert.x
Stars: ✭ 222 (-5.13%)
Mutual labels:  api-gateway
Efcoresecondlevelcacheinterceptor
EF Core Second Level Cache Interceptor
Stars: ✭ 227 (-2.99%)
Mutual labels:  aspnetcore
Proxysu
Xray,V2ray,Trojan,NaiveProxy, Trojan-Go, ShadowsocksR(SSR),Shadowsocks-libev及相关插件,MTProto+TLS 一键安装工具,windows下用(一键科学上网)
Stars: ✭ 3,309 (+1314.1%)
Mutual labels:  proxy
Angularspawebapi
Angular Single Page Application with an ASP.NET Core Web API that uses token authentication
Stars: ✭ 222 (-5.13%)
Mutual labels:  aspnetcore
Chameleon
Customizable honeypots for monitoring network traffic, bots activities and username\password credentials (DNS, HTTP Proxy, HTTP, HTTPS, SSH, POP3, IMAP, STMP, RDP, VNC, SMB, SOCKS5, Redis, TELNET, Postgres and MySQL)
Stars: ✭ 230 (-1.71%)
Mutual labels:  proxy

AspNetCore.Proxy

Actions Status codecov GitHub Release NuGet Version NuGet Downloads

ASP.NET Core Proxies made easy.

Information

Install

dotnet add package AspNetCore.Proxy

Test

Download the source and run.

dotnet restore
dotnet test src/Test/AspNetCore.Proxy.Tests.csproj

Compatibility

.NET Standard 2.0 or .NET Core 3.0.

Examples

First, you must add the required services.

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddProxies();
    ...
}

Run a Proxy

You can run a proxy over all endpoints by using RunProxy in your Configure method.

app.RunProxy(proxy => proxy.UseHttp("http://google.com"));

In addition, you can route this proxy depending on the context. You can return a string or ValueTask<string> from the computer.

app.RunProxy(proxy => proxy
    .UseHttp((context, args) =>
    {
        if(context.Request.Path.StartsWithSegments("/should/forward/to/favorite"))
            return "http://myfavoriteserver.com";

        return "http://myhttpserver.com";
    })
    .UseWs((context, args) => "ws://mywsserver.com"));

Routes At Startup

You can define mapped proxy routes in your Configure method at startup.

app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());

app.UseProxies(proxies =>
{
    // Bare string.
    proxies.Map("echo/post", proxy => proxy.UseHttp("https://postman-echo.com/post"));

    // Computed to task.
    proxies.Map("api/comments/task/{postId}", proxy => proxy.UseHttp((_, args) => new ValueTask<string>($"https://jsonplaceholder.typicode.com/comments/{args["postId"]}")));

    // Computed to string.
    proxies.Map("api/comments/string/{postId}", proxy => proxy.UseHttp((_, args) => $"https://jsonplaceholder.typicode.com/comments/{args["postId"]}"));
});

Route At Startup with Custom HttpClientHandler

ASP.NET Core allows you to configure the behavior of its HTTP client objects by registering a named HttpClient with its own HttpClientHandler, which can then be referred to by name elsewhere. This can be used to support features such as server certificate custom validation. The UseProxies setup supports using such a named client:

proxies.Map( "/api/v1/...", proxy => proxy.UseHttp( 
    (context, args) => ...,
    builder => builder.WithHttpClientName("myClientName")));

...where "myClientName" was previously registered as:

services
    .AddHttpClient("myClientName")
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() {
        ServerCertificateCustomValidationCallback = MyValidateCertificateMethod,
        UseDefaultCredentials = true
    });

Existing Controller

You can define a proxy over a specific endpoint on an existing Controller by leveraging the ProxyAsync extension methods.

public class MyController : Controller
{
    [Route("api/posts/{postId}")]
    public Task GetPosts(int postId)
    {
        return this.HttpProxyAsync($"https://jsonplaceholder.typicode.com/posts/{postId}");
    }
}

NOTE: The body of the request should not be consumed by the controller (i.e., the controller should not have any [FromBody] parameters); otherwise, the proxy operation will fail. This is due to the fact that the body is read from a Stream, and that Stream is progressed when the body is read.

You can "catch all" using ASP.NET **rest semantics.

[Route("api/google/{**rest}")]
public Task ProxyCatchAll(string rest)
{
    // If you don't need the query string, then you can remove this.
    var queryString = this.Request.QueryString.Value;
    return this.HttpProxyAsync($"https://google.com/{rest}{queryString}");
}

In addition, you can proxy to WebSocket endpoints.

public class MyController : Controller
{
    [Route("ws")]
    public Task OpenWs()
    {
        return this.WsProxyAsync($"wss://mywsendpoint.com/ws");
    }
}

Uber Example

You can also pass special options that apply when the proxy operation occurs.

public class MyController : Controller
{
    private HttpProxyOptions _httpOptions = HttpProxyOptionsBuilder.Instance
        .WithShouldAddForwardedHeaders(false)
        .WithHttpClientName("MyCustomClient")
        .WithIntercept(async context =>
        {
            if(c.Connection.RemotePort == 7777)
            {
                c.Response.StatusCode = 300;
                await c.Response.WriteAsync("I don't like this port, so I am not proxying this request!");
                return true;
            }

            return false;
        })
        .WithBeforeSend((c, hrm) =>
        {
            // Set something that is needed for the downstream endpoint.
            hrm.Headers.Authorization = new AuthenticationHeaderValue("Basic");

            return Task.CompletedTask;
        })
        .WithAfterReceive((c, hrm) =>
        {
            // Alter the content in  some way before sending back to client.
            var newContent = new StringContent("It's all greek...er, Latin...to me!");
            hrm.Content = newContent;

            return Task.CompletedTask;
        })
        .WithHandleFailure(async (c, e) =>
        {
            // Return a custom error response.
            c.Response.StatusCode = 403;
            await c.Response.WriteAsync("Things borked.");
        }).Build();

    private WsProxyOptions _wsOptions = WsProxyOptionsBuilder.Instance
        .WithBufferSize(8192)
        .WithIntercept(context => new ValueTask<bool>(context.WebSockets.WebSocketRequestedProtocols.Contains("interceptedProtocol")))
        .WithBeforeConnect((context, wso) =>
        {
            wso.AddSubProtocol("myRandomProto");
            return Task.CompletedTask;
        })
        .WithHandleFailure(async (context, e) =>
        {
            context.Response.StatusCode = 599;
            await context.Response.WriteAsync("Failure handled.");
        }).Build();
    
    [Route("api/posts/{postId}")]
    public Task GetPosts(int postId)
    {
        return this.ProxyAsync("http://myhttpendpoint.com", "ws://mywsendpoint.com", _httpOptions, _wsOptions);
    }
}

License

The MIT License (MIT)

Copyright (c) 2017 Aaron Roney

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].