aboutsummaryrefslogtreecommitdiff
path: root/server/src/client/blacklist.h
blob: 397dc6c67ba1000db1045e855f86392c723a8b96 (plain) (blame)
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
#pragma once

namespace tcp {

struct blacklist_data {
	std::string ip;
	std::string hwid;
};

class blacklist {

nlohmann::json m_data;
std::string m_name;

public:
	void init(const std::string_view file = "blacklist") {
		m_name = file;

		std::string data;
		if(!io::read_file(file, data))
			return;

		if(!nlohmann::json::accept(data)) {
			io::logger->error("blacklist file isnt valid json.");
			return;
		}

		m_data = nlohmann::json::parse(data);
	}

	void add(const blacklist_data &data) {
		m_data["ips"].emplace_back(data.ip);
		m_data["hwids"].emplace_back(data.hwid);

		save();
	}

	void save() {
		std::ofstream o(m_name, std::ios::trunc);
		o << std::setw(4) << m_data;
		o.close();
	}

	bool find(const std::string &key) {
		for(auto &item : m_data["ips"]) {
			if(item.get<std::string>() == key) {
				return true;
			}
		}
		
		for(auto &item : m_data["hwids"]) {
			if(item.get<std::string>() == key) {
				return true;
			}
		}
		return false;
	}
};

};