CSharpFeeds - All your C# feeds in one place.

Sponsors

Tuesday, January 09, 2007

ClientBlocker 1.5

by Keyvan Nayyeri via Keyvan Nayyeri on 1/9/2007 2:41:09 PM

Today I made some changes in my ClientBlocker.  This may be the last version of this HTTPModule and I won't update it anymore (because it doesn't need any features though).

Using this HTTPModule you can block or trust on single IPs or a range of IPs and also use a free webservice to block site visitors based on their country.

In addition to making some minor changes I simplified the source code and solved a major bug.  Now it works brilliantly.  Main changes in code logic have been applied here:

#region Public methods

 

public void Init(HttpApplication context)

{

    context.BeginRequest += new EventHandler(context_BeginRequest);

}

 

public void Dispose()

{

}

 

#endregion

 

#region Handlers

 

void context_BeginRequest(object sender, EventArgs e)

{

    HttpApplication application = sender as HttpApplication;

 

    bool block = false;

 

    // Configurations

    this.filePath = application.Server.MapPath

        (ConfigurationManager.AppSettings["CB_BlockedIPsFile"].ToString());

 

    this.checkService = Convert.ToBoolean

        (ConfigurationManager.AppSettings["CB_CheckService"]);

 

    if (this.checkService == true)

    {

        string[] CountriesTemp = ConfigurationManager.AppSettings

            ["CB_BlockedCountries"].ToString().Split(' ');

        this.blockedCountries = GetArrayList(CountriesTemp);

    }

 

 

    try

    {

        string clientIP = application.Request.UserHostAddress;

 

        CheckIPs objChecker = new CheckIPs(this.filePath);

 

        if (objChecker.CheckIP(clientIP) == false)

        {

            if (this.checkService == true)

            {

                // User wants to check with webservice

                XmlDocument doc = new XmlDocument();

                string serviceURL = "http://api.hostip.info/?ip=" + clientIP;

 

                doc.Load(serviceURL);

 

                XmlNamespaceManager NSManager = new XmlNamespaceManager(doc.NameTable);

                NSManager.AddNamespace("hostip", "http://www.hostip.info/api");

                string query = "//hostip:countryAbbrev";

                XmlNode node = doc.SelectSingleNode(query, NSManager);

 

                string country = null;

                if (node != null)

                {

                    country = node.InnerText;

                }

 

 

                if (this.blockedCountries.Contains(country) == true)

                {

                    block = true;

                }

                else

                {

                    block = false;

                }

            }

            else

            {

                block = true;

            }

        }

    }

    catch

    {

        // On any unexpected error we let visitor to visit website

    }

 

    if (block)

    {

        // Blocking process

        application.Response.StatusCode = 200;

        application.Response.SuppressContent = true;

        application.Response.End();

    }

}

 

#endregion

Like the past you can use an XML file to set trusted and blocked IPs.  Below is a sample file:

<?xml version="1.0" encoding="utf-8" ?>

<ips>

  <trusted>

    <ip value="127.0.0.1"/>

    <range lower="128.0.0.0" upper="128.255.255.255" />

  </trusted>

  <blocked>

    <range lower="127.0.0.0" upper="127.255.255.255" />

  </blocked>

</ips>

Configurations in Web.Config file look like this:

<?xml version="1.0" encoding="utf-8"?>

 

<configuration>

 

  <appSettings>

    <add key="CB_BlockedIPsFile" value="~/IPs.xml"/>

    <add key="CB_CheckService" value="false"/>

    <add key="CB_BlockedCountries" value="IR RU"/>

  </appSettings>

 

  <system.web>

    <httpModules>

      <add type="ClientBlocker.Blocker, ClientBlocker" name="ClientBlocker" />

    </httpModules>

  </system.web>

 

</configuration>

New version is available on my file gallery with source code and installation instructions and you can download it.

email it!bookmark it!digg it!

Original Post: ClientBlocker 1.5

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us