Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tor exit node programmatically (get new IP)

Tags:

php

curl

tor

Is there a way, and which one to change tor exit node in PHP (get new IP)?

Right now, I get new IP every 10 minutes or so..

<?php

// Initialize cURL
$ch = curl_init();

// Set the website you would like to scrape
curl_setopt($ch, CURLOPT_URL, "http://icanhazip.com/");
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_REFERER, '');
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8118');

 // Set cURL to return the results into a PHP variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// This executes the cURL request and places the results into a variable.
$curlResults= curl_exec($ch);

// Close curl
curl_close($ch);

// Echo the results to the screen>
echo $curlResults;

?>
like image 756
RhymeGuy Avatar asked Sep 21 '14 02:09

RhymeGuy


2 Answers

I have no idea why people want to close this question, but whatever.. Here is the solution which worked for me. It will change exit node on request.

<?php
$fp = fsockopen('127.0.0.1', 9051, $errno, $errstr, 30);
$auth_code = 'YOUR_PASSWORD';
if ($fp) {
    echo "Connected to TOR port<br />";
}
else {
    echo "Cant connect to TOR port<br />";
}

fputs($fp, "AUTHENTICATE \"".$auth_code."\"\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
    echo "Authenticated 250 OK<br />";
}
else {
    echo "Authentication failed<br />";
}

fputs($fp, "SIGNAL NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
    echo "New Identity OK<br />";
}
else {
    echo "SIGNAL NEWNYM failed<br />";
    die();       
}
fclose($fp);
?>
like image 194
RhymeGuy Avatar answered Sep 20 '22 12:09

RhymeGuy


Tor already cycles your circuit by default every ten minutes...

https://www.torproject.org/docs/tor-manual.html#MaxCircuitDirtiness

This is not exactly the same as 'get a new IP'. Your IP will probably change, but might not (relay selection is at random). Please see...

https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor

like image 41
Damian Avatar answered Sep 22 '22 12:09

Damian