Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
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

27
IpLocationStore::IpLocationStore()
28
{
29
}
30

31
IpLocationStore::~IpLocationStore()
32
{
33
}
34

35
void IpLocationStore::Load()
36
{
37
    _ipLocationStore.clear();
38
    TC_LOG_INFO("server.loading", "Loading IP Location Database...");
39

40
    std::string databaseFilePath = sConfigMgr->GetStringDefault("IPLocationFile", "");
41
    if (databaseFilePath.empty())
42
        return;
43

44
    // Check if file exists
45
    std::ifstream databaseFile(databaseFilePath);
46
    if (!databaseFile)
47
    {
48
        TC_LOG_ERROR("server.loading", "IPLocation: No ip database file exists (%s).", databaseFilePath.c_str());
49
        return;
50
    }
51

52
    if (!databaseFile.is_open())
53
    {
54
        TC_LOG_ERROR("server.loading", "IPLocation: Ip database file (%s) can not be opened.", databaseFilePath.c_str());
55
        return;
56
    }
57

58
    std::string ipFrom;
59
    std::string ipTo;
60
    std::string countryCode;
61
    std::string countryName;
62

63
    while (databaseFile.good())
64
    {
65
        // Read lines
66
        if (!std::getline(databaseFile, ipFrom, ','))
67
            break;
68
        if (!std::getline(databaseFile, ipTo, ','))
69
            break;
70
        if (!std::getline(databaseFile, countryCode, ','))
71
            break;
72
        if (!std::getline(databaseFile, countryName, '\n'))
73
            break;
74

75
        // Remove new lines and return
76
        countryName.erase(std::remove(countryName.begin(), countryName.end(), '\r'), countryName.end());
77
        countryName.erase(std::remove(countryName.begin(), countryName.end(), '\n'), countryName.end());
78

79
        // Remove quotation marks
80
        ipFrom.erase(std::remove(ipFrom.begin(), ipFrom.end(), '"'), ipFrom.end());
81
        ipTo.erase(std::remove(ipTo.begin(), ipTo.end(), '"'), ipTo.end());
82
        countryCode.erase(std::remove(countryCode.begin(), countryCode.end(), '"'), countryCode.end());
83
        countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end());
84

85
        // Convert country code to lowercase
86
        std::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

91
    std::sort(_ipLocationStore.begin(), _ipLocationStore.end(), [](IpLocationRecord const& a, IpLocationRecord const& b) { return a.IpFrom < b.IpFrom; });
92
    ASSERT(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

95
    databaseFile.close();
96

97
    TC_LOG_INFO("server.loading", ">> Loaded " SZFMTD " ip location entries.", _ipLocationStore.size());
98
}
99

100
IpLocationRecord const* IpLocationStore::GetLocationRecord(std::string const& ipAddress) const
101
{
102
    boost::system::error_code error;
103
    boost::asio::ip::address_v4 address = Trinity::Net::make_address_v4(ipAddress, error);
104
    if (error)
105
        return nullptr;
106

107
    uint32 ip = Trinity::Net::address_to_uint(address);
108
    auto itr = std::upper_bound(_ipLocationStore.begin(), _ipLocationStore.end(), ip, [](uint32 ip, IpLocationRecord const& loc) { return ip < loc.IpTo; });
109
    if (itr == _ipLocationStore.end())
110
        return nullptr;
111

112
    if (ip < itr->IpFrom)
113
        return nullptr;
114

115
    return &(*itr);
116
}
117

118
IpLocationStore* IpLocationStore::Instance()
119
{
120
    static IpLocationStore instance;
121
    return &instance;
122
}
123

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.