-
Notifications
You must be signed in to change notification settings - Fork 1
/
gate.hpp
53 lines (42 loc) · 909 Bytes
/
gate.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by assasin on 30.10.17.
//
#ifndef WSSERVER_GATE_HPP
#define WSSERVER_GATE_HPP
#include "redis.hpp"
#include <string>
#include <jsoncpp/json/json.h>
class Gate {
protected:
Redis md;
bool _tries(const std::string &name, const std::string &key, int tries, int timeout) {
Json::Value val;
string mdkey = "gate-" + name + "-" + key;
int cnt;
if (!md.get(mdkey, cnt)) {
cnt = 0;
}
++cnt;
if (cnt > tries) {
return false;
}
md.set(mdkey, cnt, timeout);
return true;
}
void _resetTries(const std::string &name, const std::string &key) {
string mdkey = "gate-" + name + "-" + key;
int cnt = 0;
if (md.get(mdkey, cnt) && cnt > 0) {
md.set(mdkey, 0, 1);
}
}
public:
bool auth(const string &ip, bool reset = false) {
if (reset) {
_resetTries("auth", ip);
return true;
}
return _tries("auth", ip, 4, 5*60);
}
};
#endif //WSSERVER_GATE_HPP