ban.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "jthread/jmutexautolock.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. infostream<<"WARNING: BanManager: creating "
  35. <<m_banfilepath<<std::endl;
  36. }
  37. }
  38. BanManager::~BanManager()
  39. {
  40. save();
  41. }
  42. void BanManager::load()
  43. {
  44. JMutexAutoLock 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. JMutexAutoLock lock(m_mutex);
  68. infostream<<"BanManager: saving to "<<m_banfilepath<<std::endl;
  69. std::ostringstream ss(std::ios_base::binary);
  70. for(std::map<std::string, std::string>::iterator
  71. i = m_ips.begin();
  72. i != m_ips.end(); i++)
  73. {
  74. ss << i->first << "|" << i->second << "\n";
  75. }
  76. if(!fs::safeWriteToFile(m_banfilepath, ss.str())) {
  77. infostream<<"BanManager: failed saving to "<<m_banfilepath<<std::endl;
  78. throw SerializationError("BanManager::save(): Couldn't write file");
  79. }
  80. m_modified = false;
  81. }
  82. bool BanManager::isIpBanned(const std::string &ip)
  83. {
  84. JMutexAutoLock lock(m_mutex);
  85. return m_ips.find(ip) != m_ips.end();
  86. }
  87. std::string BanManager::getBanDescription(const std::string &ip_or_name)
  88. {
  89. JMutexAutoLock lock(m_mutex);
  90. std::string s = "";
  91. for(std::map<std::string, std::string>::iterator
  92. i = m_ips.begin();
  93. i != m_ips.end(); i++)
  94. {
  95. if(i->first == ip_or_name || i->second == ip_or_name
  96. || ip_or_name == "")
  97. s += i->first + "|" + i->second + ", ";
  98. }
  99. s = s.substr(0, s.size()-2);
  100. return s;
  101. }
  102. std::string BanManager::getBanName(const std::string &ip)
  103. {
  104. JMutexAutoLock lock(m_mutex);
  105. std::map<std::string, std::string>::iterator i = m_ips.find(ip);
  106. if(i == m_ips.end())
  107. return "";
  108. return i->second;
  109. }
  110. void BanManager::add(const std::string &ip, const std::string &name)
  111. {
  112. JMutexAutoLock lock(m_mutex);
  113. m_ips[ip] = name;
  114. m_modified = true;
  115. }
  116. void BanManager::remove(const std::string &ip_or_name)
  117. {
  118. JMutexAutoLock lock(m_mutex);
  119. for(std::map<std::string, std::string>::iterator
  120. i = m_ips.begin();
  121. i != m_ips.end();)
  122. {
  123. if((i->first == ip_or_name) || (i->second == ip_or_name)) {
  124. m_ips.erase(i++);
  125. } else {
  126. ++i;
  127. }
  128. }
  129. m_modified = true;
  130. }
  131. bool BanManager::isModified()
  132. {
  133. JMutexAutoLock lock(m_mutex);
  134. return m_modified;
  135. }