Sanjay Kumar
May 4, 2021
  3910
(3 votes)

Update GeoIP2 database automatically

The purpose of the blog to update the latest GeoIP2 database automatically. Because the GeoLite2 Country, City, and ASN databases are updated weekly, every Tuesday of the week. So we required to update the database up to date, which we can accomplish through the schedule job.

About GeoIP2:

MaxMind GeoIP2 offerings provide IP geolocation and proxy detection for a wide range of applications including content customization, advertising, digital rights management, compliance, fraud detection, and security.

The GeoIP2 Database MaxMind provides both binary and CSV databases for GeoIP2. Both formats provide additional data not available in our legacy databases including localized names for cities, subdivisions, and countries.

 

Requirement:

In one of my current projects, we required to read the zip code/postal code on basis of the client IP Address and populate into the address area. Similarly, you can retrieve the Country, City Name, Latitude & Longitude, Metro Code etc.

Solution:

  1. Create the schedule job and download GeoLiteCity.dat.gz.
  2. Uncompressed the file and copy the GeoLite2-City.mmdb database file on physical location which you have define in basePath.
  3. Read the client IP Address.
  4. Read the downloaded GeoLite2-City.mmdb database through DatabaseReader and search the IP address into city database and retrieve zip code/postal code.

 

Okay, so let's do some coding for achieving the above approaches:

Step1: Create the schedule job.

public class GeoIp2DataBaseUpdateJob : ScheduledJobBase
{
        private bool _stopSignaled;
        private const string GeoLiteCityFileDownloadUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz";

        public GeoIp2DataBaseUpdateJob()
        {
            this.IsStoppable = true;
        }

        /// <summary>
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// </summary>
        public override void Stop()
        {
            base.Stop();
            _stopSignaled = true;
        }

        /// <summary>
        /// Called when a scheduled job executes
        /// </summary>
        /// <returns>A status message to be stored in the database log and visible from admin mode</returns>
        public override string Execute()
        {
                   // 1. Download file
            var result = DownloadFile()
                // 2. Unzip file
                .Bind(UnzipFile)
                // 3. Replace at physical location
                .Bind(ReplaceCurrentFile)
                .Match(
                    right => $"đź‘Ť GeoIP2 database updated successfully.",
                    left => $"đź‘Ž GeoIP2 database update failed.");

            if (_stopSignaled)
            {
                return "Stop of job was called";
            }

            return result;
        }

