-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.hpp
46 lines (36 loc) · 1 KB
/
utils.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
//
// Created by assasin on 03.05.19.
//
#ifndef WSSERVER_UTILS_HPP
#define WSSERVER_UTILS_HPP
#include <string>
#include <random>
#include "server_fwd.hpp"
inline std::string getRealClientIp(ConnectionPtr connection) {
auto iphdr = connection->header.find("X-Real-IP");
if (iphdr != connection->header.end()) {
return iphdr->second;
}
return connection->remote_endpoint().address().to_string();
}
class Keygen {
public:
Keygen() {
std::random_device dev;
generator.seed(dev()); // may throw on very specific OS/devices
distribution = std::uniform_int_distribution<uint64_t>(0, alphabet.size() - 1);
}
std::string generate(size_t len) {
std::string result;
result.reserve(len);
for (size_t i = 0; i < len; ++i) {
result += alphabet[distribution(generator)];
}
return result;
}
private:
std::string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
std::mt19937_64 generator;
std::uniform_int_distribution<uint64_t> distribution;
};
#endif //WSSERVER_UTILS_HPP