From 8696ee4c9e09bd4b3c4b7ecac232b0bfbc4a43c0 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 27 Jul 2020 05:04:49 +0200 Subject: [PATCH] WebHost: Use B64encoded UUIDs for links. Warning: This will break old links --- WebHostLib/__init__.py | 27 ++++++++++++++++++------ WebHostLib/templates/host_room.html | 13 +++++++----- WebHostLib/templates/macros.html | 2 +- WebHostLib/templates/uploads.html | 5 +++-- WebHostLib/templates/view_seed.html | 32 ++++++++++++++--------------- WebHostLib/tracker.py | 2 +- 6 files changed, 50 insertions(+), 31 deletions(-) diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index 7c9aacca..71a84f28 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -2,6 +2,8 @@ So unless you're Berserker you need to include license information.""" import os +import uuid +import base64 from pony.flask import Pony from flask import Flask, request, redirect, url_for, render_template, Response, session, abort @@ -46,7 +48,21 @@ av = Autoversion(app) cache = Cache(app) Compress(app) -# this local cache is risky business if app hosting is done with subprocesses as it will not sync. Waitress is fine though +from werkzeug.routing import BaseConverter + + +class B64UUIDConverter(BaseConverter): + + def to_python(self, value): + return uuid.UUID(bytes=base64.urlsafe_b64decode(value + '==')) + + def to_url(self, value): + return base64.urlsafe_b64encode(value.bytes).rstrip(b'=').decode('ascii') + + +# short UUID +app.url_map.converters["suuid"] = B64UUIDConverter +app.jinja_env.filters['suuid'] = lambda value: base64.urlsafe_b64encode(value.bytes).rstrip(b'=').decode('ascii') @app.before_request @@ -56,7 +72,7 @@ def register_session(): session["_id"] = uuid4() # uniquely identify each session without needing a login -@app.route('/seed/') +@app.route('/seed/') def view_seed(seed: UUID): seed = Seed.get(id=seed) if not seed: @@ -65,7 +81,7 @@ def view_seed(seed: UUID): rooms=[room for room in seed.rooms if room.owner == session["_id"]]) -@app.route('/new_room/') +@app.route('/new_room/') def new_room(seed: UUID): seed = Seed.get(id=seed) if not seed: @@ -84,14 +100,13 @@ def _read_log(path: str): f"Likely a crash during spinup of multiworld instance or it is still spinning up." -@app.route('/log/') +@app.route('/log/') def display_log(room: UUID): # noinspection PyTypeChecker return Response(_read_log(os.path.join("logs", str(room) + ".txt")), mimetype="text/plain;charset=UTF-8") - -@app.route('/hosted/', methods=['GET', 'POST']) +@app.route('/hosted/', methods=['GET', 'POST']) def host_room(room: UUID): room = Room.get(id=room) if room is None: diff --git a/WebHostLib/templates/host_room.html b/WebHostLib/templates/host_room.html index aadab905..3150ce11 100644 --- a/WebHostLib/templates/host_room.html +++ b/WebHostLib/templates/host_room.html @@ -1,19 +1,22 @@ {% extends 'layout.html' %} {% block head %} - Multiworld {{ room.id }} - + Multiworld {{ room.id|suuid }} + {% endblock %} {% block body %}
{% if room.owner == session["_id"] %} - Room created from Seed #{{ room.seed.id }}
+ Room created from Seed #{{ room.seed.id|suuid }} +
{% endif %} {% if room.tracker %} - This room has a Multiworld Tracker enabled.
+ This room has a Multiworld Tracker enabled. +
{% endif %} - This room will be closed after {{ room.timeout//60//60 }} hours of inactivity. Should you wish to continue later, + This room will be closed after {{ room.timeout//60//60 }} hours of inactivity. Should you wish to continue + later, you can simply refresh this page and the server will be started again.
{% if room.owner == session["_id"] %}
diff --git a/WebHostLib/templates/macros.html b/WebHostLib/templates/macros.html index 47891a77..425428fc 100644 --- a/WebHostLib/templates/macros.html +++ b/WebHostLib/templates/macros.html @@ -1,7 +1,7 @@ {% macro list_rooms(rooms) -%} diff --git a/WebHostLib/templates/uploads.html b/WebHostLib/templates/uploads.html index 397dec16..40f4362f 100644 --- a/WebHostLib/templates/uploads.html +++ b/WebHostLib/templates/uploads.html @@ -36,8 +36,9 @@ {% for room in rooms %} - {{ room.seed.id }} - {{ room.id }} + {{ room.seed.id|suuid }} + + {{ room.id|suuid }} {{ room.seed.multidata.names[0]|length }} Total: {{ room.seed.multidata.names[0]|join(", ")|truncate(256, False, " ...") }} {{ room.creation_time.strftime("%Y-%m-%d %H:%M") }} diff --git a/WebHostLib/templates/view_seed.html b/WebHostLib/templates/view_seed.html index b9609b1e..b9846070 100644 --- a/WebHostLib/templates/view_seed.html +++ b/WebHostLib/templates/view_seed.html @@ -2,9 +2,9 @@ {% import "macros.html" as macros %} {% block head %} - Multiworld Seed {{ seed.id }} - - + Multiworld Seed {{ seed.id|suuid }} + + {% endblock %} {% block body %} @@ -13,19 +13,19 @@

Seed Info

- - - - - - - - - - - + + + + + + + + + +
Seed: {{ seed.id }}
Created: 
Players:  -
    - {% for team in seed.multidata["names"] %} +
Seed: {{ seed.id|suuid }}
Created: 
Players:  +
    + {% for team in seed.multidata["names"] %}
  • Team #{{ loop.index }} - {{ team | length }}
      {% for player in team %} diff --git a/WebHostLib/tracker.py b/WebHostLib/tracker.py index 8fcffa44..cdbea5f0 100644 --- a/WebHostLib/tracker.py +++ b/WebHostLib/tracker.py @@ -248,7 +248,7 @@ def get_static_room_data(room: Room): return result -@app.route('/tracker/') +@app.route('/tracker/') @cache.memoize(timeout=30) # update every 30 seconds def get_tracker(tracker: UUID): room = Room.get(tracker=tracker)