aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/Access/BanManager.h
blob: 59103bec7a27a976af294c96eabec465f93aa877 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include <string>
#include <vector>

namespace ServerRuntime
{
	namespace Access
	{
		/**
		 * Information shared with player bans and IP bans
		 * プレイヤーBANとIP BANで共有する情報
		 */
		struct BanMetadata
		{
			std::string created;
			std::string source;
			std::string expires;
			std::string reason;
		};

		struct BannedPlayerEntry
		{
			std::string xuid;
			std::string name;
			BanMetadata metadata;
		};

		struct BannedIpEntry
		{
			std::string ip;
			BanMetadata metadata;
		};

		/**
		 * Dedicated server BAN file manager.
		 *
		 * Files:
		 * - banned-players.json
		 * - banned-ips.json
		 *
		 * This class only handles storage/caching.
		 * Connection-time hooks are wired separately.
		 */
		class BanManager
		{
		public:
			/**
			 * **Create Ban Manager**
			 *
			 * Binds the manager to the directory that stores the dedicated-server access files
			 * Dedicated Server のアクセスファイル配置先を設定する
			 */
			explicit BanManager(const std::string &baseDirectory = ".");

			/**
			 * Creates empty JSON array files when the dedicated server starts without persisted access data
			 * BANファイルが無い初回起動時に空JSONを用意する
			 */
			bool EnsureBanFilesExist() const;
			bool Reload();
			bool Save() const;

			bool LoadPlayers(std::vector<BannedPlayerEntry> *outEntries) const;
			bool LoadIps(std::vector<BannedIpEntry> *outEntries) const;
			bool SavePlayers(const std::vector<BannedPlayerEntry> &entries) const;
			bool SaveIps(const std::vector<BannedIpEntry> &entries) const;

			const std::vector<BannedPlayerEntry> &GetBannedPlayers() const;
			const std::vector<BannedIpEntry> &GetBannedIps() const;
			/**
			 * Copies only currently active player BAN entries so expired metadata does not leak into command output
			 * 期限切れを除いた有効なプレイヤーBAN一覧を複製取得する
			 */
			bool SnapshotBannedPlayers(std::vector<BannedPlayerEntry> *outEntries) const;
			/**
			 * Copies only currently active IP BAN entries so expired metadata does not leak into command output
			 * 期限切れを除いた有効なIP BAN一覧を複製取得する
			 */
			bool SnapshotBannedIps(std::vector<BannedIpEntry> *outEntries) const;

			bool IsPlayerBannedByXuid(const std::string &xuid) const;
			bool IsIpBanned(const std::string &ip) const;

			bool AddPlayerBan(const BannedPlayerEntry &entry);
			bool AddIpBan(const BannedIpEntry &entry);
			bool RemovePlayerBanByXuid(const std::string &xuid);
			bool RemoveIpBan(const std::string &ip);

			std::string GetBannedPlayersFilePath() const;
			std::string GetBannedIpsFilePath() const;

			static BanMetadata BuildDefaultMetadata(const char *source = "Server");

		private:
			static void NormalizeMetadata(BanMetadata *metadata);

			std::string BuildPath(const char *fileName) const;

		private:
			std::string m_baseDirectory;
			std::vector<BannedPlayerEntry> m_bannedPlayers;
			std::vector<BannedIpEntry> m_bannedIps;
		};
	}
}