I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
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.
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
DotNetKicks.com
Jan 09, 2007 8:04 AM
#
Leave a Comment