How to change which IP address a go-fiber client is using?

670 Views Asked by At

I'm using Fiber as an HTTP client to make some requests to an http server, however I'm being rate limited. On my vm I configured 5 different IP addresses (public/private) and have confirmed that they are indeed connected to the internet.

curl --interface 10.0.0.4 ipinfo.io/json
curl --interface 10.0.0.5 ipinfo.io/json
... curl --interface 10.0.0.8 ipinfo.io/json

each one returns a different public facing ip address.

Now I'm interested in making round-robin requests using these local addresses but I'm not so sure how to go about it.

Is there some sort of property or function I can set/call to change where the outgoing request is coming from?

I've looked around at fasthttp.HostClient which fiber.Agent extends but I didn't see anything useful.

Thanks guys.

1

There are 1 best solutions below

0
On
a := fiber.AcquireAgent()
req := a.Request()
req.Header.SetMethod(fiber.MethodGet)
req.SetRequestURI(fmt.Sprintf(formatUrl, args...))

if err := a.Parse(); err != nil {
    h.Logger.Error("%v", err)
    return fiber.StatusInternalServerError, nil, []error{err}
}

customDialer := fasthttp.TCPDialer{
    Concurrency: 1000,
    LocalAddr: &net.TCPAddr{
        IP: h.IPPool[atomic.AddUint32(&h.IpIdx, 1)%uint32(len(h.IPPool))],
    },
}

a.HostClient.Dial = func(addr string) (net.Conn, error) {
    return customDialer.Dial(addr)
}

Creating a custom dialer and dial func allows you to change the local address associated with the http request.