ClientBlocker 1.5
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.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
1 Comment : 01.09.07

#1
DotNetKicks.com
01.09.2007 @ 8:04 AM