How to connect to OpenVPN server via cURL?

2

Is it possible to use an OpenVPN server as a proxy in a cURL command?

I don't want to connect my entire computer to the OpenVPN. Instead, I want to use it in a single cURL command.

something like

curl -x http://user:password@OpenVPN:port url

but it doesn't work and returns

curl: (56) Recv failure: Connection reset by peer

My OS is Ubuntu 18.04

Googlebot

Posted 2019-08-04T07:54:15.057

Reputation: 846

Answers

2

No, it isn't.

First, the command doesn't work because you're telling cURL to speak a completely different protocol than the server speaks. OpenVPN and http:// are nothing alike – the server doesn't recognize any requests sent by cURL, and cURL doesn't understand any responses.

Second, it cannot work because cURL does not support any VPN protocols. There is no way to correctly specify that cURL should speak the OpenVPN protocol, or any other VPN protocol, because that's not in the program.

(In theory it's not impossible to add this to cURL, but it would massively increase complexity compared to "proxy" protocols, because cURL would need to learn TCP and IP in addition to learning about the VPNs themselves – i.e. it would need to duplicate much of of the network stack that resides in the OS.)


If you have root access to the system, you can use various mechanisms to limit what the OpenVPN connection is used for:

  • the regular IP routing table, to choose VPN usage based on destination IP address;
  • policy routing, to select the connection depending on source IP address (e.g. make it so that curl --interface tun0 would use the VPN but regular curl would not);
  • firewall rules, to select the connection depending on the protocol and ports used, and even based on user ID;
  • network namespaces, to create two different "worlds" where some processes see only the VPN while all other processes only see the original connection.

For example, if you want to limit VPN usage to just 10.0.0.0/8 (or vice versa, to exclude that network from the VPN) that's trivial to do using just OpenVPN's route options. For example, to limit the VPN to a specific network only:

route 10.0.0.0 255.0.0.0 vpn_gateway
route-nopull

And to exclude a specific network while using the VPN for everything else:

route 10.0.0.0 255.0.0.0 net_gateway

user1686

Posted 2019-08-04T07:54:15.057

Reputation: 283 655

Hey Gravvity, how would you go about policy routing? I want to be able to do exactly what you said, curl --interface tun0 https://website.com, but I don't know how. I've posted a question here (https://serverfault.com/questions/992624/vpn-client-doesnt-have-internet-connection) and it has reputation with your name on it :)

– Housemd – 2019-11-20T20:34:33.150

@Housemd: Plenty of existing threads on that topic - see e.g. https://superuser.com/a/1503160/1686 or search for ip rule.

– user1686 – 2019-11-20T21:27:02.420

Thanks, but that answer uses CentOS, I use Ubuntu, and I have no idea what is NetworkManager or network in Ubuntu. – Housemd – 2019-11-22T19:18:21.830