here's how I did it.
create a sub folder in your hmailserver programs folder called 'geoip'
download the free database (updated monthly) from
http://geolite.maxmind.com/download/geo ... oIP.dat.gz
unzip and place it in the folder you just created (hmailserver\geoip)
the unzipped file should be called GeoIP.dat
Read Microsoft COM section at
Download the COM API zip file from the link on page. or from following link
32bit OS users:
unzip GeoIP-COM-1.3.zip and place the file GeoIpComEx.dll into your windows\system32 folder
run windows command prompt and enter the following two commands.
then reboot pc and continue skipping the 64bit OS users section
64bit OS users:
unzip GeoIP-COM-1.3.zip and place the file GeoIpComEx.dll into your windows\sysWOW64 folder
run windows command prompt as administrator and enter the following two commands.
(N.B. some people report problems with installing 32bit dll on 64bit windows. If after completing full instructions below you have problems using GeoIpComEx.dll you can try downloading the "MS COM API Guide for 64bit machines" from http://www.maxmind.com/GeoIP_MSCOM_64bit.pdf and follow instructions for full installation/registering of dll. And please check events log to see what problem is.)
then reboot pc and continue below
then both 32bit and 64bit OS users:
edit your hMailServer EventHandlers.vbs script file to include the following code in the OnClientConnect event handler section.
Code: Select all
Dim geoip Result.Value = 1 set geoip = CreateObject("GeoIPCOMEx.GeoIPEx") geoip.set_db_path("c:\Program files\hmailserver\geoip\") geoip.find_by_addr(oClient.IPAddress) country = geoip.country_code If (country = "LH" ) Then Result.Value = 0 End if If (country = "LN" ) Then Result.Value = 0 End if If (country = "GB" ) Then Result.Value = 0 End if If (country = "US" ) Then Result.Value = 0 End if If (country = "SE" ) Then Result.Value = 0 End if If (country = "DE" ) Then Result.Value = 0 End if If (country = "FR" ) Then Result.Value = 0 End if If (Result.Value = 1 ) Then ' Rejected EventLog.Write("Geo-IP rejected:"+Chr(34)+vbTab+oClient.IPAddress+vbTab+Chr(34)+geoip.country_code+" "+geoip.country_name) End if
to point to where ever you put your geoIP.dat file.
save EventHandlers.vbs file and then in the hmailsever script page reload the scripts.
You're good to go.
What this will actually do is block any IP which is not in the listed countries from connecting to your mailserver. The country codes LH and LN mean localhost and Local area Lan so both should always be included in the inclusion list or removed if you decide to make an exclusion list (see below).
The way I have set it up, it will only allow through a few selected countries. You can take the opposite approach and and only block selected countries by changing default Result.value to 0 (towards top of code) and setting the Result.value to 1 for each selected country.
Obviously you can add or subtract as many countries as you like.
N.B. GB is the correct code for the United Kingdom. I assumed it was UK and spent a lot of time head scratching as to why it wouldn't work.
I never programmed in VBScript before so I'm sure someone can improve the list by putting it in a loop...
*** WARNING ***
Implement this at your own risk...
this will block every IP from a given country so users of gmail, hotmail, yahoo and all the other online mail providers will be blocked if they are being sent from servers in one of the blocked countries. So it is probably not a good idea to block the US even if you do get some spam from them.
If you are receiving thousands of spam messages everyday from certain countries, this will stop those mails from ever reaching your server and reduce log file sizes significantly as well as reduce spam processing and communications with other mail severs. And since it blocks the initial connect before mail message is transferred, it will also lose all the log file messages for attempts to deliver to unknown users.
Only one call to a local binary lookup file is required to get the country code so performance is reasonable.
For optimum performance it may be better to download the csv file and load a mysql or mssql db and modify code to lookup from db. This is becaue the geoip.dat file is 800K or so in size and I don't think it stays in memory from one ip lookup to the next but not too sure about that.
You could easily lose genuine emails from countries you have blocked. But then how often do you get real messages from foreign countries. That's your call and depends on where you are doing business and which country you are based in.
To keep IP list uptodate you have to download the GeoIP.dat database file monthly but you can easily automate that using scheduled task or using the maxmind API for it (see their site). Its not a problem if you don't update it monthly but best to do it fairly regularly.
to cycle the event log file create a .vbs file on your system with the code below.
To be run using windows task scheduler so you can set frequency to whatever you like as long as its no shorter than 24 hours(daily).
The archived log file is given the previous days date so it represents event log data upto that date from previous log file date. (more or less)
Be sure to set your password in the authenticate step.
Its just a simple rename of the events log file.
HMS is closed to release the log file and restarted again after the rename.
The new log file is created automatically by HMS next time it tries to write to it.
Code: Select all
Set WshShell = Wscript.CreateObject("Wscript.Shell") Set oLog = CreateObject("hMailServer.Logging") Set oApp = CreateObject("hMailServer.Application") Set oFSO = CreateObject("Scripting.FileSystemObject") LogDate = DateAdd("d", -1, Date) myMonth = Right("0"+CStr(Month(LogDate)),2) myDay = Right("0"+CStr(Day(LogDate)),2) OldName = oLog.CurrentEventLog NewName = Left(oldName,(Len(OldName) - 4)) + "_" + CStr(Year(LogDate))+"-"+myMonth+"-"+myDay + ".log" If oFSO.FileExists(NewName) = False Then Call oApp.Authenticate("Administrator", "mypass") Call oApp.Stop oFSO.MoveFile OldName , NewName Call oApp.Start End If Set WshShell = Nothing