Core/Misc: Improved ip2location code and set it to disabled by default

This commit is contained in:
Shauren
2018-06-23 21:31:09 +02:00
parent 1c7f9a2ad8
commit 0ead73516a
8 changed files with 67 additions and 75 deletions

View File

@@ -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;

View File

@@ -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()

View File

@@ -341,8 +341,8 @@ void AuthSession::LogonChallengeCallback(PreparedQueryResult result)
}
else
{
if (IpLocationRecord* location = sIPLocation->GetData(ipAddress))
_ipCountry = location->country_code;
if (IpLocationRecord const* location = sIPLocation->GetLocationRecord(ipAddress))
_ipCountry = location->CountryCode;
TC_LOG_DEBUG("server.authserver", "[AuthChallenge] Account '%s' is not locked to ip", _accountInfo.Login.c_str());
if (_accountInfo.LockCountry.empty() || _accountInfo.LockCountry == "00")

View File

@@ -160,12 +160,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 = ""
#
###################################################################################################

View File

@@ -520,8 +520,8 @@ void WorldSocket::HandleAuthSessionCallback(std::shared_ptr<AuthSession> authSes
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.IsLockedToIP)

View File

@@ -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);

View File

@@ -1689,10 +1689,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(")");
}
}

View File

@@ -211,12 +211,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 = ""
#
###################################################################################################