move GeoIP functions into it's own class
extract the geoip database .gz in-game
This commit is contained in:
@@ -15,7 +15,6 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using MaxMind.GeoIP2;
|
|
||||||
using OpenRA.FileSystem;
|
using OpenRA.FileSystem;
|
||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.Network;
|
using OpenRA.Network;
|
||||||
@@ -44,8 +43,6 @@ namespace OpenRA
|
|||||||
public static Renderer Renderer;
|
public static Renderer Renderer;
|
||||||
public static bool HasInputFocus = false;
|
public static bool HasInputFocus = false;
|
||||||
|
|
||||||
public static DatabaseReader GeoIpDatabase;
|
|
||||||
|
|
||||||
public static OrderManager JoinServer(string host, int port, string password, bool recordReplay = true)
|
public static OrderManager JoinServer(string host, int port, string password, bool recordReplay = true)
|
||||||
{
|
{
|
||||||
IConnection connection = new NetworkConnection(host, port);
|
IConnection connection = new NetworkConnection(host, port);
|
||||||
@@ -216,14 +213,7 @@ namespace OpenRA
|
|||||||
Settings.Server.AllowPortForward = false;
|
Settings.Server.AllowPortForward = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
GeoIP.Initialize();
|
||||||
{
|
|
||||||
GeoIpDatabase = new DatabaseReader("GeoLite2-Country.mmdb");
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("geoip", "DatabaseReader failed: {0}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
GlobalFileSystem.Mount(Platform.GameDir); // Needed to access shaders
|
GlobalFileSystem.Mount(Platform.GameDir); // Needed to access shaders
|
||||||
var renderers = new[] { Settings.Graphics.Renderer, "Sdl2", null };
|
var renderers = new[] { Settings.Graphics.Renderer, "Sdl2", null };
|
||||||
|
|||||||
52
OpenRA.Game/Network/GeoIP.cs
Normal file
52
OpenRA.Game/Network/GeoIP.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2015 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. For more information,
|
||||||
|
* see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using ICSharpCode.SharpZipLib.Core;
|
||||||
|
using ICSharpCode.SharpZipLib.GZip;
|
||||||
|
using MaxMind.GeoIP2;
|
||||||
|
|
||||||
|
namespace OpenRA.Network
|
||||||
|
{
|
||||||
|
public class GeoIP
|
||||||
|
{
|
||||||
|
static DatabaseReader database;
|
||||||
|
|
||||||
|
public static void Initialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var fileStream = new FileStream("GeoLite2-Country.mmdb.gz", FileMode.Open, FileAccess.Read))
|
||||||
|
using (var gzipStream = new GZipInputStream(fileStream))
|
||||||
|
database = new DatabaseReader(gzipStream);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("geoip", "DatabaseReader failed: {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string LookupCountry(string ip)
|
||||||
|
{
|
||||||
|
const string Unknown = "Unknown Location";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return database.Country(ip).Country.Name ?? Unknown;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("geoip", "LookupCountry failed: {0}", e);
|
||||||
|
return Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -128,6 +128,7 @@
|
|||||||
<Compile Include="Network\Connection.cs" />
|
<Compile Include="Network\Connection.cs" />
|
||||||
<Compile Include="Network\FrameData.cs" />
|
<Compile Include="Network\FrameData.cs" />
|
||||||
<Compile Include="Network\GameServer.cs" />
|
<Compile Include="Network\GameServer.cs" />
|
||||||
|
<Compile Include="Network\GeoIP.cs" />
|
||||||
<Compile Include="Network\Handshake.cs" />
|
<Compile Include="Network\Handshake.cs" />
|
||||||
<Compile Include="Network\Order.cs" />
|
<Compile Include="Network\Order.cs" />
|
||||||
<Compile Include="Network\OrderIO.cs" />
|
<Compile Include="Network\OrderIO.cs" />
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
address = UPnP.NatDevice.GetExternalIP().ToString();
|
address = UPnP.NatDevice.GetExternalIP().ToString();
|
||||||
var cachedDescriptiveIP = LobbyUtils.DescriptiveIpAddress(address);
|
var cachedDescriptiveIP = LobbyUtils.DescriptiveIpAddress(address);
|
||||||
ip.GetText = () => cachedDescriptiveIP;
|
ip.GetText = () => cachedDescriptiveIP;
|
||||||
var cachedCountryLookup = LobbyUtils.LookupCountry(address);
|
var cachedCountryLookup = GeoIP.LookupCountry(address);
|
||||||
location.GetText = () => cachedCountryLookup;
|
location.GetText = () => cachedCountryLookup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,21 +235,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string LookupCountry(string ip)
|
|
||||||
{
|
|
||||||
const string Unknown = "Unknown Location";
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Game.GeoIpDatabase.Country(ip).Country.Name ?? Unknown;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("geoip", "LookupCountry failed: {0}", e);
|
|
||||||
return Unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SetupClientWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, bool visible)
|
public static void SetupClientWidget(Widget parent, Session.Slot s, Session.Client c, OrderManager orderManager, bool visible)
|
||||||
{
|
{
|
||||||
parent.Get("ADMIN_INDICATOR").IsVisible = () => c.IsAdmin;
|
parent.Get("ADMIN_INDICATOR").IsVisible = () => c.IsAdmin;
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
var location = item.GetOrNull<LabelWidget>("LOCATION");
|
var location = item.GetOrNull<LabelWidget>("LOCATION");
|
||||||
if (location != null)
|
if (location != null)
|
||||||
{
|
{
|
||||||
var cachedServerLocation = LobbyUtils.LookupCountry(game.Address.Split(':')[0]);
|
var cachedServerLocation = GeoIP.LookupCountry(game.Address.Split(':')[0]);
|
||||||
location.GetText = () => cachedServerLocation;
|
location.GetText = () => cachedServerLocation;
|
||||||
location.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : location.TextColor;
|
location.GetColor = () => !compatible ? Color.DarkGray : !canJoin ? Color.LightGray : location.TextColor;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user