Files
OpenRA/OpenRA.Game/Network/GeoIP.cs
2020-03-02 17:29:30 +01:00

99 lines
2.0 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.IO;
using System.Net;
using ICSharpCode.SharpZipLib.GZip;
using MaxMind.Db;
namespace OpenRA.Network
{
public class GeoIP
{
public class GeoIP2Record
{
[Constructor]
public GeoIP2Record(GeoIP2Country country)
{
Country = country;
}
public GeoIP2Country Country { get; set; }
}
public class GeoIP2Country
{
[Constructor]
public GeoIP2Country(GeoIP2CountryNames names)
{
Names = names;
}
public GeoIP2CountryNames Names { get; set; }
}
public class GeoIP2CountryNames
{
[Constructor]
public GeoIP2CountryNames(string en)
{
English = en;
}
public string English { get; set; }
}
static Reader database;
public static void Initialize(string databasePath)
{
if (string.IsNullOrEmpty(databasePath))
return;
try
{
using (var fileStream = new FileStream(databasePath, FileMode.Open, FileAccess.Read))
{
if (databasePath.EndsWith(".gz"))
using (var gzipStream = new GZipInputStream(fileStream))
database = new Reader(gzipStream);
else
database = new Reader(fileStream);
}
}
catch (Exception e)
{
Log.Write("geoip", "DatabaseReader failed: {0}", e);
}
}
public static string LookupCountry(IPAddress ip)
{
if (database != null)
{
try
{
var record = database.Find<GeoIP2Record>(ip);
if (record != null)
return record.Country.Names.English;
}
catch (Exception e)
{
Log.Write("geoip", "LookupCountry failed: {0}", e);
}
}
return null;
}
}
}