Created
November 12, 2016 20:31
-
-
Save ghstahl/007b8249d1bf5fc01503c68be35e6e12 to your computer and use it in GitHub Desktop.
GeoLite2 console app that fetches the GeoLite2 DB, decompresses it, and looks up an IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO.Compression; | |
using System.Threading; | |
using MaxMind.GeoIP2; | |
using MaxMind.GeoIP2.Model; | |
using MaxMind.GeoIP2.Responses; | |
namespace GeoLiteDBFetch | |
{ | |
class Program | |
{ | |
public static DatabaseReader DatabaseReader { get; set; } | |
public static string GzFilePath { get; set; } | |
public static string GeoLite2Db { get; set; } | |
public static string GeoLiteDataBaseDirectory { get; set; } | |
public static void Decompress(FileInfo fileToDecompress) | |
{ | |
using (FileStream originalFileStream = fileToDecompress.OpenRead()) | |
{ | |
string currentFileName = fileToDecompress.FullName; | |
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length); | |
using (FileStream decompressedFileStream = File.Create(newFileName)) | |
{ | |
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress)) | |
{ | |
decompressionStream.CopyTo(decompressedFileStream); | |
Console.WriteLine("Decompressed: {0}", fileToDecompress.Name); | |
} | |
} | |
} | |
} | |
private static void DownloadCurrent() | |
{ | |
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz"); | |
webRequest.Method = "GET"; | |
webRequest.Timeout = 3000; | |
webRequest.BeginGetResponse(new AsyncCallback(PlayResponeAsync), webRequest); | |
} | |
private static void PlayResponeAsync(IAsyncResult asyncResult) | |
{ | |
long total = 0; | |
long received = 0; | |
HttpWebRequest webRequest = (HttpWebRequest) asyncResult.AsyncState; | |
try | |
{ | |
using (HttpWebResponse webResponse = (HttpWebResponse) webRequest.EndGetResponse(asyncResult)) | |
{ | |
byte[] buffer = new byte[1024]; | |
FileStream fileStream = File.OpenWrite(GzFilePath); | |
using (Stream input = webResponse.GetResponseStream()) | |
{ | |
int size = input.Read(buffer, 0, buffer.Length); | |
while (size > 0) | |
{ | |
total += size; | |
fileStream.Write(buffer, 0, size); | |
received += size; | |
size = input.Read(buffer, 0, buffer.Length); | |
} | |
} | |
fileStream.Flush(); | |
fileStream.Close(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
} | |
Done = true; | |
} | |
public static bool Done { get; set; } | |
public static void DumpCity(CityResponse city) | |
{ | |
Console.WriteLine(city.Country.IsoCode); // 'US' | |
Console.WriteLine(city.Country.Name); // 'United States' | |
Console.WriteLine(city.Country.Names["zh-CN"]); // '美国' | |
Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' | |
Console.WriteLine(city.MostSpecificSubdivision.IsoCode); // 'MN' | |
Console.WriteLine(city.City.Name); // 'Minneapolis' | |
Console.WriteLine(city.Postal.Code); // '55455' | |
Console.WriteLine(city.Location.Latitude); // 44.9733 | |
Console.WriteLine(city.Location.Longitude); // -93.2323 | |
} | |
static void Main(string[] args) | |
{ | |
var dd = System.Reflection.Assembly.GetExecutingAssembly().Location; | |
var cc = Path.GetDirectoryName (dd); | |
GeoLiteDataBaseDirectory = Path.Combine(cc, "GeoLiteDataBase"); | |
Directory.CreateDirectory(GeoLiteDataBaseDirectory); | |
GzFilePath = Path.Combine(GeoLiteDataBaseDirectory, "GeoLite2-City.mmdb.gz"); | |
GeoLite2Db = Path.Combine(GeoLiteDataBaseDirectory, "GeoLite2-City.mmdb"); | |
DownloadCurrent(); | |
Console.WriteLine("Press ESC to stop"); | |
bool stop = false; | |
do | |
{ | |
if (Console.ReadKey(true).Key == ConsoleKey.Escape) | |
{ | |
stop = Done; | |
} | |
} while (stop == false); | |
FileInfo fileToDecompress = new FileInfo(GzFilePath); | |
Decompress(fileToDecompress); | |
DatabaseReader = new DatabaseReader(GeoLite2Db); | |
stop = false; | |
do | |
{ | |
if (Console.ReadKey(true).Key == ConsoleKey.Escape) | |
{ | |
stop = Done; | |
} | |
Console.WriteLine("172.91.98.240"); // 'US' | |
DumpCity(DatabaseReader.City("172.91.98.240")); | |
IPHostEntry host; | |
host = Dns.GetHostEntry("www.microsoft.com"); | |
foreach (IPAddress ip in host.AddressList) | |
{ | |
Console.WriteLine(ip); // 'US' | |
DumpCity(DatabaseReader.City(ip)); | |
} | |
} while (stop == false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment