【问题标题】:How do you proxy though a server using ssh (socks…) using php’s CURL?您如何使用 php 的 CURL 代理使用 ssh (socks...) 的服务器?
【发布时间】:2011-01-13 10:58:03
【问题描述】:

我想使用 ssh,类似这样:

ssh -D 9999 username@ip-address-of-ssh-server

但是在 php CURL 中,但我真的不明白这是怎么做到的?

我注意到“CURLPROXY_SOCKS5”是 php 站点中的一种类型,但猜想这行不通,因为它不是真正的 socks,它是 ssh……

我目前正在使用此代码:

curl_setopt($ch, CURLOPT_PROXY, ‘ip:port'); 

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

【问题讨论】:

  • 如果你能用 ssh2_tunnel 做请帮帮我,我用 ssh2_tunnel 做的,有错误Warning: ssh2_tunnel() [function.ssh2-tunnel]: Unable to request a channel from remote host

标签: php curl proxy


【解决方案1】:

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

  • 首先您需要从 PECL 站点get the ssh2 library。或者,PEAR package 也支持 SSH2。
  • 安装后,您可以阅读ssh2 documentation 设置隧道。
  • 然后您可以在脚本中设置隧道。
  • 在脚本中设置隧道后,您可以指定 CURL 代理。
  • 执行 CURL 操作。
  • 释放隧道资源并关闭脚本中的连接。

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

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_pubkey_file($connection, 'username', 'id_dsa.pub', 'id_dsa');
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 9999);
curl_setopt($ch, CURLOPT_PROXY, ‘127.0.0.1:9999'); 
// perform curl operations

// The connection and tunnel will die at the and of the session.
?>

最简单的选择

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

更简单的例子:

<?php
$connection = ssh2_connect(ip-address-of-ssh-server, 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

【讨论】:

  • 我会这样做但是错误Warning: ssh2_tunnel() [function.ssh2-tunnel]: Unable to request a channel from remote host如何解决这个问题?
【解决方案2】:

根据manpage,-D 确实创建了一个 socks 代理。

-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.

【讨论】:

  • 嗯,你说得有道理,没想到那样。虽然这意味着我需要在运行 php 脚本之前运行 linux 命令……有没有办法在 php 脚本中完成这一切? (脚本完成后与 ssh/socks 断开连接)
  • 我认为为每个请求设置/拆除代理不是一个好主意(至少您可能会遇到端口冲突等问题)。我建议将代理 (ssh -D) 作为环境设置的一部分。并将其用作普通的套接字代理服务器。
【解决方案3】:

您可以使用 ssh2 模块和ssh2_tunnel 函数通过远程服务器创建 ssh 隧道。 可用示例:http://www.php.net/manual/en/function.ssh2-tunnel.php

【讨论】:

  • 你能给我一个 CURL 请求的例子吗?考虑到 ssh 代理?
  • Curl 不能与 SSH 一起使用。即使您确实设法设置它,也需要一些 root 权限才能启用端口转发。 cURL 和 SSH 都用于连接到远程主机,但它们的功能不同。尝试将它们一起使用会非常尴尬,并且可能会加倍缓慢。这让我想到了另一个问题:您在执行 PHP 命令的服务器上是否具有 root 权限。如果是这样,您可以使用 PHP 从服务器内启动 putty
  • Stanislav Palatnik,1) 端口转发不需要 root。而且它不需要在服务器端进行特殊设置。 2) 我并不是说在这里使用带有 ssh2_tunnel 的 CURL。我的意思是使用 ssh2_tunnel 而不是 CURL
【解决方案4】:

请参阅我对 Qwerty 提出的解决方案的评论。我认为您正在寻找错误的方向来尝试解决这个问题。相反,您应该只使用 cURL 并为自己创建一个个人证书。你说你想使用 SSH 来保证安全,但为什么不用证书呢?

这个网站可以让你轻松创建一个 http://www.cacert.org/

因为它只适合您,您可以向浏览器添加例外,这样他们就不会抱怨证书错误。不需要 ssh!

【讨论】:

  • 我的浏览器从来没有看到 php 页面,它是通过 php-cgi -f /script.php 运行的,那么证书有什么帮助呢? (我已经为 lighttpd 中的 https 设置创建了自己的证书……我不太明白你们在说什么,请解释一下:)
【解决方案5】:

要仅在脚本执行期间打开 SSH 隧道,您可能需要使用 PHP forks。在一个过程中,打开 SSH 隧道(-D - 您需要做一些工作以确保您没有在此处的端口上发生冲突),在另一个过程中,使用带有 socks 代理配置的 CURL。传输完成后,发出 ssh fork 终止信号,以便断开连接。

请记住,当隧道打开时,同一台计算机上的其他用户也可以根据需要在该端口上代理。考虑到这一点,最好使用 -L 1234:remotehost:80 标志,然后获取 URL http://localhost:1234/some/uri

如果出现问题,您可能会在服务器上发现孤立的 SSH 隧道,所以我认为这有点脆弱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多