handlerlogin: use std::atomic for counter and lockguard in ban check

这个提交包含在:
Albert S. 2019-05-12 21:14:26 +02:00
父节点 93aea9ed7b
当前提交 d7db1fbe39

查看文件

@ -18,12 +18,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <atomic>
#include <mutex>
#include <openssl/evp.h> #include <openssl/evp.h>
#include "handlerlogin.h" #include "handlerlogin.h"
#include "../logger.h" #include "../logger.h"
struct LoginFail struct LoginFail
{ {
unsigned int count; std::mutex mutex;
std::atomic<unsigned int> count;
time_t lastfail; time_t lastfail;
}; };
static std::map<std::string, LoginFail> loginFails; static std::map<std::string, LoginFail> loginFails;
@ -34,6 +37,7 @@ bool HandlerLogin::isBanned(std::string ip)
if(utils::hasKey(loginFails, ip)) if(utils::hasKey(loginFails, ip))
{ {
LoginFail &fl = loginFails[ip]; LoginFail &fl = loginFails[ip];
std::lock_guard<std::mutex> lock(fl.mutex);
return fl.count > 5 && (time(nullptr) - fl.lastfail) < 1200; return fl.count > 5 && (time(nullptr) - fl.lastfail) < 1200;
} }
return false; return false;