3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "threading/mutex_auto_lock.h"
26 #include "util/strfnd.h"
27 #include "util/string.h"
31 BanManager::BanManager(const std::string &banfilepath) : m_banfilepath(banfilepath)
35 } catch (SerializationError &e) {
36 infostream << "BanManager: creating " << m_banfilepath << std::endl;
40 BanManager::~BanManager()
45 void BanManager::load()
47 MutexAutoLock lock(m_mutex);
48 infostream << "BanManager: loading from " << m_banfilepath << std::endl;
49 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
51 infostream << "BanManager: failed loading from " << m_banfilepath
53 throw SerializationError("BanManager::load(): Couldn't open file");
56 while (!is.eof() && is.good()) {
58 std::getline(is, line, '\n');
60 std::string ip = trim(f.next("|"));
61 std::string name = trim(f.next("|"));
69 void BanManager::save()
71 MutexAutoLock lock(m_mutex);
72 infostream << "BanManager: saving to " << m_banfilepath << std::endl;
73 std::ostringstream ss(std::ios_base::binary);
75 for (const auto &ip : m_ips)
76 ss << ip.first << "|" << ip.second << "\n";
78 if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
79 infostream << "BanManager: failed saving to " << m_banfilepath
81 throw SerializationError("BanManager::save(): Couldn't write file");
87 bool BanManager::isIpBanned(const std::string &ip)
89 MutexAutoLock lock(m_mutex);
90 return m_ips.find(ip) != m_ips.end();
93 std::string BanManager::getBanDescription(const std::string &ip_or_name)
95 MutexAutoLock lock(m_mutex);
97 for (const auto &ip : m_ips) {
98 if (ip.first == ip_or_name || ip.second == ip_or_name ||
100 s += ip.first + "|" + ip.second + ", ";
103 s = s.substr(0, s.size() - 2);
107 std::string BanManager::getBanName(const std::string &ip)
109 MutexAutoLock lock(m_mutex);
110 StringMap::iterator it = m_ips.find(ip);
111 if (it == m_ips.end())
116 void BanManager::add(const std::string &ip, const std::string &name)
118 MutexAutoLock lock(m_mutex);
123 void BanManager::remove(const std::string &ip_or_name)
125 MutexAutoLock lock(m_mutex);
126 for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
127 if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
136 bool BanManager::isModified()
138 MutexAutoLock lock(m_mutex);