PHP ssh2_tunnel: Use ssh proxy?

I want to use proxy / socks throw ssh server, and I found a good example from here How do you proxy using ssh server (socks ...) using phps CURL? but the example does not 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 clean this script, I have an 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 be googled for this error and try to fix it, but cannot solve the problem.

Can anybody help me?

+2
source share
1 answer

It seems that ssh_tunnel is not working like this, ssh_tunnel will ask for "$ connection" to run the tunnel with the specified ip / port.

In your code, I assume that you are trying to create a local port that listens for cURL requests as a proxy, but basically you ask the ssh server to connect to itself (127.0.0.1).

As you can see in my example, the tunnel in Google is just a stream through which you can send streaming requests

 $connection = ssh2_connect($sshServerIP, 22); ssh2_auth_password($connection, $sshServerUser, $sshServerPass); $sock = ssh2_tunnel($connection, "google.com", 80); fwrite($sock, "GET /? HTTP/1.0\r\n\r\n"); $body = ""; while (!feof($sock)) $body .= fgets($sock); echo $body; 

You can open the local port before requesting a tunnel for your ip / port via:

 $localsock = socket_create_listen(0); socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort); 

And then your code with these changes:

 $connection = ssh2_connect("64.246.126.25", 22); if(ssh2_auth_password($connection, "ubnt", "ubnt")) { if ($tunnel = ssh2_tunnel($connection, $YourInternetIPaccessible, $openedPort)) { $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:$openedPort"); curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); $result = curl_exec($ch); echo $result; }else{ echo "Tunnel creation failed.\n"; } } else { echo "failed!"; } 
+1
source

Source: https://habr.com/ru/post/1261772/


All Articles