diff --git a/assets/ws_debug.js b/assets/ws_debug.js new file mode 100644 index 0000000..b001790 --- /dev/null +++ b/assets/ws_debug.js @@ -0,0 +1,68 @@ +let ws; + +const JS_MESSAGE = "JS code"; +const IN_MESSAGE = "Incoming"; + +/** + * Log message + */ +function log(src, txt) { + const target = document.getElementById("ws_log"); + const msg = document.createElement("div"); + msg.className = "message"; + msg.innerHTML = `
${src}
${txt}
` + target.insertBefore(msg, target.firstChild); +} + +/** + * Set the state of the WebSocket + */ +function setState(state) { + document.getElementById("state").innerText = state; +} + +/** + * Initialize WebSocket connection + */ +function connect() { + disconnect(); + log(JS_MESSAGE, "Initialize connection..."); + ws = new WebSocket("/api/ws"); + setState("Connecting..."); + ws.onopen = function () { + log(JS_MESSAGE, "Connected to WebSocket !"); + setState("Connected"); + } + ws.onmessage = function (event) { + log(IN_MESSAGE, event.data); + } + ws.onclose = function () { + log(JS_MESSAGE, "Disconnected from WebSocket !"); + setState("Disconnected"); + } + ws.onerror = function (event) { + console.error("WS Error!", event); + log(JS_MESSAGE, `Error with websocket! ${event}`); + setState("Error"); + } +} + +/** + * Close WebSocket connection + */ +function disconnect() { + if (ws && ws.readyState === WebSocket.OPEN) { + log(JS_MESSAGE, "Close connection..."); + ws.close(); + } + + setState("Disconnected"); + ws = undefined; +} + +/** + * Clear WS logs + */ +function clearLogs() { + document.getElementById("ws_log").innerHTML = ""; +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 89183c5..668e220 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ async fn main() -> std::io::Result<()> { .route("/", web::post().to(web_ui::home)) .route("/oidc_cb", web::get().to(web_ui::oidc_cb)) .route("/sign_out", web::get().to(web_ui::sign_out)) + .route("/ws_debug", web::get().to(web_ui::ws_debug)) // API routes .route("/api", web::get().to(api::api_home)) .route("/api", web::post().to(api::api_home)) diff --git a/src/server/web_ui.rs b/src/server/web_ui.rs index adc0462..cb6bbf3 100644 --- a/src/server/web_ui.rs +++ b/src/server/web_ui.rs @@ -223,3 +223,22 @@ pub async fn sign_out(session: Session) -> HttpResult { .insert_header(("location", "/")) .finish()) } + +#[derive(askama::Template)] +#[template(path = "ws_debug.html")] +struct WsDebugTemplate { + name: String, +} + +/// WebSocket debug +pub async fn ws_debug(session: Session) -> HttpResult { + let Some(user): Option = session.get(USER_SESSION_KEY)? else { + return Ok(HttpResponse::Found() + .insert_header(("location", "/")) + .finish()); + }; + + Ok(HttpResponse::Ok() + .content_type("text/html") + .body(WsDebugTemplate { name: user.name }.render().unwrap())) +} diff --git a/templates/base_page.html b/templates/base_page.html new file mode 100644 index 0000000..f86c767 --- /dev/null +++ b/templates/base_page.html @@ -0,0 +1,46 @@ + + + + + Matrix GW + + + + + + + + +
+ +
+ + +
+ {% block content %} + TO_REPLACE + {% endblock content %} +
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index d5bd290..f1360f1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,181 +1,146 @@ - - - - - Matrix GW - +{% extends "base_page.html" %} +{% block content %} + +{% if let Some(msg) = success_message %} +
+ {{ msg }} +
+{% endif %} - - - - - + +{% if let Some(msg) = error_message %} +
+ {{ msg }} +
+{% endif %} - -
- -
+ +
Current user ID: {{ user_id.0 }}
+ +
+
Registered clients
+
+ {% if clients.len() > 0 %} + + + + + + + + + + + + + + {% for client in clients %} + + + + + + + + + + {% endfor %} + +
IDDescriptionRead onlyNetworkCreatedUsed
{{ client.id.0 }}{{ client.description }} + {% if client.readonly_client %} + YES + {% else %} + NO + {% endif %} + + {% if let Some(net) = client.network %} + {{ net }} + {% else %} + Unrestricted + {% endif %} + {{ client.fmt_created() }}{{ client.fmt_used() }} + +
+ {% endif %} -
- - {% if let Some(msg) = success_message %} -
- {{ msg }} -
- {% endif %} - - - {% if let Some(msg) = error_message %} -
- {{ msg }} -
- {% endif %} - - -
Current user ID: {{ user_id.0 }}
- - -
-
Registered clients
-
- {% if clients.len() > 0 %} - - - - - - - - - - - - - - {% for client in clients %} - - - - - - - - - - {% endfor %} - -
IDDescriptionRead onlyNetworkCreatedUsed
{{ client.id.0 }}{{ client.description }} - {% if client.readonly_client %} - YES - {% else %} - NO - {% endif %} - - {% if let Some(net) = client.network %} - {{ net }} - {% else %} - Unrestricted - {% endif %} - {{ client.fmt_created() }}{{ client.fmt_used() }} - -
- {% endif %} - - {% if clients.len() == 0 %} -

No client registered yet!

- {% endif %} -
-
- - -
-
New client
-
-
-
- - - Client description helps with identification. -
-
- - - Restrict the networks this IP address can be used from. -
- -
- -
- - -
- -
- - - -
-
-
- - -
-
Matrix authentication token
-
-

To obtain a new Matrix authentication token:

-
    -
  1. Sign in to Element from a private browser window
  2. -
  3. Open All settings and access the Help & About tag
  4. -
  5. Expand Access Token and copy the value
  6. -
  7. Paste the copied value below
  8. -
  9. Close the private browser window without signing out!
  10. -
- -

You should not need to replace this value unless you explicitly signed out the associated browser - session.

- -

Tip: you can rename the session to easily identify it among all your other sessions!

- -
-
- - - Changing this value will reset all active - connections - to Matrix GW. -
- - -
-
+ {% if clients.len() == 0 %} +

No client registered yet!

+ {% endif %}
+ +
+
New client
+
+
+
+ + + Client description helps with identification. +
+
+ + + Restrict the networks this IP address can be used from. +
- - \ No newline at end of file +
+ +
+ + +
+ +
+ + + +
+
+
+ + +
+
Matrix authentication token
+
+

To obtain a new Matrix authentication token:

+
    +
  1. Sign in to Element from a private browser window
  2. +
  3. Open All settings and access the Help & About tag
  4. +
  5. Expand Access Token and copy the value
  6. +
  7. Paste the copied value below
  8. +
  9. Close the private browser window without signing out!
  10. +
+ +

You should not need to replace this value unless you explicitly signed out the associated browser + session.

+ +

Tip: you can rename the session to easily identify it among all your other sessions!

+ +
+
+ + + Changing this value will reset all active + connections + to Matrix GW. +
+ + +
+
+
+ + +{% endblock content %} \ No newline at end of file diff --git a/templates/ws_debug.html b/templates/ws_debug.html new file mode 100644 index 0000000..d3f9119 --- /dev/null +++ b/templates/ws_debug.html @@ -0,0 +1,46 @@ +{% extends "base_page.html" %} +{% block content %} + + + + +

WS Debug

+ +
+ + + + State: DISCONNECTED +
+ +
+
+
INFO
+
Welcome!
+
+
+ + +{% endblock content %} \ No newline at end of file