Legends-of-Azeroth-Pandaria-5.4.8
122 строки · 4.2 Кб
1/*
2* This file is part of the Pandaria 5.4.8 Project. See THANKS file for Copyright information
3*
4* This program is free software; you can redistribute it and/or modify it
5* under the terms of the GNU General Public License as published by the
6* Free Software Foundation; either version 2 of the License, or (at your
7* option) any later version.
8*
9* This program is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12* more details.
13*
14* You should have received a copy of the GNU General Public License along
15* with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "IPLocation.h"
19#include "Common.h"
20#include "Config.h"
21#include "Errors.h"
22#include "IpAddress.h"
23#include "Log.h"
24#include <fstream>
25#include <iostream>
26
27IpLocationStore::IpLocationStore()
28{
29}
30
31IpLocationStore::~IpLocationStore()
32{
33}
34
35void IpLocationStore::Load()
36{
37_ipLocationStore.clear();
38TC_LOG_INFO("server.loading", "Loading IP Location Database...");
39
40std::string databaseFilePath = sConfigMgr->GetStringDefault("IPLocationFile", "");
41if (databaseFilePath.empty())
42return;
43
44// Check if file exists
45std::ifstream databaseFile(databaseFilePath);
46if (!databaseFile)
47{
48TC_LOG_ERROR("server.loading", "IPLocation: No ip database file exists (%s).", databaseFilePath.c_str());
49return;
50}
51
52if (!databaseFile.is_open())
53{
54TC_LOG_ERROR("server.loading", "IPLocation: Ip database file (%s) can not be opened.", databaseFilePath.c_str());
55return;
56}
57
58std::string ipFrom;
59std::string ipTo;
60std::string countryCode;
61std::string countryName;
62
63while (databaseFile.good())
64{
65// Read lines
66if (!std::getline(databaseFile, ipFrom, ','))
67break;
68if (!std::getline(databaseFile, ipTo, ','))
69break;
70if (!std::getline(databaseFile, countryCode, ','))
71break;
72if (!std::getline(databaseFile, countryName, '\n'))
73break;
74
75// Remove new lines and return
76countryName.erase(std::remove(countryName.begin(), countryName.end(), '\r'), countryName.end());
77countryName.erase(std::remove(countryName.begin(), countryName.end(), '\n'), countryName.end());
78
79// Remove quotation marks
80ipFrom.erase(std::remove(ipFrom.begin(), ipFrom.end(), '"'), ipFrom.end());
81ipTo.erase(std::remove(ipTo.begin(), ipTo.end(), '"'), ipTo.end());
82countryCode.erase(std::remove(countryCode.begin(), countryCode.end(), '"'), countryCode.end());
83countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end());
84
85// Convert country code to lowercase
86std::transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::tolower);
87
88_ipLocationStore.emplace_back(uint32(atoul(ipFrom.c_str())), uint32(atoul(ipTo.c_str())), std::move(countryCode), std::move(countryName));
89}
90
91std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; });
92ASSERT(std::is_sorted(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpTo; }),
93"Overlapping IP ranges detected in database file");
94
95databaseFile.close();
96
97TC_LOG_INFO("server.loading", ">> Loaded " SZFMTD " ip location entries.", _ipLocationStore.size());
98}
99
100IpLocationRecord const* IpLocationStore::GetLocationRecord(std::string const& ipAddress) const
101{
102boost::system::error_code error;
103boost::asio::ip::address_v4 address = Trinity::Net::make_address_v4(ipAddress, error);
104if (error)
105return nullptr;
106
107uint32 ip = Trinity::Net::address_to_uint(address);
108auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return ip < loc.IpTo; });
109if (itr == _ipLocationStore.end())
110return nullptr;
111
112if (ip < itr->IpFrom)
113return nullptr;
114
115return &(*itr);
116}
117
118IpLocationStore* IpLocationStore::Instance()
119{
120static IpLocationStore instance;
121return &instance;
122}
123