From 43176d274b03b3950ac058be35b35ecbaee0c35c Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 7 Jan 2023 16:39:31 +0100 Subject: [PATCH] brigadier: wip signed arguments --- .../minosoft/commands/nodes/ArgumentNode.kt | 11 +++++++---- .../minosoft/commands/nodes/LiteralNode.kt | 6 +++--- .../minosoft/commands/nodes/SignedNode.kt | 18 ++++++++++++++++++ .../minosoft/commands/parser/SignedParser.kt | 16 ++++++++++++++++ .../parser/factory/ArgumentParserFactories.kt | 4 ++-- .../minosoft/commands/stack/CommandStack.kt | 16 +++++++++++----- .../commands/stack/CommandStackEntry.kt | 3 ++- .../chat/signature/signer/MessageSigner.kt | 2 +- .../chat/signature/signer/MessageSigner3.kt | 1 - 9 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/commands/nodes/SignedNode.kt create mode 100644 src/main/java/de/bixilon/minosoft/commands/parser/SignedParser.kt diff --git a/src/main/java/de/bixilon/minosoft/commands/nodes/ArgumentNode.kt b/src/main/java/de/bixilon/minosoft/commands/nodes/ArgumentNode.kt index 53a6f66a5..54d58fa94 100644 --- a/src/main/java/de/bixilon/minosoft/commands/nodes/ArgumentNode.kt +++ b/src/main/java/de/bixilon/minosoft/commands/nodes/ArgumentNode.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -15,13 +15,16 @@ package de.bixilon.minosoft.commands.nodes import de.bixilon.minosoft.commands.nodes.builder.CommandNodeBuilder import de.bixilon.minosoft.commands.parser.ArgumentParser +import de.bixilon.minosoft.commands.parser.SignedParser import de.bixilon.minosoft.commands.stack.CommandExecutor import de.bixilon.minosoft.commands.stack.CommandStack import de.bixilon.minosoft.commands.suggestion.types.SuggestionType import de.bixilon.minosoft.commands.util.CommandReader -class ArgumentNode : ExecutableNode { +class ArgumentNode : ExecutableNode, SignedNode { private val parser: ArgumentParser<*> + override val sign: Boolean + get() = parser is SignedParser constructor( name: String, @@ -49,7 +52,7 @@ class ArgumentNode : ExecutableNode { override fun execute(reader: CommandReader, stack: CommandStack) { reader.skipWhitespaces(1) val parsed = parser.parse(reader) - stack.push(name, parsed) + stack.push(this, parsed) super.execute(reader, stack) } @@ -61,7 +64,7 @@ class ArgumentNode : ExecutableNode { // try parsing our argument // it should succeed if we are not the last val parsed = parser.parse(reader) - stack.push(name, parsed) + stack.push(this, parsed) return super.getSuggestions(reader, stack) } catch (error: Throwable) { if (stack.size > stackSize + 1) { diff --git a/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt b/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt index 8dc65b46c..1ac695b91 100644 --- a/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt +++ b/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -48,7 +48,7 @@ class LiteralNode : ExecutableNode { if (literalName != name && literalName !in aliases) { throw InvalidLiteralArgumentError(reader, literalName) } - stack.push(name, name) + stack.push(this, name) return name } @@ -62,7 +62,7 @@ class LiteralNode : ExecutableNode { val literalName = reader.readUnquotedString() if (literalName == name || literalName in aliases) { - stack.push(name, name) + stack.push(this, name) return super.getSuggestions(reader, stack) } return suggester.suggest(literalName) ?: throw InvalidLiteralArgumentError(reader, literalName ?: "") diff --git a/src/main/java/de/bixilon/minosoft/commands/nodes/SignedNode.kt b/src/main/java/de/bixilon/minosoft/commands/nodes/SignedNode.kt new file mode 100644 index 000000000..08611ec78 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/nodes/SignedNode.kt @@ -0,0 +1,18 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.nodes + +interface SignedNode { + val sign: Boolean +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/SignedParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/SignedParser.kt new file mode 100644 index 000000000..132eb4737 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/SignedParser.kt @@ -0,0 +1,16 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.parser + +interface SignedParser diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt b/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt index c4bb9a0fc..83e9e3776 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -85,7 +85,7 @@ object ArgumentParserFactories : DefaultFactory>( minecraft:block_predicate minecraft:item_stack minecraft:item_predicate - minecraft:message + minecraft:message, SIGNED minecraft:nbt minecraft:nbt_path minecraft:objective diff --git a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt index 14aa2cbff..71f2330f9 100644 --- a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt +++ b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt @@ -14,6 +14,8 @@ package de.bixilon.minosoft.commands.stack import de.bixilon.kutil.cast.CastUtil.nullCast +import de.bixilon.minosoft.commands.nodes.NamedNode +import de.bixilon.minosoft.commands.nodes.SignedNode import de.bixilon.minosoft.commands.stack.print.PrintTarget import de.bixilon.minosoft.commands.stack.print.SystemPrintTarget import de.bixilon.minosoft.data.chat.signature.signer.MessageSigner @@ -43,7 +45,7 @@ class CommandStack( } fun getAny(name: String): Any? { - return stack.find { it.name == name }?.data + return stack.find { it.node.name == name }?.data } fun reset(size: Int) { @@ -51,14 +53,18 @@ class CommandStack( stack.removeAll { index++ >= size } } - fun push(name: String, data: Any?) { - stack.add(CommandStackEntry(name, data)) + fun push(node: NamedNode, data: Any?) { + stack.add(CommandStackEntry(node, data)) } - fun sign(chain: MessageSigner, key: PrivateKey, salt: Long, time: Instant): Map { + fun sign(signer: MessageSigner, key: PrivateKey, salt: Long, time: Instant): Map { val output: MutableMap = mutableMapOf() for (entry in stack) { - output[entry.name] = entry.sign(connection, chain, key, salt, time) + if (entry.node !is SignedNode || !entry.node.sign) { + continue + } + // TODO: properly sign + output[entry.node.name] = entry.sign(connection, signer, key, salt, time) } return output } diff --git a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt index df6c42c6a..ff4602ec4 100644 --- a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt +++ b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.commands.stack +import de.bixilon.minosoft.commands.nodes.NamedNode import de.bixilon.minosoft.data.chat.signature.LastSeenMessageList import de.bixilon.minosoft.data.chat.signature.signer.MessageSigner import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection @@ -20,7 +21,7 @@ import java.security.PrivateKey import java.time.Instant data class CommandStackEntry( - val name: String, + val node: NamedNode, val data: Any?, ) { diff --git a/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner.kt b/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner.kt index f834fd598..5fb336499 100644 --- a/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner.kt +++ b/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner.kt @@ -36,7 +36,7 @@ interface MessageSigner { if (version < ProtocolVersions.V_22W42A) { return MessageSigner2(version) } - return MessageSigner3(version, connection) + return MessageSigner3(version, connection.sessionId) } } } diff --git a/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner3.kt b/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner3.kt index 74bdbac2e..fb5144c4b 100644 --- a/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner3.kt +++ b/src/main/java/de/bixilon/minosoft/data/chat/signature/signer/MessageSigner3.kt @@ -62,7 +62,6 @@ class MessageSigner3( signature.update(lastSeenMessage.signature) } - return signature.sign() } }