CommandRelay Plugin
Version: 1.0
Author: vcesar
Description:
CommandRelayPlugin allows external TCP requests to execute commands on a Minecraft server, using a secret token and a whitelist of allowed IPs for enhanced security.
Main Features:
Built-in TCP server to receive commands.
Secret token validation for each command.
IP whitelist to control access.
Commands executed on Bukkit’s main thread.
Error logging to errors.log.
Reloadable configuration at runtime (reloadPluginConfig).
Debug mode for detailed command tracking.
Thread pool to handle multiple concurrent clients.
Security:
1. Only allowed IPs can connect.
2. Only commands listed in allowed_commands are executed.
3. Secret token required for every request.
Typical Configuration (config.yml):
port: 8193
secret_key: "my_secret_token"
allowed_ips:
- "127.0.0.1"
debug: false
allowed_commands:
- say
- give
messages:
activated: "Plugin activated"
invalid_token: "ERROR: invalid token"
command_executed: "Command executed successfully"
Plugin Command Usage:
/cr <reload|info|tcpstatus>
Permissions:
commandrelay.admin (default op)
EXAMPLE OF CLIENT SENDER
Code (Text):
// SENDER MODULE
const net = require("net"); // Node.js module for TCP connections
// CONFIGURATION
const PLUGIN_HOST = "localhost"; // IP address of the server running the Java plugin
const PLUGIN_PORT = 25579; // Port defined in the plugin configuration
const TOKEN = "123"; // Secret token configured in config.yml
/**
* Sends a command to the CommandRelayPlugin via TCP.
* @param {string} command - The command string to be executed on the server.
* @returns {Promise<string>} - Resolves with the response from the plugin.
*/
function sendCommandToPlugin(command) {
return new Promise((resolve, reject) => {
// Create a TCP connection to the plugin server
const client = net.createConnection(
{ host: PLUGIN_HOST, port: PLUGIN_PORT },
() => {
console.log("✅ Connected to TCP plugin server");
// Send the token and command separated by ':', followed by a newline
client.write(`${TOKEN}:${command}\n`);
}
);
let dataBuffer = ""; // Buffer to collect response data
// Collect data sent back by the plugin
client.on("data", (data) => {
dataBuffer += data.toString();
});
// When the server ends the connection, resolve the promise with the response
client.on("end", () => {
console.log("⬅ Response from plugin:", dataBuffer.trim());
resolve(dataBuffer.trim());
});
// Handle any connection errors
client.on("error", (err) => {
console.error("❌ Error connecting to plugin:", err.message);
reject(err);
});
});
}
// EXAMPLE USAGE
(async () => {
try {
// Send a command to the plugin
const response = await sendCommandToPlugin("say Hello from Node.js");
console.log("Final response:", response);
} catch (err) {
console.error("Connection failed:", err);
}
})();