I am writing a PHP script that basically loops thru some data I have stored in my database. The two main important fields for this project are 'url' and 'country'.
Basically what I am trying to do is loop thru all of these URLs and determine which links are broken (using some PHP resolver, but isn't important to know for this problem). However the issue I am having is that you cannot access a URL that is assigned to a certain country, unless you have an IP address coming from that country. I already have this script set up for U.S URLs because I am here and so it makes that simple.
I was wondering if and how (point me to resources, tips, solutions) I can use Tor to programmatically check this 2 letter country code and go to that URL using that IP. I am guessing it will have something to do with using an Exit Node from that country, but I am not too sure.
Using the ExitNodes
configuration option, it is possible to specify exit nodes from a specific country using the syntax {US}
where US is the country code.
Using the PHP TorUtils library this can be automated in a simple way.
Here is a code example:
<?php
require 'TorUtils/src/ControlClient.php';
require 'TorUtils/src/TorCurlWrapper.php';
// list of country codes to use
$countries = array('US', 'FR', 'RU', 'GB', 'CA');
// get new control client for connecting to Tor's control port
$tc = new Dapphp\TorUtils\ControlClient();
$tc->connect(); // connect
$tc->authenticate('password'); // authenticate
foreach($countries as $country) {
$country = '{' . $country . '}'; // e.g. {US}
$tc->setConf(array('ExitNodes' => $country)); // set config to use exit node from country
// get new curl wrapped through Tor SOCKS5 proxy
$curl = new Dapphp\TorUtils\TorCurlWrapper();
$curl->setopt(CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox 41.0');
// make request - should go through exit node from specified country
if ($curl->httpGet('http://whatismycountry.com')) {
echo $curl->getResponseBody();
}
}
TorCurlWrapper is a simple cURL wrapper that routes cURL through the Tor SOCKS5 proxy. The ControlClient is used to set the configuration option before each request to switch to ExitNodes from a certain country.
Using composer you can install TorUtils:
php composer.phar require dapphp/torutils
Then in your code use require 'vendor/autoload.php';
to autoload the TorUtils classes.