gpt4 book ai didi

php - PHP ssh2_tunnel:使用代理/ socks 抛出ssh服务器?

转载 作者:行者123 更新时间:2023-12-02 14:04:07 31 4
gpt4 key购买 nike

我想使用代理/ socks 抛出ssh服务器,我从这里找到了很好的示例How do you proxy though a server using ssh (socks…) using php’s CURL?但示例不起作用,这是我的代码。

<?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!";
}
?>

当我执行此脚本时,我会收到错误响应:
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.

我将用Google搜索此错误并尝试修复,但无法解决问题。

有人可以帮助我吗?

最佳答案

似乎ssh_tunnel不能那样工作,ssh_tunnel将请求“$ connection”执行到指定ip /端口的隧道。

在您的代码中,我假设您正在尝试创建一个本地端口,以侦听cURL请求作为代理,但是基本上您是在要求ssh服务器连接到其自身(127.0.0.1)。

如您在我的示例中所见,通往Google的隧道只是一个流,您可以通过该流发送流请求

$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;

您可以通过以下方法请求到您的ip / port的隧道之前打开本地端口:
$localsock = socket_create_listen(0);
socket_getsockname($localsock, $YourInternetIPaccessible, $openedPort);

然后您的代码进行了以下更改:
$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!";
}

关于php - PHP ssh2_tunnel:使用代理/ socks 抛出ssh服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22765956/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com