From 03a4f70c0048f435b65c8a84f37f8528adfa22fc Mon Sep 17 00:00:00 2001 From: GitGinocchio Date: Sun, 23 Feb 2025 01:01:14 +0100 Subject: [PATCH] ws and wss fix --- src/ext/syncify.js | 6 +++--- src/web/durables.ts | 4 ++-- src/web/routes/403/route.ts | 2 +- src/web/routes/404/route.ts | 5 +++-- src/web/routes/all/route.ts | 2 +- src/web/routes/bugreport/route.ts | 12 +++++++----- src/web/routes/index/route.ts | 5 +++-- src/web/routes/logout/route.ts | 4 ++-- src/web/routes/onboard/route.ts | 2 +- src/web/routes/room/room.js | 2 +- src/web/routes/room/route.ts | 2 +- 11 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/ext/syncify.js b/src/ext/syncify.js index 1f51b03..10fe008 100644 --- a/src/ext/syncify.js +++ b/src/ext/syncify.js @@ -1,7 +1,7 @@ const reconnectionAttempts = 3; const Addresses = [ - `http://127.0.0.1:8787`, - `https://syncify.ginocchio.workers.dev/` + `https://syncify.ginocchio.workers.dev`, + `http://127.0.0.1:8787` ]; let customButton; @@ -81,7 +81,7 @@ function showDialog(title, message) { async function attemptConnection(url, user_data) { return new Promise((resolve, reject) => { - socket = new WebSocket(`${url.replace("https", "ws").replace("http", "ws")}/auth/${user_data.id}`); + socket = new WebSocket(`${url.replace("https", "wss").replace("http", "ws")}/auth/${user_data.id}`); socket.addEventListener("open", (event) => { const data = JSON.stringify({ diff --git a/src/web/durables.ts b/src/web/durables.ts index e546aac..519b9d9 100644 --- a/src/web/durables.ts +++ b/src/web/durables.ts @@ -129,7 +129,7 @@ export class Room extends DurableObject { if (!this.awakaned) { await this.awake(); } return { - id : this.id, + id : this.id.toString(), name : this.name, max_members : this.max_members, editable_queue : this.editable_queue, @@ -400,7 +400,7 @@ export class User extends DurableObject { if (!this.awakaned) { await this.awake(); } return { - id : this.id, + id : this.id.toString(), spotifyid : this.spotifyid, display_name : this.display_name, birthdate : this.birthdate, diff --git a/src/web/routes/403/route.ts b/src/web/routes/403/route.ts index f1759fd..c28c803 100644 --- a/src/web/routes/403/route.ts +++ b/src/web/routes/403/route.ts @@ -6,7 +6,7 @@ import Auth from '../../auth.js'; import Utils from '../../utils.js'; export default { - async get (request, env, ctx) { + async get (request : Request, env, ctx) { const url = new URL(request.url); const cookies = Utils.parseCookies(request.headers.get('cookie')); diff --git a/src/web/routes/404/route.ts b/src/web/routes/404/route.ts index e5e1c6b..0b207d1 100644 --- a/src/web/routes/404/route.ts +++ b/src/web/routes/404/route.ts @@ -2,6 +2,7 @@ import Page404 from './404.html'; import mustache from 'mustache'; +import { User } from '../../durables.js'; import Auth from '../../auth.js'; import Utils from '../../utils.js'; @@ -13,10 +14,10 @@ export default { const token = cookies.get('user_access_token'); const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY); - let data = null; + let data : any = null; if (payload != null){ const id = env.users.idFromString(payload.id); - const user = env.users.get(id); + const user : User = env.users.get(id); data = await user.getData(); } diff --git a/src/web/routes/all/route.ts b/src/web/routes/all/route.ts index 7fae0bc..d8ea110 100644 --- a/src/web/routes/all/route.ts +++ b/src/web/routes/all/route.ts @@ -1,7 +1,7 @@ export default { - async get (request, env, ctx) { + async get (request : Request, env, ctx) { const url = new URL(request.url); url.pathname = '/404' return Response.redirect(url); diff --git a/src/web/routes/bugreport/route.ts b/src/web/routes/bugreport/route.ts index d91292f..7aa109e 100644 --- a/src/web/routes/bugreport/route.ts +++ b/src/web/routes/bugreport/route.ts @@ -1,26 +1,28 @@ +// @ts-ignore import BugReport from './bugreport.html' import mustache from 'mustache'; +import { User } from '../../durables.js'; import Auth from '../../auth.js' import Utils from '../../utils.js'; export default { - async post (request, env, ctx) { + async post (request : Request, env, ctx) { return new Response("Not implemented"); }, - async get (request, env, ctx) { + async get (request : Request, env, ctx) { const cookies = Utils.parseCookies(request.headers.get('cookie')); const url = new URL(request.url); const token = cookies.get('user_access_token'); const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY); - let data = null; + let data : any = null; if (payload != null){ const id = env.users.idFromString(payload.id); - const user = env.users.get(id); - data = await user.getUserData(); + const user : User = env.users.get(id); + data = await user.getData(); } const html = mustache.render(BugReport, { diff --git a/src/web/routes/index/route.ts b/src/web/routes/index/route.ts index ea9261c..f248994 100644 --- a/src/web/routes/index/route.ts +++ b/src/web/routes/index/route.ts @@ -12,8 +12,9 @@ export default { const token = cookies.get('user_access_token'); const payload = await Auth.verifyToken(token, env.JWT_SECRET_KEY); - if (payload) { - return Response.redirect(`${url.protocol}${url.hostname}:${url.port}/user`); + if (payload) { + url.pathname = '/user' + return Response.redirect(url); } return new Response(Index, { headers: { 'Content-Type': 'text/html' }}); diff --git a/src/web/routes/logout/route.ts b/src/web/routes/logout/route.ts index 95c84e7..ddabcec 100644 --- a/src/web/routes/logout/route.ts +++ b/src/web/routes/logout/route.ts @@ -1,10 +1,10 @@ export default { - async get (request, env, ctx) { + async get (request : Request, env, ctx) { const url = new URL(request.url); return new Response(null, { headers: { - 'Set-Cookie': `user_access_token=; Max-Age=-1; room_access_token=; Max-Age=-1;`, + 'Set-Cookie': `user_access_token=; Path=/; Max-Age=0; Secure; HttpOnly; room_access_token=; Path=/; Max-Age=0; Secure; HttpOnly;`, Location: `/` }, status: 302 diff --git a/src/web/routes/onboard/route.ts b/src/web/routes/onboard/route.ts index e8f3b01..407b6bd 100644 --- a/src/web/routes/onboard/route.ts +++ b/src/web/routes/onboard/route.ts @@ -2,7 +2,7 @@ import OnBoard from './onboard.html' import mustache from 'mustache'; export default { - async get (request, env, ctx) { + async get (request : Request, env, ctx) { const url = new URL(request.url); /* diff --git a/src/web/routes/room/room.js b/src/web/routes/room/room.js index 06c9083..183506e 100644 --- a/src/web/routes/room/room.js +++ b/src/web/routes/room/room.js @@ -15,7 +15,7 @@ function copyurl(roomid) { } document.addEventListener("DOMContentLoaded", (event) => { - const socket = new WebSocket(`ws://${window.location.host}/room`); + const socket = new WebSocket((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/room'); const messageInput = document.getElementById("message-input"); const queueInput = document.getElementById("queue-input"); diff --git a/src/web/routes/room/route.ts b/src/web/routes/room/route.ts index 3c8952f..df43ab7 100644 --- a/src/web/routes/room/route.ts +++ b/src/web/routes/room/route.ts @@ -94,7 +94,7 @@ export default { url: user_data.external_urls.spotify }, room : { - id : room_id, + id : room_id.toString(), chat : room_data.messages, members : room_data.members.values(), artists : room_data.artists,