Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you proxy though a server using ssh (socks…) using php’s CURL?

Tags:

php

curl

proxy

I want to use ssh, something like this:

ssh -D 9999 username@ip-address-of-ssh-server

But within php CURL, but I don't really see how this could be done?

I noticed “CURLPROXY_SOCKS5” as a type in the php site, but guess that wouldn’t work since it isn’t really socks, it’s ssh…

I’m currently using this code:

curl_setopt($ch, CURLOPT_PROXY, ‘ip:port'); 

But I'm using a free proxy and it’s rather slow and unreliable, I'm also sending sensitive information over this proxy. This is why I want to proxy it over a save server I trust, but I only have ssh setup on it and it’s unable to host a proper proxy.

like image 876
Mint Avatar asked Feb 05 '10 09:02

Mint


1 Answers

You can use both libssh2 and curl from within a PHP script.

  • First you need to get the ssh2 library from the PECL site. Alternatively, the PEAR package has SSH2 support too.
  • After installing you can then read the ssh2 documentation on setting up a tunnel.
  • In your script you can then set up the tunnel.
  • After the tunnel is set up in the script you can specify the CURL proxy.
  • Perform your CURL operation.
  • Release the tunnel resource and close the connection in your script.

I'm not a PHP expert, but here's a rough example:

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_pubkey_file($connection, 'username', 'id_dsa.pub', 'id_dsa');
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 9999);
curl_setopt($ch, CURLOPT_PROXY, ‘127.0.0.1:9999'); 
// perform curl operations

// The connection and tunnel will die at the and of the session.
?>

The simplest option

Another option to consider is using sftp (ftp over ssh) instead of CURL... this is probably the recommended way to copy a file from one server to another securely in PHP...

Even simpler example:

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>
like image 132
John Weldon Avatar answered Sep 30 '22 23:09

John Weldon