Curl through intermediate server

I need to send PUT/GET/POST request with curl through a custom port from client1 to server2. However, server2 only accepts these requests from server1, and denies client1 if connected directly. What would be the best way to tunnel curl requests from client1 to server2 through server1.

In case of SSH, for example, I do this by creating a tunnel. So I am wondering whether a similar functionality exists for curl.


Solution 1:

Curl doesn't have this functionality, and in any case it would have to be a server option. In the case of HTTP, this means configuring a proxy of some kind. If you can't modify the server configuration or install a proxy, but you can SSH, then SSH is your only option.

To tunnel port 80 to your local machine via server1:

ssh -L 8080:server2:80 user@server1

Then curl localhost:8080 will send a request to port 80 on server2.

Note; some server configurations will deny port forwarding to some or all users.

Solution 2:

You could also use a oneliner, this saves you from having to maintain a tunnel in another process.

ssh user@server1 curl http://server2/path -o -

On other words, curl doesn't support ssh tunnels, but ssh tunnels do support curl (sort of)...