aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/Console/commands/pardon-ip/CliCommandPardonIp.cpp
blob: 3517dbd87d372efeac0b66d17c5c2ebdec60c5d5 (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
#include "stdafx.h"

#include "CliCommandPardonIp.h"

#include "..\..\ServerCliEngine.h"
#include "..\..\ServerCliParser.h"
#include "..\..\..\Access\Access.h"
#include "..\..\..\Common\NetworkUtils.h"
#include "..\..\..\Common\StringUtils.h"

namespace ServerRuntime
{
	const char *CliCommandPardonIp::Name() const
	{
		return "pardon-ip";
	}

	const char *CliCommandPardonIp::Usage() const
	{
		return "pardon-ip <address>";
	}

	const char *CliCommandPardonIp::Description() const
	{
		return "Remove an IP ban.";
	}

	/**
	 * Validates the literal IP argument and removes the matching Access IP ban entry
	 * リテラルIPを検証して一致するIP BANを解除する
	 */
	bool CliCommandPardonIp::Execute(const ServerCliParsedLine &line, ServerCliEngine *engine)
	{
		if (line.tokens.size() != 2)
		{
			engine->LogWarn("Usage: pardon-ip <address>");
			return false;
		}
		if (!ServerRuntime::Access::IsInitialized())
		{
			engine->LogWarn("Access manager is not initialized.");
			return false;
		}

		// Java Edition pardon-ip only operates on a literal address, so do not resolve player names here.
		const std::string ip = StringUtils::TrimAscii(line.tokens[1]);
		if (!NetworkUtils::IsIpLiteral(ip))
		{
			engine->LogWarn("Invalid IP address: " + line.tokens[1]);
			return false;
		}
		// Distinguish invalid input from a valid but currently unbanned address for clearer operator feedback.
		if (!ServerRuntime::Access::IsIpBanned(ip))
		{
			engine->LogWarn("That IP address is not banned.");
			return false;
		}
		if (!ServerRuntime::Access::RemoveIpBan(ip))
		{
			engine->LogError("Failed to remove IP ban.");
			return false;
		}

		engine->LogInfo("Unbanned IP address " + ip + ".");
		return true;
	}

	/**
	 * Suggests currently banned IP addresses for the Java Edition literal-IP argument
	 * BAN済みIPの補完候補を返す
	 */
	void CliCommandPardonIp::Complete(const ServerCliCompletionContext &context, const ServerCliEngine *engine, std::vector<std::string> *out) const
	{
		(void)engine;
		// Complete from the persisted IP-ban snapshot because this command only accepts already-banned literals.
		if (context.currentTokenIndex != 1 || out == nullptr)
		{
			return;
		}

		std::vector<ServerRuntime::Access::BannedIpEntry> entries;
		if (!ServerRuntime::Access::SnapshotBannedIps(&entries))
		{
			return;
		}

		// Reuse the normalized prefix match used by other commands so completion stays case-insensitive.
		for (const auto &entry : entries)
		{
			const std::string &candidate = entry.ip;
			if (StringUtils::StartsWithIgnoreCase(candidate, context.prefix))
			{
				out->push_back(context.linePrefix + candidate);
			}
		}
	}
}