diff options
author | RaindropsSys <raindrops@equestria.dev> | 2024-03-29 22:05:24 +0100 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2024-03-29 22:05:24 +0100 |
commit | a79cf1bd000da84111ac839cfada1f8d867211fd (patch) | |
tree | a9fb8ae189c0e7176a32ac0078f1fce845d231a2 /server | |
parent | 231489c7f2c2b2b93eb23db1b28eaefca4d219ec (diff) | |
download | chatroom-a79cf1bd000da84111ac839cfada1f8d867211fd.tar.gz chatroom-a79cf1bd000da84111ac839cfada1f8d867211fd.tar.bz2 chatroom-a79cf1bd000da84111ac839cfada1f8d867211fd.zip |
Updated 7 files and added 4 files (automated)
Diffstat (limited to 'server')
-rw-r--r-- | server/platforms.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/server/platforms.js b/server/platforms.js new file mode 100644 index 0000000..01773ed --- /dev/null +++ b/server/platforms.js @@ -0,0 +1,75 @@ +const secrets = require('./secrets.json'); +const WebSocketServer = require('ws').WebSocketServer; +const wss = new WebSocketServer({ port: 19219 }); + +async function processRequest(ws, data) { + if (data.method === "wolfram") { + let query = "https://api.wolframalpha.com/v1/simple?appid=" + secrets.wolfram + "&i=" + encodeURIComponent(data.query ?? ""); + + ws.send({ + state: "REQUEST_RESPONSE", + data: { + _id: data._id ?? null, + data: Buffer.from(await (await fetch(query)).arrayBuffer()).toString("base64") + } + }); + + return; + } + + ws.send({ + state: "REQUEST_RESPONSE", + data: null + }); +} + +wss.on('connection', function connection(ws) { + ws.authenticated = false; + ws.token = null; + ws.auth = null; + + ws._send = ws.send; + ws.send = (d) => { + return ws._send(JSON.stringify(d)); + } + + ws.on('error', console.error); + + ws.on('message', async (_data) => { + try { + let data = JSON.parse(_data); + + if (data.state) { + switch (data.state) { + case "AUTHENTICATION_RESPONSE": + if (data.token) { + let userInfo = (await (await fetch("https://school.equestria.dev/_matrix/client/v3/account/whoami", { headers: { 'Authorization': 'Bearer ' + data.token } })).json()); + console.log(userInfo); + + if (userInfo['user_id']) { + ws.auth = userInfo; + ws.token = data.token; + ws.authenticated = true; + + ws.send({ + state: "AUTHENTICATION_OK" + }); + } else { + ws.close(); + } + } + break; + } + } + + if (!ws.authenticated) ws.close(); + if (!data.state || data.state !== "AUTHENTICATION_RESPONSE") processRequest(ws, data); + } catch (e) { + console.error(e); + } + }); + + ws.send({ + state: "NEEDS_AUTHENTICATION" + }); +}); |