I want using proxy/socks throw ssh server and i found nice example from here How do you proxy though a server using ssh (socks…) using php’s CURL? but example don't work, here is my code.
<?php
$connection = ssh2_connect("64.246.126.25", 22);
if(ssh2_auth_password($connection, "ubnt", "ubnt"))
{
if ($tunnel = ssh2_tunnel($connection, "127.0.0.1", 9999))
{
$url = "http://checkip.dyndns.com/";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9999");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$result = curl_exec($ch);
echo $result;
}else{
echo "Tunnel creation failed.\n";
}
}
else
{
echo "failed!";
}
?>
When i excute this script i have error response:
Warning: ssh2_tunnel() [function.ssh2-tunnel]: Unable to request a channel from remote host in /home/domain.com/ssh2/index.php on line 7
Tunnel creation failed.
I will googled for this error and trying fix but can't solve problems.
Anyone can help me?
It seems ssh_tunnel does not work like that, ssh_tunnel will request the "$connection" to perform a tunnel to the specified ip / port.
In your code i assume you are trying to create a local port which listens for the cURL requests as proxy, but basically you are asking the ssh server to connect to itself (127.0.0.1).
As you may see in my example, the tunnel to Google is only a stream through which you may send stream requests
You can open a local port before requesting the tunnel to your ip / port through:
And then your code with these changes: