Pergunta

I'm using Tor with PHP, for now, everything is working properly. But, when I try to start Tor from php it'll sometimes throw an error becuase Tor is currently running, so i've been wondering if there is any method to check Tor's status(running or not)

I can only use SOCKS5 protocol or Linux CLI; these are my only options.

Note: This functions connects to Tor(it works).

function init()
    {
        //Connect to Tor

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

    }  
Foi útil?

Solução 2

I found a solution to this problem, It's basically getting all running processes and searching for "tor" among them(based on Bass Jobsen's comment).

That's the code:

Note: this code was tested in Ubuntu only, but It should work on most Linux OS'es.

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

}

Outras dicas

In general, Unix/Linux daemons commonly implement the "already running" check using a lock file (often located in /var/run). You could use file_exists() to check for the lock file's presence. Of course, this cannot be used to detect whether the process has crashed (and thus whether the process is actually running at any given moment); checking the process table may be necessary.

As for Tor specifically, a quick web search seems to suggest you don't have to do that. Tor locates a Unix domain socket at /var/run/tor/control (and listens on TCP port 9051 as well, depending on configuration) to allow other programs to control it. Trying to connect to Tor through that socket (using PHP's socket functions and the Tor Control Protocol) might work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top