This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwispValidation.cpp
More file actions
75 lines (69 loc) · 1.86 KB
/
wispValidation.cpp
File metadata and controls
75 lines (69 loc) · 1.86 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "wispValidation.hpp"
#include "wispServer.hpp"
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <mutex>
#include <string>
#include <thread>
using namespace std::chrono_literals;
WISP_PACKET_TYPE validatePacket(char *buffer, size_t size) {
if (size < PACKET_SIZE((size_t)sizeof(uint8_t)))
return WISP_NULL;
switch (buffer[0]) {
case WISP_CONNECT: {
if (size < PACKET_SIZE(WISP_PAYLOAD_CONNECT_SIZE))
return WISP_NULL;
uint8_t type = *(uint8_t *)(buffer + PACKET_SIZE(0));
if (type != UDP_TYPE && type != TCP_TYPE)
return WISP_NULL;
return WISP_CONNECT;
}
case WISP_DATA:
if (size < PACKET_SIZE((size_t)sizeof(char)))
return WISP_NULL;
return WISP_DATA;
case WISP_CONTINUE: {
if (size != PACKET_SIZE((size_t)sizeof(uint32_t)))
return WISP_NULL;
return WISP_CONTINUE;
}
case WISP_CLOSE:
if (size != PACKET_SIZE((size_t)sizeof(uint8_t)))
return WISP_NULL;
return WISP_CLOSE;
default:
return WISP_NULL;
}
return WISP_NULL;
}
std::map<void *, unsigned int> forwardsRequested;
std::mutex ratelimitLock;
// true == limit
bool checkRatelimits(void *ip) {
if (maxForward == -1) {
return false;
}
ratelimitLock.lock();
if (forwardsRequested.find(ip) == forwardsRequested.end()) {
forwardsRequested[ip] = 0;
}
forwardsRequested[ip]++;
#ifdef DEBUG
printf("This minute on %p now at %i forwards\n", ip, forwardsRequested[ip]);
#endif // DEBUG
std::thread([ip]() {
std::this_thread::sleep_for(60s);
ratelimitLock.lock();
forwardsRequested[ip]--;
#ifdef DEBUG
printf("This minute on %p now at %i forwards\n", ip, forwardsRequested[ip]);
#endif // DEBUG
if (forwardsRequested[ip] <= 0) {
forwardsRequested.erase(ip);
}
ratelimitLock.unlock();
}).detach();
ratelimitLock.unlock();
return forwardsRequested[ip] > maxForward;
}