From 1968beaa0d9555c252ff7dbc4350aded1b588944 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Wed, 30 Mar 2022 14:39:52 -0400 Subject: [PATCH] app: add a welcome handler Signed-off-by: Eric Callahan --- moonraker/app.py | 85 +++++++++++++++- moonraker/assets/welcome.html | 180 ++++++++++++++++++++++++++++++++++ 2 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 moonraker/assets/welcome.html diff --git a/moonraker/app.py b/moonraker/app.py index 94200e0..467d847 100644 --- a/moonraker/app.py +++ b/moonraker/app.py @@ -11,6 +11,7 @@ import logging import json import traceback import ssl +import pathlib import urllib.parse import tornado import tornado.iostream @@ -205,8 +206,10 @@ class MoonrakerApp: self.mutable_router = MutableRouter(self) app_handlers: List[Any] = [ (AnyMatches(), self.mutable_router), + (r"/", WelcomeHandler), (r"/websocket", WebSocket), - (r"/server/redirect", RedirectHandler)] + (r"/server/redirect", RedirectHandler) + ] self.app = tornado.web.Application(app_handlers, **app_args) self.get_handler_delegate = self.app.get_handler_delegate @@ -932,3 +935,83 @@ class RedirectHandler(AuthorizedRequestHandler): raise tornado.web.HTTPError( 400, f"Unauthorized URL redirect: {url}") self.redirect(url) + +class WelcomeHandler(tornado.web.RequestHandler): + def initialize(self) -> None: + self.server: Server = self.settings['server'] + + def get(self) -> None: + summary: List[str] = [] + auth: AuthComp = self.server.lookup_component("authorization", None) + if auth is not None: + try: + user = auth.check_authorized(self.request) + except tornado.web.HTTPError: + authorized = False + else: + authorized = True + if authorized: + summary.append( + "Your device is authorized to access Moonraker's API." + ) + else: + summary.append( + "Your device is not authorized to access Moonraker's API. " + "This is normal if you intend to use API Key " + "authentication or log in as an authenticated user. " + "Otherwise you need to add your IP address to the " + "'trusted_clients' option in the [authorization] section " + "of moonraker.conf." + ) + cors_enabled = auth.cors_enabled() + if cors_enabled: + summary.append( + "CORS is enabled. Cross origin requests will be allowed " + "for origins that match one of the patterns specified in " + "the 'cors_domain' option of the [authorization] section." + ) + else: + summary.append( + "All cross origin requests will be blocked by the browser. " + "The 'cors_domains' option in [authorization] must be " + "configured to enable CORS." + ) + else: + authorized = True + cors_enabled = False + summary.append( + "The [authorization] component is not enabled in " + "moonraker.conf. All connections will be considered trusted." + ) + summary.append( + "All cross origin requests will be blocked by the browser. " + "The [authorization] section in moonraker.conf must be " + "configured to enable CORS." + ) + kstate = self.server.get_klippy_state() + if kstate != "disconnected": + kinfo = self.server.get_klippy_info() + kmsg = kinfo.get("state_message", kstate) + summary.append(f"Klipper reports {kmsg.lower()}") + else: + summary.append( + "Moonraker is not currently connected to Klipper. Make sure " + "that the klipper service has successfully started and that " + "its unix is enabled." + ) + wsm: WebsocketManager = self.server.lookup_component("websockets") + context: Dict[str, Any] = { + "ip_address": self.request.remote_ip, + "authorized": authorized, + "cors_enabled": cors_enabled, + "version": self.server.get_app_args()["software_version"], + "ws_count": wsm.get_count(), + "klippy_state": kstate, + "warnings": self.server.get_warnings(), + "summary": summary + } + self.render("welcome.html", **context) + + def get_template_path(self) -> Optional[str]: + tpath = pathlib.Path(__file__).parent.joinpath("assets") + return str(tpath) diff --git a/moonraker/assets/welcome.html b/moonraker/assets/welcome.html new file mode 100644 index 0000000..c0d511d --- /dev/null +++ b/moonraker/assets/welcome.html @@ -0,0 +1,180 @@ + + + Moonraker {{ version }} + + + +
+ +
+

Welcome to Moonraker

+

You may have intended + to navigate to one of Moonraker's front ends, if so check + that you entered the correct port in the address bar. +

+
+
+
+

Authorization

+
+
+ Request IP: +
{{ ip_address }}
+
+
+ Trusted: +
{{ authorized}}
+
+
+ CORS Enabled: +
{{ cors_enabled }}
+
+
+
+
+

Status

+
+
+ Version: +
{{ version }}
+
+
+ Websocket Count: +
{{ ws_count }}
+
+
+ Klipper State: +
{{ klippy_state }}
+
+
+
+ {% if summary %} +
+

Summary

+
+
    + {% for item in summary %} +
  • {{ item }}
  • + {% end %} +
+
+
+ {% end %} + {% if warnings %} +
+

Warnings

+
+
    + {% for warn in warnings %} +
  • {{ warn }}
  • + {% end %} +
+
+
+ {% end %} +
+
+ +