Created
February 25, 2016 07:55
-
-
Save coquer/cdd9f021ff58f61c4edf to your computer and use it in GitHub Desktop.
Simple way to detect Tor exit node using laravel 5.2 and PHP 7.0
This file contains hidden or 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
/** | |
* Detect if current User is using tor network | |
* @return bool | |
*/ | |
public static function detectIfTorNetwork(): bool { | |
return ( gethostbyname( | |
self::reverseIPOctets( getenv( 'REMOTE_ADDR' ) ) . "." . | |
getenv( 'SERVER_PORT' ) . "." . | |
self::reverseIPOctets( getenv( 'SERVER_ADDR' ) ) . | |
".ip-port.exitlist.torproject.org" ) == "127.0.0.2" ? true : false | |
); | |
} | |
/** | |
* Reverse the IP | |
* | |
* @param string $input | |
* | |
* @return string | |
*/ | |
public static function reverseIPOctets( string $input ) : string { | |
if ( filter_var( $input, FILTER_VALIDATE_IP ) ) { | |
$ip = explode( '.', $input ); | |
$ip = array_reverse( $ip ); | |
$ip = implode( '.', $ip ); | |
return $ip; | |
} | |
return '127.0.0.1'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment