wasStitchNET – Rev 13

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////

using System;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using MaxMind.GeoIP2;
using MaxMind.GeoIP2.Responses;

namespace wasStitchNET.GeoIP
{
    public static class Extensions
    {
        public static CityResponse GeoIPGetCity(this IPAddress address)
        {
            // Open the MaxMind GeoIP database.
            using (var gZipStream =
                new GZipStream(
                    Assembly.GetExecutingAssembly()
                        .GetManifestResourceStream($"wasStitchNET.GeoIP.MaxMind.{STITCH_CONSTANTS.GEOIP_CITY_DATABASE}"),
                    CompressionMode.Decompress))
            {
                // Resolve the IP address to a city response.
                using (var reader = new DatabaseReader(gZipStream))
                {
                    CityResponse response;
                    if (reader.TryCity(address, out response))
                        return response;
                }
            }

            return null;
        }
    }
}

Generated by GNU Enscript 1.6.5.90.