A Ovest Di Paperino

Welcome to the dark side.

Tor exit node lookup in ASP.Net

Potrebbe tornare utile a qualcun altro:

using System.Web;

using System.Net;

 

namespace TorHelper

{

    public class TorHelper

    {

        const string TOR_EXIT_IP = "127.0.0.2";

 

        private static string ResolveName(string name)

        {

            string ip = (string) HttpContext.Current.Cache[name];

            if (ip == null)

            {

                ip = ResolveDNS(name);

                HttpContext.Current.Cache[name] = ip;

            }

            return ip;

        }

 

        private static string ResolveDNS(string name)

        {

            try

            {

                IPHostEntry host = Dns.GetHostEntry(name);

                if (host.AddressList.Length == 0)

                {

                    // this should never happen

                    // but throwing an exception is expensive.

                    return null;

                }

                return host.AddressList[0].ToString();

            }

            catch(SocketException)

            {

                // this can happen when the DNS doesn't get resolve

                // and it actually happens for all IP addresses
                // that are
not part of the TOR exit list

                return String.Empty;

            }

        }

 

        public static bool IsTorExitNode(string ipAddress)

        {

 

            try

            {

                string torExitEntry = String.Format(

                   "{0}.{1}.{2}.ip-port.exitlist.torproject.org",

                    ReverseIPOctet(ipAddress),

                   "80",

                    // Domain name is the running

                    // ASP.Net webSite

                    ReverseIPOctet(ResolveName(DomainName)) );

 

                if (ResolveName(torExitEntry) == TOR_EXIT_IP)

                {

                    return true;

                }

            }

            catch (Exception e)

            {

                // log the error

            }

 

            return false;

        }

 

        private static string ReverseIPOctet(string ipAddress)

        {

            string[] octets = ipAddress.Split('.');

            return String.Format("{3}.{2}.{1}.{0}",

               octets[0],
               octets[1],
               octets[2],
               octets[3]);

        }

 

    }

}

Su questo blog i commenti lasciati via Tor finiscono in coda di moderazione insieme a quelli che partono da una subnet ban list. Ed ora il giochino si fa più interessante.

-quack

P.S. il pezzo di codice di cui sopra è rilasciato al pubblico dominio con la licenza SQLite:

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.