fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🔥 Feature: Add custom http proxy for http client in proxy middleware

Open jj0e opened this issue 3 years ago • 9 comments

Description

The proxy middleware is a great way to make your server act as a proxy in order to make changes to requests, etc. However, it lacks the ability to control the dialer of http client directly specifically for using http proxies. What do I mean by this?

Right now, this diagram can be used to describe the middleware:

Proxy in this case is the fiber server using the proxy middleware. Lets say the IP address of Proxy is at example.com:1337, when Bob receives the proxy request via Alice, the IP address that he will see is example.com:1337, not the IP address of Alice. This presents an issue for those who are handling a high load of requests and proxying them to other sites which may contain rate limiting or web application firewalls resulting in 429 Too Many Requests errors.

This error is not at the fault of Alice but rather Proxy. It may seem to be an odd or rare solution, however in the market that I'm operating, it is common practice to implement another proxy layer. The logic is similar to the already implemented load balancer, however there are certain cases where even the load balancer is not enough to handle the load. In this case, we require the user to send a custom HTTP proxy in their request (header, cookie, or payload). We then take this proxy and use it inside of the proxy middleware to spoof the IP address of the server. This can be demonstrated like so:

This pull requests allows us to create another proxy layer in between Proxy and Bob. There are no added dependencies. It is possible to implement a custom HTTP proxy via Fasthttp's fasthttpproxy package which is already implemented inside of Fasthttp. It edits the clients Dial which in the case of this middleware is not being used for anything else.

Usage

Without authentication

proxy.WithHttpProxy("example.com:1337")

// Will use example.com:1337 as the request IP instead of the server IP
if err := proxy.Do(c, url); err != nil {
    log.Fatal(err)
}

With user/pass authentication

proxy.WithHttpProxy("user:[email protected]:1337")

// Will use user:[email protected]:1337 as the request IP instead of the server IP
if err := proxy.Do(c, url); err != nil {
    log.Fatal(err)
}

jj0e avatar Aug 10 '21 01:08 jj0e

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Aug 10 '21 01:08 welcome[bot]

@jj0e could you check my last comment

ReneWerner87 avatar Aug 13 '21 06:08 ReneWerner87

@jj0e could you check my last comment

I'm having trouble creating a reverse proxy to use in the test. I don't have much time to work on this although I did want to get the ball rolling. I will get to it ASAP

jj0e avatar Aug 13 '21 21:08 jj0e

Please check the existing tests https://github.com/gofiber/fiber/blob/master/middleware/proxy/proxy_test.go#L31

ReneWerner87 avatar Aug 13 '21 23:08 ReneWerner87

@jj0e Please check the existing tests https://github.com/gofiber/fiber/blob/master/middleware/proxy/proxy_test.go#L31

and adapt them for the new tests

ReneWerner87 avatar Aug 23 '21 06:08 ReneWerner87

@jj0e please check my review comments

ReneWerner87 avatar Sep 28 '21 08:09 ReneWerner87

@jj0e any progress here ?

ReneWerner87 avatar Dec 28 '21 13:12 ReneWerner87

No, I have looked into implementing tests and I'm stuck. I do not know how to correctly implement.

jj0e avatar Dec 28 '21 17:12 jj0e

No, I have looked into implementing tests and I'm stuck. I do not know how to correctly implement.

It looks like we need to create simple tunnel proxy to create unit tests. I'll try to do it weekend.

efectn avatar Apr 20 '22 21:04 efectn

Overridden by https://github.com/gofiber/fiber/pull/2117

efectn avatar Sep 26 '22 06:09 efectn