I’ve got an interface with multiple IP addresses and I’m looking to
make HTTP requests from the non-primary IP.
How would I pull that off?
In principle, you ‘bind’ the socket to the correct local IP address
Unfortunately, TCPSocket#open (or #new) does the connect before it gives
a chance to bind. So you’ll need to replace TCPSocket.open with
messy along the lines of
s = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM,
s.bind(Socket.pack_sockaddr_in(0, “126.96.36.199”)) # chosen local IP
Test this and check that it works at the TCP layer, e.g. by adding
s.write("GET / HTTP/1.0\r\n\r\n") puts s.read
If that works, then unfortunately net/http.rb doesn’t seem to have a
suitable hook, so copy the source and look for TCPSocket.open to see
you need to modify it.