mirror of https://github.com/lumapu/ahoy.git
Browse Source
* prevent sending commands to inverter which isn't active #1387 * protect commands from popup in `/live` if password is set #1199pull/1402/head
lumapu
12 months ago
12 changed files with 156 additions and 58 deletions
@ -0,0 +1,7 @@ |
|||||
|
//-----------------------------------------------------------------------------
|
||||
|
// 2024 Ahoy, https://github.com/lumpapu/ahoy
|
||||
|
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
||||
|
//-----------------------------------------------------------------------------
|
||||
|
|
||||
|
#include "Protection.h" |
||||
|
Protection *Protection::mInstance = nullptr; |
@ -0,0 +1,93 @@ |
|||||
|
//-----------------------------------------------------------------------------
|
||||
|
// 2024 Ahoy, https://github.com/lumpapu/ahoy
|
||||
|
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
||||
|
//-----------------------------------------------------------------------------
|
||||
|
|
||||
|
#ifndef __PROTECTION_H__ |
||||
|
#define __PROTECTION_H__ |
||||
|
#pragma once |
||||
|
|
||||
|
#include <array> |
||||
|
#include <cstdint> |
||||
|
|
||||
|
#include "../config/config.h" |
||||
|
#include "../utils/helper.h" |
||||
|
|
||||
|
class Protection { |
||||
|
protected: |
||||
|
Protection(const char *pwd) { |
||||
|
mPwd = pwd; |
||||
|
mLogoutTimeout = 0; |
||||
|
mLoginIp.fill(0); |
||||
|
|
||||
|
// no password set - unlock
|
||||
|
if(pwd[0] == '\0') |
||||
|
mProtected = false; |
||||
|
} |
||||
|
|
||||
|
public: |
||||
|
Protection(Protection &other) = delete; |
||||
|
void operator=(const Protection &) = delete; |
||||
|
|
||||
|
static Protection* getInstance(const char *pwd) { |
||||
|
if(nullptr == mInstance) |
||||
|
mInstance = new Protection(pwd); |
||||
|
return mInstance; |
||||
|
} |
||||
|
|
||||
|
void tickSecond() { |
||||
|
// auto logout
|
||||
|
if(0 != mLogoutTimeout) { |
||||
|
if (0 == --mLogoutTimeout) { |
||||
|
if(mPwd[0] == '\0') |
||||
|
mProtected = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void lock(void) { |
||||
|
mProtected = true; |
||||
|
mLoginIp.fill(0); |
||||
|
} |
||||
|
|
||||
|
void unlock(const char *clientIp) { |
||||
|
mProtected = false; |
||||
|
ah::ip2Arr(static_cast<uint8_t*>(mLoginIp.data()), clientIp); |
||||
|
} |
||||
|
|
||||
|
void resetLockTimeout(void) { |
||||
|
mLogoutTimeout = LOGOUT_TIMEOUT; |
||||
|
} |
||||
|
|
||||
|
bool isProtected(void) const { |
||||
|
return mProtected; |
||||
|
} |
||||
|
|
||||
|
bool isProtected(const char *clientIp) const { |
||||
|
if(mProtected) |
||||
|
return true; |
||||
|
|
||||
|
if(mPwd[0] == '\0') |
||||
|
return false; |
||||
|
|
||||
|
uint8_t ip[4]; |
||||
|
ah::ip2Arr(ip, clientIp); |
||||
|
for(uint8_t i = 0; i < 4; i++) { |
||||
|
if(mLoginIp[i] != ip[i]) |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
protected: |
||||
|
static Protection *mInstance; |
||||
|
|
||||
|
private: |
||||
|
const char *mPwd; |
||||
|
bool mProtected = true; |
||||
|
uint16_t mLogoutTimeout = LOGOUT_TIMEOUT; |
||||
|
std::array<uint8_t, 4> mLoginIp; |
||||
|
}; |
||||
|
|
||||
|
#endif /*__PROTECTION_H__*/ |
Loading…
Reference in new issue