如何使用php的CURL使用ssh(socks ...)代理服务器?

Min*_*int 5 php proxy curl

我想使用ssh,像这样:

ssh -D 9999 username@ip-address-of-ssh-server
Run Code Online (Sandbox Code Playgroud)

但在PHP CURL中,但我真的不知道如何做到这一点?

我注意到"CURLPROXY_SOCKS5"作为PHP的网站类型,但猜测这是行不通的,因为它不是真正的袜子,这是SSH ...

我目前正在使用此代码:

curl_setopt($ch, CURLOPT_PROXY, ‘ip:port'); 
Run Code Online (Sandbox Code Playgroud)

但我使用的是免费代理,而且速度慢且不可靠,我也通过此代理发送敏感信息.这就是为什么我想通过我信任的保存服务器代理它,但我只有ssh设置它并且它无法托管正确的代理.

Joh*_*don 5

您可以在 PHP 脚本中使用 libssh2 和 curl。

\n\n
    \n
  • 首先,您需要从 PECL 站点获取 ssh2 库。另外,PEAR 包也支持 SSH2。
  • \n
  • 安装后,您可以阅读有关设置隧道的ssh2 文档。
  • \n
  • 然后您可以在脚本中设置隧道。
  • \n
  • 在脚本中设置隧道后,您可以指定 CURL 代理。
  • \n
  • 执行 CURL 操作。
  • \n
  • 释放隧道资源并关闭脚本中的连接。
  • \n
\n\n

我不是 PHP 专家,但这里有一个粗略的例子:

\n\n
<?php\n$connection = ssh2_connect(ip-address-of-ssh-server, 22);\nssh2_auth_pubkey_file($connection, \'username\', \'id_dsa.pub\', \'id_dsa\');\n$tunnel = ssh2_tunnel($connection, \'127.0.0.1\', 9999);\ncurl_setopt($ch, CURLOPT_PROXY, \xe2\x80\x98127.0.0.1:9999\'); \n// perform curl operations\n\n// The connection and tunnel will die at the and of the session.\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

最简单的选择

\n\n

另一个需要考虑的选择是使用 sftp(通过 ssh 进行 ftp)而不是 CURL...这可能是在 PHP 中安全地将文件从一台服务器复制到另一台服务器的推荐方法...

\n\n

更简单的例子:

\n\n
<?php\n$connection = ssh2_connect(ip-address-of-ssh-server, 22);\nssh2_auth_password($connection, \'username\', \'password\');\nssh2_scp_send($connection, \'/local/filename\', \'/remote/filename\', 0644);\n?>\n
Run Code Online (Sandbox Code Playgroud)\n


Dyn*_* Fu 3

根据联机帮助页,-D 确实创建了一个袜子代理。

-D [bind_address:]port
             Specifies a local ``dynamic'' application-level port forwarding.
             This works by allocating a socket to listen to port on the local
             side, optionally bound to the specified bind_address.  Whenever a
             connection is made to this port, the connection is forwarded over
             the secure channel, and the application protocol is then used to
             determine where to connect to from the remote machine.  Currently
             the SOCKS4 and SOCKS5 protocols are supported, and ssh will act
             as a SOCKS server.  Only root can forward privileged ports.  Dy-
             namic port forwardings can also be specified in the configuration
             file.
Run Code Online (Sandbox Code Playgroud)

  • 我认为为每个请求设置/拆除代理不是一个好主意(至少您可能会遇到端口冲突等问题)。我建议将代理(ssh -D)作为您的环境设置的一部分。并将其用作普通的套接字代理服务器。 (2认同)