ban.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "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. m_modified(false)
  28. {
  29. try{
  30. load();
  31. }
  32. catch(SerializationError &e)
  33. {
  34. warningstream<<"BanManager: creating "
  35. <<m_banfilepath<<std::endl;
  36. }
  37. }
  38. BanManager::~BanManager()
  39. {
  40. save();
  41. }
  42. void BanManager::load()
  43. {
  44. MutexAutoLock lock(m_mutex);
  45. infostream<<"BanManager: loading from "<<m_banfilepath<<std::endl;
  46. std::ifstream is(m_banfilepath.c_str(), std::ios::binary);
  47. if(is.good() == false)
  48. {
  49. infostream<<"BanManager: failed loading from "<<m_banfilepath<<std::endl;
  50. throw SerializationError("BanManager::load(): Couldn't open file");
  51. }
  52. while(!is.eof() && is.good())
  53. {
  54. std::string line;
  55. std::getline(is, line, '\n');
  56. Strfnd f(line);
  57. std::string ip = trim(f.next("|"));
  58. std::string name = trim(f.next("|"));
  59. if(!ip.empty()) {
  60. m_ips[ip] = name;
  61. }
  62. }
  63. m_modified = false;
  64. }
  65. void BanManager::save()
  66. {
  67. MutexAutoLock lock(m_mutex);
  68. infostream << "BanManager: saving to " << m_banfilepath << std::endl;
  69. std::ostringstream ss(std::ios_base::binary);
  70. for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it)
  71. ss << it->first << "|" << it->second << "\n";
  72. if (!fs::safeWriteToFile(m_banfilepath, ss.str())) {
  73. infostream << "BanManager: failed saving to " << m_banfilepath << std::endl;
  74. throw SerializationError("BanManager::save(): Couldn't write file");
  75. }
  76. m_modified = false;
  77. }
  78. bool BanManager::isIpBanned(const std::string &ip)
  79. {
  80. MutexAutoLock lock(m_mutex);
  81. return m_ips.find(ip) != m_ips.end();
  82. }
  83. std::string BanManager::getBanDescription(const std::string &ip_or_name)
  84. {
  85. MutexAutoLock lock(m_mutex);
  86. std::string s = "";
  87. for (StringMap::iterator it = m_ips.begin(); it != m_ips.end(); ++it) {
  88. if (it->first == ip_or_name || it->second == ip_or_name
  89. || ip_or_name == "") {
  90. s += it->first + "|" + it->second + ", ";
  91. }
  92. }
  93. s = s.substr(0, s.size() - 2);
  94. return s;
  95. }
  96. std::string BanManager::getBanName(const std::string &ip)
  97. {
  98. MutexAutoLock lock(m_mutex);
  99. StringMap::iterator it = m_ips.find(ip);
  100. if (it == m_ips.end())
  101. return "";
  102. return it->second;
  103. }
  104. void BanManager::add(const std::string &ip, const std::string &name)
  105. {
  106. MutexAutoLock lock(m_mutex);
  107. m_ips[ip] = name;
  108. m_modified = true;
  109. }
  110. void BanManager::remove(const std::string &ip_or_name)
  111. {
  112. MutexAutoLock lock(m_mutex);
  113. for (StringMap::iterator it = m_ips.begin(); it != m_ips.end();) {
  114. if ((it->first == ip_or_name) || (it->second == ip_or_name)) {
  115. m_ips.erase(it++);
  116. } else {
  117. ++it;
  118. }
  119. }
  120. m_modified = true;
  121. }
  122. bool BanManager::isModified()
  123. {
  124. MutexAutoLock lock(m_mutex);
  125. return m_modified;
  126. }