Is it possible to block Tor users?

后端 未结 14 2400
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 04:03

Would it be possible to block Tor users? (https://www.torproject.org/)

Due to the nature of the site I run I should do all I can to stop multiple accounts and block cert

14条回答
  •  耶瑟儿~
    2021-01-30 04:59

    You can use the TorDNSEL service to perform a live query about whether a specific IP address is a Tor exit node. You query the service via a specially-formed DNS request.

    Here is some sample PHP code that performs the lookup:

    function isTorExitNode() {
        $serverPort = $_SERVER['SERVER_PORT'];
        $remoteAddr = reverseIp(getClientIp());
        $serverAddr = reverseIp($_SERVER['SERVER_ADDR']);
        $placeholders = '%s.%s.%s.ip-port.exitlist.torproject.org';
        $name = sprintf($placeholders, $remoteAddr, $serverPort, $serverAddr);
        return ( gethostbyname($name) === '127.0.0.2' );
    }
    
    function getClientIp() {
        if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
            return $_SERVER['HTTP_CF_CONNECTING_IP'];
        }
        return $_SERVER['REMOTE_ADDR'];
    }
    
    function reverseIp($ip) {
        $ipParts = explode('.', $ip);
        return $ipParts[3] . '.' . $ipParts[2] . '.' .
               $ipParts[1] . '.' . $ipParts[0];
    }
    
    if (!isTorExitNode()) {
        // Do nothing
    } else {
        Die("Sorry, You cannot use TOR network!!!");
    }
    

    Important Notes:

    • This example supports IPv4 addresses only, not IPv6.

    • It could take a couple of seconds to get a response, so be careful about introducing delays into your site rendering.

提交回复
热议问题