-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sid
committed
Oct 8, 2016
0 parents
commit 673e3a7
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), '.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|