aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2018-06-23 21:31:09 +0200
committerShauren <shauren.trinity@gmail.com>2019-01-09 17:35:18 +0100
commit50cfeb9aa406b9f81a6aa51dcd87201e5f3bae1e (patch)
tree5f47b715128418cc91091711f0358e5c2dea961f /src
parent61532e9816f750ae1690f8d5fca92525d6297ab6 (diff)
Core/Misc: Improved ip2location code and set it to disabled by default
(cherry picked from commit 0ead73516adfeaff96292685592085e20737d535)
Diffstat (limited to 'src')
-rw-r--r--src/common/IPLocation/IPLocation.cpp90
-rw-r--r--src/common/IPLocation/IPLocation.h18
-rw-r--r--src/server/bnetserver/Server/Session.cpp4
-rw-r--r--src/server/bnetserver/bnetserver.conf.dist9
-rw-r--r--src/server/game/Server/WorldSocket.cpp4
-rw-r--r--src/server/scripts/Commands/cs_account.cpp4
-rw-r--r--src/server/scripts/Commands/cs_misc.cpp4
-rw-r--r--src/server/worldserver/worldserver.conf.dist9
8 files changed, 67 insertions, 75 deletions
diff --git a/src/common/IPLocation/IPLocation.cpp b/src/common/IPLocation/IPLocation.cpp
index 0c6ff30757e..5153308b868 100644
--- a/src/common/IPLocation/IPLocation.cpp
+++ b/src/common/IPLocation/IPLocation.cpp
@@ -34,93 +34,83 @@ IpLocationStore::~IpLocationStore()
void IpLocationStore::Load()
{
- std::string value = sConfigMgr->GetStringDefault("IPLocationFile", ".");
- if (value.empty())
- return;
-
_ipLocationStore.clear();
TC_LOG_INFO("server.loading", "Loading IP Location Database...");
- // Default folder
- if (!value.compare("."))
- value.append("/IP2LOCATION-LITE-DB1.CSV");
-
- // Check file name
- if (value.find("IP2LOCATION-LITE-DB1.CSV") == std::string::npos)
- {
- TC_LOG_ERROR("server.loading", "IPLocation:: File name is not valid, must be IP2LOCATION-LITE-DB1.CSV");
+ std::string databaseFilePath = sConfigMgr->GetStringDefault("IPLocationFile", "");
+ if (databaseFilePath.empty())
return;
- }
// Check if file exists
- std::ifstream ipfile(value);
- if (!ipfile)
+ std::ifstream databaseFile(databaseFilePath);
+ if (!databaseFile)
{
- TC_LOG_ERROR("server.loading", "IPLocation:: No database file exists.");
+ TC_LOG_ERROR("server.loading", "IPLocation: No ip database file exists (%s).", databaseFilePath.c_str());
return;
}
- if (!ipfile.is_open())
+ if (!databaseFile.is_open())
{
- TC_LOG_ERROR("server.loading", "IPLocation:: The file can not be opened.");
+ TC_LOG_ERROR("server.loading", "IPLocation: Ip database file (%s) can not be opened.", databaseFilePath.c_str());
return;
}
- std::string ip_from;
- std::string ip_to;
- std::string country_code;
- std::string country_name;
+ std::string ipFrom;
+ std::string ipTo;
+ std::string countryCode;
+ std::string countryName;
- while (ipfile.good())
+ while (databaseFile.good())
{
// Read lines
- std::getline(ipfile, ip_from, ',');
- std::getline(ipfile, ip_to, ',');
- std::getline(ipfile, country_code, ',');
- std::getline(ipfile, country_name, '\n');
+ if (!std::getline(databaseFile, ipFrom, ','))
+ break;
+ if (!std::getline(databaseFile, ipTo, ','))
+ break;
+ if (!std::getline(databaseFile, countryCode, ','))
+ break;
+ if (!std::getline(databaseFile, countryName, '\n'))
+ break;
// Remove new lines and return
- country_name.erase(std::remove(country_name.begin(), country_name.end(), '\r'), country_name.end());
- country_name.erase(std::remove(country_name.begin(), country_name.end(), '\n'), country_name.end());
+ countryName.erase(std::remove(countryName.begin(), countryName.end(), '\r'), countryName.end());
+ countryName.erase(std::remove(countryName.begin(), countryName.end(), '\n'), countryName.end());
// Remove quotation marks
- ip_from.erase(std::remove(ip_from.begin(), ip_from.end(), '"'), ip_from.end());
- ip_to.erase(std::remove(ip_to.begin(), ip_to.end(), '"'), ip_to.end());
- country_code.erase(std::remove(country_code.begin(), country_code.end(), '"'), country_code.end());
- country_name.erase(std::remove(country_name.begin(), country_name.end(), '"'), country_name.end());
+ ipFrom.erase(std::remove(ipFrom.begin(), ipFrom.end(), '"'), ipFrom.end());
+ ipTo.erase(std::remove(ipTo.begin(), ipTo.end(), '"'), ipTo.end());
+ countryCode.erase(std::remove(countryCode.begin(), countryCode.end(), '"'), countryCode.end());
+ countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end());
// Convert country code to lowercase
- std::transform(country_code.begin(), country_code.end(), country_code.begin(), ::tolower);
+ std::transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::tolower);
- IpLocationRecord data;
- data.ip_from = (uint32)atoull(ip_from.c_str());
- data.ip_to = (uint32)atoull(ip_to.c_str());
- data.country_code = country_code;
- data.country_name = country_name;
-
- _ipLocationStore.push_back(data);
+ _ipLocationStore.emplace_back(uint32(atoul(ipFrom.c_str())), uint32(atoul(ipTo.c_str())), std::move(countryCode), std::move(countryName));
}
- std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.ip_from < b.ip_from; });
+ std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; });
+ ASSERT(std::is_sorted(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpTo; }),
+ "Overlapping IP ranges detected in database file");
- ipfile.close();
+ databaseFile.close();
- TC_LOG_INFO("server.loading", ">> Loaded %u entries.", uint32(_ipLocationStore.size()));
+ TC_LOG_INFO("server.loading", ">> Loaded " SZFMTD " ip location entries.", _ipLocationStore.size());
}
-IpLocationRecord* IpLocationStore::GetData(std::string const& ipAddress)
+IpLocationRecord const* IpLocationStore::GetLocationRecord(std::string const& ipAddress) const
{
- if (_ipLocationStore.empty())
+ uint32 ip = Trinity::Net::address_to_uint(Trinity::Net::make_address_v4(ipAddress));
+ auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return ip < loc.IpTo; });
+ if (itr == _ipLocationStore.end())
return nullptr;
- uint32 ip = Trinity::Net::address_to_uint(Trinity::Net::make_address_v4(ipAddress));
- auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return loc.ip_to >= ip; });
- ASSERT(&(*itr));
+ if (ip < itr->IpFrom)
+ return nullptr;
return &(*itr);
}
-IpLocationStore* IpLocationStore::instance()
+IpLocationStore* IpLocationStore::Instance()
{
static IpLocationStore instance;
return &instance;
diff --git a/src/common/IPLocation/IPLocation.h b/src/common/IPLocation/IPLocation.h
index a22aafa2e5d..e322fcc1d4c 100644
--- a/src/common/IPLocation/IPLocation.h
+++ b/src/common/IPLocation/IPLocation.h
@@ -21,10 +21,14 @@
struct IpLocationRecord
{
- uint32 ip_from;
- uint32 ip_to;
- std::string country_code;
- std::string country_name;
+ IpLocationRecord() : IpFrom(0), IpTo(0) { }
+ IpLocationRecord(uint32 ipFrom, uint32 ipTo, std::string countryCode, std::string countryName)
+ : IpFrom(ipFrom), IpTo(ipTo), CountryCode(std::move(countryCode)), CountryName(std::move(countryName)) { }
+
+ uint32 IpFrom;
+ uint32 IpTo;
+ std::string CountryCode;
+ std::string CountryName;
};
class TC_COMMON_API IpLocationStore
@@ -32,13 +36,13 @@ class TC_COMMON_API IpLocationStore
public:
IpLocationStore();
~IpLocationStore();
- static IpLocationStore* instance();
+ static IpLocationStore* Instance();
void Load();
- IpLocationRecord* GetData(std::string const& ipAddress);
+ IpLocationRecord const* GetLocationRecord(std::string const& ipAddress) const;
private:
std::vector<IpLocationRecord> _ipLocationStore;
};
-#define sIPLocation IpLocationStore::instance()
+#define sIPLocation IpLocationStore::Instance()
diff --git a/src/server/bnetserver/Server/Session.cpp b/src/server/bnetserver/Server/Session.cpp
index 56c71a3eeb6..7ebb7abb58b 100644
--- a/src/server/bnetserver/Server/Session.cpp
+++ b/src/server/bnetserver/Server/Session.cpp
@@ -336,8 +336,8 @@ uint32 Battlenet::Session::VerifyWebCredentials(std::string const& webCredential
}
else
{
- if (IpLocationRecord* location = sIPLocation->GetData(ip_address))
- _ipCountry = location->country_code;
+ if (IpLocationRecord const* location = sIPLocation->GetLocationRecord(ip_address))
+ _ipCountry = location->CountryCode;
TC_LOG_DEBUG("session", "[Session::HandleVerifyWebCredentials] Account '%s' is not locked to ip", _accountInfo->Login.c_str());
if (_accountInfo->LockCountry.empty() || _accountInfo->LockCountry == "00")
diff --git a/src/server/bnetserver/bnetserver.conf.dist b/src/server/bnetserver/bnetserver.conf.dist
index 0e5ccc18a53..f6fcb5764b4 100644
--- a/src/server/bnetserver/bnetserver.conf.dist
+++ b/src/server/bnetserver/bnetserver.conf.dist
@@ -200,12 +200,11 @@ MySQLExecutable = ""
#
# IPLocationFile
# Description: The path to your IP2Location database CSV file.
-# Example: "C:/Trinity/IP2Location.csv"
-# "/home/trinity/IP2Location.csv"
-# Default: "." - (Current core directory)
-# "" - (Disabled)
+# Example: "C:/Trinity/IP2LOCATION-LITE-DB1.CSV"
+# "/home/trinity/IP2LOCATION-LITE-DB1.CSV"
+# Default: "" - (Disabled)
-IPLocationFile = "."
+IPLocationFile = ""
#
###################################################################################################
diff --git a/src/server/game/Server/WorldSocket.cpp b/src/server/game/Server/WorldSocket.cpp
index 3f45e40a62a..36608a4a075 100644
--- a/src/server/game/Server/WorldSocket.cpp
+++ b/src/server/game/Server/WorldSocket.cpp
@@ -743,8 +743,8 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr<WorldPackets::Auth::
return;
}
- if (IpLocationRecord* location = sIPLocation->GetData(address))
- _ipCountry = location->country_code;
+ if (IpLocationRecord const* location = sIPLocation->GetLocationRecord(address))
+ _ipCountry = location->CountryCode;
///- Re-check ip locking (same check as in auth).
if (account.BattleNet.IsLockedToIP)
diff --git a/src/server/scripts/Commands/cs_account.cpp b/src/server/scripts/Commands/cs_account.cpp
index 2cb4ef8f0a3..f162b6465be 100644
--- a/src/server/scripts/Commands/cs_account.cpp
+++ b/src/server/scripts/Commands/cs_account.cpp
@@ -291,10 +291,10 @@ public:
{
if (param == "on")
{
- if (IpLocationRecord* location = sIPLocation->GetData(handler->GetSession()->GetRemoteAddress()))
+ if (IpLocationRecord const* location = sIPLocation->GetLocationRecord(handler->GetSession()->GetRemoteAddress()))
{
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_ACCOUNT_LOCK_COUNTRY);
- stmt->setString(0, location->country_code);
+ stmt->setString(0, location->CountryCode);
stmt->setUInt32(1, handler->GetSession()->GetAccountId());
LoginDatabase.Execute(stmt);
handler->PSendSysMessage(LANG_COMMAND_ACCLOCKLOCKED);
diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp
index 417153bfeff..56b7071a8a8 100644
--- a/src/server/scripts/Commands/cs_misc.cpp
+++ b/src/server/scripts/Commands/cs_misc.cpp
@@ -1769,10 +1769,10 @@ public:
lastIp = fields[4].GetString();
lastLogin = fields[5].GetString();
- if (IpLocationRecord* location = sIPLocation->GetData(lastIp))
+ if (IpLocationRecord const* location = sIPLocation->GetLocationRecord(lastIp))
{
lastIp.append(" (");
- lastIp.append(location->country_name);
+ lastIp.append(location->CountryName);
lastIp.append(")");
}
}
diff --git a/src/server/worldserver/worldserver.conf.dist b/src/server/worldserver/worldserver.conf.dist
index 79005a69d3e..cfcde52c061 100644
--- a/src/server/worldserver/worldserver.conf.dist
+++ b/src/server/worldserver/worldserver.conf.dist
@@ -229,12 +229,11 @@ MySQLExecutable = ""
#
# IPLocationFile
# Description: The path to your IP2Location database CSV file.
-# Example: "C:/Trinity/IP2Location.csv"
-# "/home/trinity/IP2Location.csv"
-# Default: "." - (Current core directory)
-# "" - (Disabled)
+# Example: "C:/Trinity/IP2LOCATION-LITE-DB1.CSV"
+# "/home/trinity/IP2LOCATION-LITE-DB1.CSV"
+# Default: "" - (Disabled)
-IPLocationFile = "."
+IPLocationFile = ""
#
###################################################################################################