twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
525 stars 83 forks source link

Is there a way to use this to proxy all requests? #28

Closed pkellner closed 4 years ago

pkellner commented 4 years ago

That is, something like everything coming into localhost/api goes to myserver/ ?

twitchax commented 4 years ago

Yes!

You could use {**catchall}.

app.UseProxy("api/{**catchall}", async (args) => {
    return $"http://myserver/{args["catchall"]}";
});

You could also do it in a controller if you use the ** catchall. All of the routes use the ASP.NET standard routing, and the catchall is one of the built-in helpers.

twitchax commented 4 years ago

@pkellner, this work for you?

waxingsatirical commented 4 years ago

With the UseProxy extension the query string parameters are lost by default. I've ended up using something like this:

app.UseProxy("api/{**catchall}", async (context, args) => {
    return $"http://myserver/{args["catchall"]}{content.Request.QueryString.ToUriComponent()}";
});

Is that the best way to do it? I feel like it would have been better if the query string was appended by default, and maybe there was an overload of getProxiedAddress which had the querystring as a parameter in case anyone wanted to do logic on it.

twitchax commented 4 years ago

@waxingsatirical, good catch.

I am also in the midst of a large redesign, and I am adding a RunProxy helper that will catch everything. So, that will be nice.

The problem with the "catchall" method right now is that it is an ASP.NET route type that I have no control over. Maybe they should add a ***whatever route that catches the whole path and query string.

An overload of getProxiedAddress is not a bad idea either. If you are interested, you seem to have dug around enough to help out on my redesign PR. When I finally get around to posting it, any interest in providing feedback?

waxingsatirical commented 4 years ago

Yes, I see that you are pretty much tied to the MapMiddleWareRoute extension and therefore you need to take the ASP.Net template as a parameter. Hmmn.

Happy to have a look, although I would not describe myself as a "power user". I just use this one method to proxy everything to another domain, tacking on a client certificate for authentication.

twitchax commented 4 years ago

@waxingsatirical, cool! Is that open sourced at all? Would be cool to have a good example of how you do the client certificate work. :)

waxingsatirical commented 4 years ago

Open source! No, we're not very progressive like that. I'm reluctant to share my implementation, because it's very heavy handed, but essentially you have to provide another implementation of the IHttpClientFactory and add the client certificate in the CreateClient method.

services.AddScoped(IHttpClientFactory, CertificateHttpClientFactory>();

twitchax commented 4 years ago

Haha, cool. Yeah. That is what I guessed you did.

glucaci commented 4 years ago
set http_proxy=http://myproxy
dotnet run

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy?view=netcore-3.1