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):
32 m_banfilepath(banfilepath)
36 } catch(SerializationError &e) {
37 infostream << "BanManager: creating "
38 << m_banfilepath << std::endl;
42 BanManager::~BanManager()
47 void BanManager::load()
49 MutexAutoLock lock(m_mutex);
50 infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
51 std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
53 infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
54 throw SerializationError("BanManager::load(): Couldn't open file");
57 while (!is.eof() && is.good()) {
59 std::getline(is, line, '\n');
61 std::string ip = trim(f.next("|"));
62 std::string name = trim(f.next("|"));
70 void BanManager::save()
72 MutexAutoLock lock(m_mutex);
73 infostream << "BanManager: saving to " << m_banfilepath << std::endl;
74 std::ostringstream ss(std::ios_base::binary);
76 for (const auto &ip : m_ips)
77 ss << ip.first << "|" << ip.second << "\n";
79 if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
80 infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
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
99 || ip_or_name.empty()) {
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)) {
137 bool BanManager::isModified()
139 MutexAutoLock lock(m_mutex);