#pragma once #include // CUSTOM (TeTeHacko): a cap on wrong passwords. Upstream checks every login, // so a password can be guessed over the air as fast as the channel carries // packets, from a fresh identity each time, and a wrong one is not even // answered. After LOGIN_FAIL_MAX wrong passwords within LOGIN_FAIL_SECS no // password is checked until that window ends. // // The caller lets a client that is already an admin in the ACL through anyway: // a login is encrypted with the sender's key, so nobody can send one in an // admin's name, and a guessing attack cannot lock the admins out. // // Times are millis(), compared by difference: a clock set backwards or the // 49-day wrap cannot stretch the window. #ifndef LOGIN_FAIL_MAX #define LOGIN_FAIL_MAX 4 #endif #ifndef LOGIN_FAIL_SECS #define LOGIN_FAIL_SECS 180 #endif class LoginGuard { uint32_t _since = 0; // millis() of the first failure in the window uint16_t _fails = 0; bool inWindow(uint32_t now_ms) const { return now_ms - _since < LOGIN_FAIL_SECS * 1000UL; } public: bool locked(uint32_t now_ms) const { return _fails >= LOGIN_FAIL_MAX && inWindow(now_ms); } void failed(uint32_t now_ms) { if (_fails == 0 || !inWindow(now_ms)) { _since = now_ms; _fails = 0; } if (_fails < LOGIN_FAIL_MAX) _fails++; } };