34{
36 LOG_INFO(
"server.loading",
"Loading IP Location Database...");
37
38 std::string databaseFilePath =
sConfigMgr->GetOption<std::string>(
"IPLocationFile",
"");
39 if (databaseFilePath.empty())
40 {
42 return;
43 }
44
45
46 std::ifstream databaseFile(databaseFilePath);
47 if (!databaseFile)
48 {
49 LOG_ERROR(
"server.loading",
"IPLocation: No ip database file exists ({}).", databaseFilePath);
50 return;
51 }
52
53 if (!databaseFile.is_open())
54 {
55 LOG_ERROR(
"server.loading",
"IPLocation: Ip database file ({}) can not be opened.", databaseFilePath);
56 return;
57 }
58
59 std::string ipFrom;
60 std::string ipTo;
61 std::string countryCode;
62 std::string countryName;
63
64 while (databaseFile.good())
65 {
66
67 if (!std::getline(databaseFile, ipFrom, ','))
68 break;
69 if (!std::getline(databaseFile, ipTo, ','))
70 break;
71 if (!std::getline(databaseFile, countryCode, ','))
72 break;
73 if (!std::getline(databaseFile, countryName, '\n'))
74 break;
75
76
77 countryName.erase(std::remove(countryName.begin(), countryName.end(), '\r'), countryName.end());
78 countryName.erase(std::remove(countryName.begin(), countryName.end(), '\n'), countryName.end());
79
80
81 ipFrom.erase(std::remove(ipFrom.begin(), ipFrom.end(), '"'), ipFrom.end());
82 ipTo.erase(std::remove(ipTo.begin(), ipTo.end(), '"'), ipTo.end());
83 countryCode.erase(std::remove(countryCode.begin(), countryCode.end(), '"'), countryCode.end());
84 countryName.erase(std::remove(countryName.begin(), countryName.end(), '"'), countryName.end());
85
86
87 std::transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::tolower);
88
89 auto IpFrom = Acore::StringTo<uint32>(ipFrom);
90 auto IpTo = Acore::StringTo<uint32>(ipTo);
91
92 if (!IpFrom || !IpTo)
93 continue;
94
95 _ipLocationStore.emplace_back(*IpFrom, *IpTo, std::move(countryCode), std::move(countryName));
96 }
97
100 "Overlapping IP ranges detected in database file");
101
102 databaseFile.close();
103
106}
#define ASSERT
Definition: Errors.h:68
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
#define sConfigMgr
Definition: Config.h:74