Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sid committed Oct 8, 2016
0 parents commit 673e3a7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# tor-detector
A library to detect if your website visitor is from a TOR network or not
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "onlinesid/tor-detector",
"type": "library",
"description": "Detect if your website visitor is from a TOR exit node",
"keywords": [
"tor",
"tor browser",
"detector"
],
"homepage": "https://github.com/onlinesid/tor-detector",
"license": "MIT",
"autoload": {
"psr-4": { "OnlineSid\\" : "src/" }
}
}
50 changes: 50 additions & 0 deletions src/Tor/TorDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace OnlineSid\Tor;

/**
* Class TorDetector
*
* Helps to detect if visitors are using a Tor browser to surf the website.
*
* Thanks to https://trac.torproject.org/projects/tor/wiki/doc/TorDNSExitList
*
* Thanks to https://jrnv.nl/detecting-the-use-of-proxies-and-tor-network-6c240d6cc5f#.k8ldcs21o
*
*/
class TorDetector
{
/**
* Checks if the remote IP is from a TOR exit node or not
*
* @param string $remoteIp The IP of the visitor you'd like to check.
* @param int $port The port on which your server is running, e.g.: port 80.
* @param string $serverIp Your server IP.
*
* @return bool
*/
public function check($remoteIp, $port, $serverIp)
{
$detectHost = sprintf(
'%s.%s.%s.ip-port.exitlist.torproject.org',
$this->reverseIPOctets($remoteIp),
$port,
$this->reverseIPOctets($serverIp)
);

// According to the guide, if this returns 127.0.0.2, it's a Tor exit node
return gethostbyname($detectHost) === '127.0.0.2';
}

/**
* This function simply reverses the IP's octets.
*
* @param string $ip The IP to be reversed.
*
* @return string
*/
protected function reverseIPOctets($ip)
{
return implode(array_reverse(explode('.', $ip)), '.');
}
}
15 changes: 15 additions & 0 deletions usage_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require_once "src/Tor/TorDetector.php";

$tor_detector = new OnlineSid\Tor\TorDetector();

$your_server_ip = '1.2.3.4'; // the IP address of your server (web server)
$user_ip = '62.102.148.67'; // is this IP from TOR exit node?
if ($tor_detector->check($user_ip, 80, $your_server_ip)) {
echo "Tor!\n";
} else {
echo "Not TOR!\n";
}

0 comments on commit 673e3a7

Please sign in to comment.