commit f6de717fec2b19ca0561689d28b8933542156e2b Author: Daniel Flanagan Date: Thu Oct 29 15:22:08 2020 -0500 Initial commit diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..37b0698 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +export BIND_HOST=0.0.0.0 +export BIND_PORT=25566 +export MINECRAFT_RCON_HOST=localhost +# export MINECRAFT_RCON_PORT=255 +export MINECRAFT_RCON_PASSWORD=secret diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb67639 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.env diff --git a/makefile b/makefile new file mode 100644 index 0000000..ab7a2e1 --- /dev/null +++ b/makefile @@ -0,0 +1,8 @@ +.PHONY: run +run: .env ; @./run.sh +.PHONY: init +init: + pip install -r requirements.txt + @cp .env.example .env + ${EDITOR} .env + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2bcdac7 --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +# TPUI + +TPUI is a very small app that effectively gives Minecraft players the ability to +teleport in your server without needing mods or `/op`. It is currently only +suitable for semi-trustworthy situations, since any player can teleport any +other player, but is nice when you want to ensure nobody can perform other +cheats. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5d95dfd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +mcrcon diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..b6847fc --- /dev/null +++ b/run.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh + +source "./.env" +env FLASK_APP=server.py python -m flask run --host="$BIND_HOST" --port="$BIND_PORT" diff --git a/server.py b/server.py new file mode 100644 index 0000000..d9ba77d --- /dev/null +++ b/server.py @@ -0,0 +1,50 @@ +from mcrcon import MCRcon +from flask import Flask, render_template, redirect, request +import json +import os + +app = Flask(__name__) + +# load from env vars +host = os.environ['RCON_HOST'] +password = os.environ['RCON_PASSWORD'] +port = int(os.environ.setdefault('RCON_PORT', '25575')) +print(f'Connecting to Minecraft Server: {host}:{port} with {password}') + +@app.route('/') +def index(): + return render_template('index.html', players=current_players_list()) + +@app.route('/tp', methods=['POST']) +def tp(): + # TODO: sanitize inputs to avoid possible command injection + src = request.form['src'] + dest = request.form['dest'] + + # this echoes a string that contains the src player's coordinates + src_coords = rcon_command(f"/tp {src} ~ ~ ~") + + # teleport the player + rcon_command(f"/tp {src} {dest}") + + # have the server report the old coordinates in case they need teleporting + # back + rcon_command(f"/say TP: {src_coords.replace(f'Teleported {src} to', f'{src} just teleported from', 1)} to {dest}".replace("[Rcon] ", "")) + + return redirect('/') + +def rcon_command(cmd): + print(f"RCON: Sending \"{cmd}\"...") + response = "" + with MCRcon(host, password, port) as mcr: + response = mcr.command(cmd) + print(f"RCON: \"{cmd}\" Response:\n\t{response}") + return response + +def current_players_list(): + # TODO: this breaks if a player has a comma in their name + # is that even possible? + plist = rcon_command(f"/list") + print(f'PLIST: {plist}') + print(f'PLIST: {plist.split(":")[1].split(", ")}') + return plist.split(":")[1].split(", ") diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..568573b --- /dev/null +++ b/templates/index.html @@ -0,0 +1,38 @@ +Super Simple Minecraft RCON Teleporter + +
+ Teleport + + to + + +
+ +