twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
505 stars 80 forks source link

Does the WS Proxy have a WithHTTPClient-like capability? #108

Closed upsampled closed 1 month ago

upsampled commented 1 year ago

I am attempting to port go code using AspNetCore.Proxy. The basic function being ported is to redirect http and ws connections through Unix Sockets. For typical HTTP proxying I am attempting this with something like:

builder.Services.AddHttpClient("MyUnixHandler").ConfigurePrimaryHttpMessageHandler(x =>new SocketsHttpHandler
{
    ConnectCallback = async (context, token) =>
    {
        var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
        var endpoint = new UnixDomainSocketEndPoint(UnixSocketPath);
        await socket.ConnectAsync(endpoint).ConfigureAwait(false);
        return new NetworkStream(socket, ownsSocket: false);
    }
});
...
RunProxy(proxy => proxy

    .UseHttp((context, args) =>
    {
       return context.Request.Path; //do nothing
    },
    builder => builder.WithHttpClientName("MyUnixHandler")));

The issue I am having is that I would also like to support WebSockets the same way, but the websocket proxy builder does not have anything like the WithHttpClientName method. Is there any way mimic this behavior for websockets? Can/should I try to switch both HTTP and WS Proxies over to WithIntercept handlers?

twitchax commented 1 year ago

Honestly, WithIntercept might be a good solution. However, we could probably make a change that would allow you to pass in a custom WebSocketClient, but it wouldn't work in the same way because there is nothing like builder.Services.AddWsClient.

Therefore, it would just be something like WithCustomWsClient(clientGenerator: Func<IWsClient>), though I don't think IWsClient exists.

upsampled commented 1 year ago

I think the WithCustomWsClient is a worth while feature. Unix Sockets may be a bit exotic but this would also allow for WebSockets to be proxied to HTTP3 Web Transport over QUIC. You would think this would become a more popular use-case as HTTP3 starts to roll out.