如何在php中使用ssh作为sock5与curl?

时间:2015-11-19 18:58:09

标签: php curl

$connection = ssh2_connect($ip_ssh, 22);

if (ssh2_auth_password($connection,$username, $password)) {
    if ($tunnel = ssh2_tunnel($connection, '127.0.0.1', 443)) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io");
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:443");
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

        $result = curl_exec($ch);   

        echo $result;
    } else {
        echo "Tunnel creation failed.\n";
    }
} else {
    echo "failed;
}

我试过但它没有用。感谢您的关注 ............................................. ..........................................

1 个答案:

答案 0 :(得分:0)

您不能将SSH隧道用作具有cURL的SOCKS代理,因为隧道不代表SOCKS连接。

ssh2_tunnel返回的资源是套接字流(类似fsockopen返回的那个)。

即使远程SSH服务器上有SOCKS代理(例如Tor),我也不认为有办法让本地服务器上的cURL通过远程服务器上的SOCKS代理使用cURL代理选项的任意组合在隧道上。

以下是使用隧道的示例:

<?php

$ip_ssh      = 'ssh_host';
$username    = 'user';
$password    = 'password';
$remote_host = 'ipinfo.io';

$connection = ssh2_connect($ip_ssh, 22);

$addr = gethostbyname($remote_host);

if (ssh2_auth_password($connection,$username, $password)) {
    if ($tunnel = ssh2_tunnel($connection, $addr, 80)) {
        $request = "GET / HTTP/1.0\r\n" .
                   "Host: $remote_host\r\n" .
                   "Connection: close\r\n\r\n";

        fwrite($tunnel, $request);

        $result = '';

        while (!feof($tunnel)) {
            $result .= fgets($tunnel);
        }

        echo $result;
    } else {
        echo "Tunnel creation failed.\n";
    }
} else {
    echo "failed";
}