【问题标题】:How to check if Tor is running within PHP?如何检查 Tor 是否在 PHP 中运行?
【发布时间】:2013-04-14 17:28:12
【问题描述】:

我正在将 Tor 与 PHP 一起使用,目前一切正常。但是,当我尝试从 php 启动 Tor 时,它有时会抛出一个错误,因为 Tor 当前正在运行,所以我一直想知道是否有任何方法可以检查 Tor 的状态(运行与否)

我只能使用 SOCKS5 协议或 Linux CLI;这些是我唯一的选择。

注意:此函数连接到 Tor(它可以工作)。

function init()
    {
        //Connect to Tor

        $socket = fsockopen($this->proxyIp, $this->proxyPort);      
        fwrite($socket, 'AUTHENTICATE' . PHP_EOL);

    }  

【问题讨论】:

  • @Servant: The Onion Router
  • 在Linux上你可以检查进程ps aux | grep "[t]or"

标签: php socks tor


【解决方案1】:

一般来说,Unix/Linux 守护程序通常使用锁定文件(通常位于 /var/run)来实现“已运行”检查。您可以使用file_exists() 检查锁定文件是否存在。当然,这不能用于检测进程是否崩溃(以及进程是否在任何给定时刻实际运行); checking the process table 可能是必要的。

具体来说,对于 Tor,快速的网络搜索似乎表明您不必这样做。 Tor 将Unix domain socket 定位在/var/run/tor/control(并且根据配置也侦听TCP 端口9051)以允许其他程序控制它。尝试通过该套接字(使用 PHP 的 socket functionsTor Control Protocol)连接到 Tor 可能会奏效。

【讨论】:

    【解决方案2】:

    我找到了解决这个问题的方法,它基本上是获取所有正在运行的进程并在其中搜索“tor”(基于Bass Jobsen's comment)。

    代码如下:

    注意:此代码仅在 Ubuntu 中测试过,但它应该适用于大多数 Linux 操作系统。

    function isRunning()
    {
        //Get all running processes and search for "tor" within them
        //The "[" and "]" are used to execlude ps and grep from the returned result.
        $CLIResult = exec('ps aux | grep -w [t]or');
    
        //If no processes was found(= Tor is not running..)
        return empty($CLIResult);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多