2

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?

0

1 Answer 1

1

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

$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 a local port before requesting the tunnel to your ip / port through:

$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!";
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your code but server didn't response when I add this line "socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);". And how can I get $YourInternetIPaccessible in ubuntu terminal? Thanks.
Have you test your code yet? I tried but get error at this line "socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);".
What's the error, it can't create the listening socket ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.