ban.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "ban.h"
  17. #include <fstream>
  18. #include "threading/mutex_auto_lock.h"
  19. #include <sstream>
  20. #include <set>
  21. #include "util/strfnd.h"
  22. #include "util/string.h"
  23. #include "log.h"
  24. #include "filesys.h"
  25. BanManager::BanManager(const std::string &banfilepath):
  26. m_banfilepath(banfilepath)
  27. {
  28. try {
  29. load();
  30. } catch(SerializationError &e) {
  31. infostream << "BanManager: creating "
  32. << m_banfilepath << std::endl;
  33. }
  34. }
  35. BanManager::~BanManager()
  36. {
  37. save();
  38. }
  39. void BanManager::load()
  40. {
  41. MutexAutoLock lock(m_mutex);
  42. infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
  43. std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
  44. if (!is.good()) {
  45. infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
  46. throw SerializationError("BanManager::load(): Couldn't open file");
  47. }
  48. while (!is.eof() && is.good()) {
  49. std::string line;
  50. std::getline(is, line, '\n');
  51. Strfnd f(line);
  52. std::string ip = trim(f.next("|"));
  53. std::string name = trim(f.next("|"));
  54. if(!ip.empty()) {
  55. m_ips[ip] = name;
  56. }
  57. }
  58. m_modified = false;
  59. }
  60. void BanManager::save()
  61. {
  62. MutexAutoLock lock(m_mutex);
  63. infostream << "BanManager: saving to " << m_banfilepath << std::endl;
  64. std::ostringstream ss(std::ios_base::binary);
  65. for (const auto &ip : m_ips)
  66. ss << ip.first << "|" << ip.second << "\n";
  67. if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
  68. infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
  69. throw SerializationError("BanManager::save(): Couldn't write file");
  70. }
  71. m_modified = false;
  72. }
  73. bool BanManager::isIpBanned(const std::string &ip)
  74. {
  75. MutexAutoLock lock(m_mutex);
  76. return m_ips.find(ip) != m_ips.end();
  77. }
  78. std::string BanManager::getBanDescription(const std::string &ip_or_name)
  79. {
  80. MutexAutoLock lock(m_mutex);
  81. std::string s;
  82. for (const auto &ip : m_ips) {
  83. if (ip.first == ip_or_name || ip.second == ip_or_name
  84. || ip_or_name.empty()) {
  85. s += ip.first + "|" + ip.second + ", ";
  86. }
  87. }
  88. s = s.substr(0, s.size() - 2);
  89. return s;
  90. }
  91. std::string BanManager::getBanName(const std::string &ip)
  92. {
  93. MutexAutoLock lock(m_mutex);
  94. StringMap::iterator it = m_ips.find(ip);
  95. if (it == m_ips.end())
  96. return "";
  97. return it->second;
  98. }
  99. void BanManager::add(const std::string &ip, const std::string &name)
  100. {
  101. MutexAutoLock lock(m_mutex);
  102. m_ips[ip] = name;
  103. m_modified = true;
  104. }
  105. void BanManager::remove(const std::string &ip_or_name)
  106. {
  107. MutexAutoLock lock(m_mutex);
  108. for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
  109. if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
  110. m_ips.erase(it++);
  111. } else {
  112. ++it;
  113. }
  114. }
  115. m_modified = true;
  116. }
  117. bool BanManager::isModified()
  118. {
  119. MutexAutoLock lock(m_mutex);
  120. return m_modified;
  121. }