        private static Either<Exception, string> DownloadFile()
        {
            var licenseKey = ConfigurationManager.AppSettings["Geolocation.LicenseKey"];
            var uri = new Uri(GeoLiteCityFileDownloadUrl + $"&license_key={licenseKey}");

            return Prelude.Try(
                    () =>
                    {
                        using (var client = new WebClient())
                        {
                            var tempDir = Path.GetDirectoryName(Path.GetTempPath());
                            var localFile = Path.Combine(tempDir, "MaxMind/GeoLite2-City.tar.gz");
                            var maxMindFolderPath = Path.GetDirectoryName(localFile);
                            if (!Directory.Exists(maxMindFolderPath) && !string.IsNullOrWhiteSpace(maxMindFolderPath))
                            {
                                Directory.CreateDirectory(maxMindFolderPath);
                            }

                            client.DownloadFile(uri, localFile);
                            return localFile;
                        }
                    })
                .Succ(localFile => Prelude.Right<Exception, string>(localFile))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> ReplaceCurrentFile(string unzippedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
                        File.Copy(unzippedFileName, maxMindDbFilePath, true);

                        //Delete extracted folder
                        var dir = Path.GetDirectoryName(unzippedFileName);
                        if (Directory.Exists(dir))
                        {
                            Directory.Delete(dir, true);
                        }
                        return unzippedFileName;

                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> UnzipFile(string downloadedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var dir = Path.GetDirectoryName(downloadedFileName);
                        FileInfo tarFileInfo = new FileInfo(downloadedFileName);

                        DirectoryInfo targetDirectory = new DirectoryInfo(dir ?? string.Empty);
                        if (!targetDirectory.Exists)
                        {
                            targetDirectory.Create();
                        }
                        using (Stream sourceStream = new GZipInputStream(tarFileInfo.OpenRead()))
                        {
                            using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(sourceStream, TarBuffer.DefaultBlockFactor))
                            {
                                tarArchive.ExtractContents(targetDirectory.FullName);
                            }
                        }

                        var filePath = Directory.GetFiles(dir ?? string.Empty, "*.mmdb", SearchOption.AllDirectories)?.LastOrDefault();

                        //Delete .tar.gz file
                        if (File.Exists(downloadedFileName))
                        {
                            File.Delete(downloadedFileName);
                        }
                        return filePath;
                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

    }

Step 2: Update the web.config settings

  1. Set the database file path for geolocation provider if you are using for personalization.
  2. Add the basePath for updating the file on a given physical location.
  3. Set MaxMind license key
<episerver.framework>
  ...
  <geolocation defaultprovider="maxmind">
    <providers>
      <add databasefilename="C:\Program Files (x86)\MaxMind\GeoLite2-City.mmdb" name="maxmind" type="EPiServer.Personalization.Providers.MaxMind.GeolocationProvider, EPiServer.ApplicationModules">
    </add></providers>
  </geolocation>
  <appData basePath="C:\Program Files (x86)\MaxMind" />
</episerver.framework>

<appSettings>
 ...
 <add key="Geolocation.LicenseKey" value="{YourLicenseKey}" 
</appSettings>

Step 3: Run the job and make sure the file is updating, if the code cause the error then update accordingly.

Step4: Create the GeoLocationUtility class and read the database file for client IP Address.

 public class GeoLocationUtility
 {
        private static string _maxMindDatabaseFileName = "GeoLite2-City.mmdb";

        public static GeoLocationViewModel GetGeoLocation(IPAddress address, NameValueCollection config)
        {
            string text = config["databaseFileName"];

            if (!string.IsNullOrEmpty(text))
            {
                _maxMindDatabaseFileName = VirtualPathUtilityEx.RebasePhysicalPath(text);
                config.Remove("databaseFileName");
            }

            if (string.IsNullOrWhiteSpace(_maxMindDatabaseFileName)
                || !File.Exists(_maxMindDatabaseFileName)
                || address.AddressFamily != AddressFamily.InterNetwork && address.AddressFamily != AddressFamily.InterNetworkV6)
            {
                return null;
            }

            var reader = new DatabaseReader(_maxMindDatabaseFileName);
            try
            {
                var dbResult = reader.City(address);
                var result = GeoLocationViewModel.Make(dbResult);
                return result;
            }
            catch
            { 
                //ignore exception
            }
            finally
            {
                reader.Dispose();
            }

            return null;
        }

Step 5: Finally, call the utility method where you want to get the zip code/postal code value such as:

  var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
   var config = new NameValueCollection
   {
    {"databaseFileName", maxMindDbFilePath}
   };

   var  postalCode= GeoLocationUtility.GetGeoLocation(ipAddress, config)?.PostalCode;

I hope you found this informative and helpful, Please leave your valuable comments in the comment box.

Thanks for your visit!

May 04, 2021

Comments

Praful Jangid
Praful Jangid May 4, 2021 06:58 AM

I will spend some of my time to implement and try your approach. Thanks for sharing.

Sunil
Sunil May 4, 2021 07:17 AM

Nice, Thanks for sharing.

Sujit Senapati
Sujit Senapati Sep 23, 2021 07:09 PM

Great article @Sanjay!

Please login to comment.
Latest blogs
Optimizely’s Sustainability Journey: Creating a Future That’s Built to Last

  For me, sustainability isn’t just a company priority; it’s a personal one. I’m especially mindful of the role that content management plays at th...

Joey Moore | Dec 13, 2024

How I Fixed DLL Conflicts During EPiServer CMS Upgrade to .NET Framework 4.8.1

We had a CMS solution of EPiServer 11.26.0, which was built on .NET Framework 4.7.1. We needed to update the target framework from .NET Framework...

calimat | Dec 12, 2024

Custom form element view in Optimizely CMS 12

Do you want full control over the form element markup? Create your own views!

Tomas Hensrud Gulla | Dec 11, 2024 | Syndicated blog

How to Elevate Your Experimentation - Opticon workshop experience

As a non-expert in the field of experimentation, I’d like to share my feedback on the recent Opticon San Antonio workshop session titled "How to...

David Ortiz | Dec 11, 2024