punk_runner Posted April 3, 2011 Share Posted April 3, 2011 I have a PHP app that I wrote that requires a new IP address each time that it is run. Currently I use Tor on my desktop and the Vidalia app to change the Tor identity each time I run the script but I want to automate this. Can anyone help with switching Tor identities with PHP? Essentially after my script runs I need to call a function that switches to a new Tor identity. I have the following code but it doesn't seem to work, and I am not sure why. I am running this from XAMPP on OSX, Tor is running locally (client not server)... in my Tor settings I have 127.0.0.1 and port 9051, set to "no password"... <?php function tor_new_identity($tor_ip='127.0.0.1', $control_port='9051', $auth_code='') { $fp = fsockopen($tor_ip, $control_port, $errno, $errstr, 30); if (!$fp) { return false; //can't connect to the control port } fputs($fp, "AUTHENTICATE $auth_code\r\n"); $response = fread($fp, 1024); list($code, $text) = explode(' ', $response, 2); if ($code != '250') { return false; //authentication failed } //send the request to for new identity fputs($fp, "signal NEWNYM\r\n"); $response = fread($fp, 1024); list($code, $text) = explode(' ', $response, 2); if ($code != '250') { return false; //signal failed } fclose($fp); return true; } tor_new_identity(); ?> Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted April 4, 2011 Share Posted April 4, 2011 I've had experience with PHP, Tor and cURL all talking to each other . What "Doesnt seem to work" about your code? Error messages? Does auth fail? Quote Link to comment Share on other sites More sharing options...
kc111 Posted April 6, 2011 Share Posted April 6, 2011 Hi, I am having a similar problem. I am working on win7-64 bit with WAMP 2.1. I am trying to do basically the same thing with the latest Vidalia ( including Tor 0.2.1.30 and Polipo ), running locally. I am using php 5.3 <?php ? //include the class //remember the configuration file is located in: //proxyConfiguration.ini include("./proxyConnector.class.php"); //get an istance of the proxy $connection = proxyConnector::getIstance(); //connect to google.com and change my identity //because "switchIdentityAfterRequest" is set to TRUE //in the .ini file $connection->launch("http://whatismyip.com", null); //get the data and show it $data = $connection->getProxyData(); echo "<pre>"; print_r($data); The class is attached. My problem is that , I would like to switch Identity after each request ( I am querying http://whatismyip.com ) . this happens sometimes, but when I run my script too close together , temporally, the IP address does not switch ( If I wait longer it will ). The following code snippet is running without obvious error when I step through it with xdebug: /** * * Change identity in the Tor Network * (change public IP Address) * * @return bool * true is new identity is created * false is fail creating a new identity */ public function newIdentity(){ $fp = fsockopen($this->ip, $this->controlPort, $errno, $errstr, 30); if (!$fp) return false; //can't connect to the control port fputs($fp, "AUTHENTICATE ".$this->controlPassword."\r\n"); $response = fread($fp, 1024); list($code, $text) = explode(' ', $response, 2); if ($code != '250') return false; //authentication failed //send the request to for new identity fputs($fp, "signal NEWNYM\r\n"); $response = fread($fp, 1024); list($code, $text) = explode(' ', $response, 2); if ($code != '250') return false; //signal failed fclose($fp); return true; } I think that some cacheing may be going on somewhere, or a connection is being maintained when it should not . As a php beginner, I don't know how to test this or where to look. I would appreciate any help. Thank you in advance, KC PS - here is the ini file - I couldn't attach it ; this is the ini file for the proxy configuration ; this default value are for a TOR PROXY running ; on a local machine with HashedControlPassword method ; activated [general] ; timeout timeout = 300 ; proxy ip ip = 127.0.0.1 ; proxy port (default value stands for polipo port) port = 8118 [TOR] ; proxy control port ; be sure to enable control in your ; tor configuration file (/etc/tor/torrc) controlPort = 9051 ; the password setted in the HashedControlPassword ; set a tor control password with: ; tor --hash-password YOUR_STRONG_PASSWORD ; ; To use this default configuration file try: ; tor --hash-password t0rS3cr3tPwD ; ; left blank if you have configured TOR CONTROL PORT ; without a password controlPassword = t0rS3cr3tPwD ; if true after every request the class try to change ; his identity and his userAgent switchIdentityAfterRequest = true [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.