From 61467f263524ec1adaf073d77faabb8ec689fef6 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Wed, 7 Feb 2024 21:49:28 -0600 Subject: [PATCH] Lanyard Proxy --- basicDocs.json | 12 +++++++ index.js | 85 +++++++++++++++++++++++++++++++++++++++++------ package-lock.json | 23 ++++++++++++- package.json | 3 +- 4 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 basicDocs.json diff --git a/basicDocs.json b/basicDocs.json new file mode 100644 index 0000000..0a943f7 --- /dev/null +++ b/basicDocs.json @@ -0,0 +1,12 @@ +{ + "info": "Welcome to the API! It's still a work in process, so there's not much here yet. I don't really know how to do APIs yet so expect it to suck if I add anything.", + "documentation": "https://codeberg.org/Bingus_Violet/api.violets-purgatory.dev", + "current_commands": { + "/v1/pfp": { + "description": "Generates a random PFP. For use on the main page & with UserPFP on Discord." + }, + "/v1/lanyard": { + "description": "Fetches the most recently updated Lanyard update. Only gives data about *my* lanyard status. Don't worry about 'overusing' this, as it doesn't call a request to Lanyard or Discord everytime, it just gets the most recent data gathered by the web socket. Mainly designed to prevent my many sites from overloading the Lanyard socket, but also so that I can add extra features in the future if I so desire." + } + } +} \ No newline at end of file diff --git a/index.js b/index.js index bb40baa..83f1bc7 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,95 @@ const express = require('express'), -path = require("path"), -fs = require("fs") + path = require("path"), + fs = require("fs"), + WebSocket = require("ws") const PORT = process.env.PORT || 8080 var app = express() +var lanyardData = "Nothing Fetched yet!" + +var sockets = [] + function randomFileInDir(directory) { var dir = fs.readdirSync(directory) return path.join(directory, dir[Math.floor(Math.random() * dir.length)]) } app.get("/", (req, res) => { - res.send({ - info: "Welcome to the API! It's still a work in process, so there's not much here yet. I don't really know how to do APIs yet so expect it to suck if I add anything.", - documentation: "https://codeberg.org/Bingus_Violet/api.violets-purgatory.dev/wiki", - current_commands: { - "/v1/pfp": { - description: "Generates a random PFP. For use on the main page & with UserPFP on Discord." - } - } - }) + var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json")) + docs = JSON.parse(docs) + res.send(docs) }) app.get("/v1/pfp", (req, res) => { res.sendFile(randomFileInDir(path.join(__dirname, 'pfps'))) }) +app.get("/v1/lanyard", (req, res) => { + res.send(lanyardData) +}) + app.listen(PORT, () => { console.log("API is now listening on port 8080!") +}) + +var lanyard = new WebSocket('wss://api.lanyard.rest/socket') + +function beat(dur) { + lanyard.send(JSON.stringify({ + op: 3 + })) + setTimeout(() => { + beat(dur) + }, dur); +} + +lanyard.addEventListener("message", async (res) => { + var data = JSON.parse(res.data) + if (data.op == 1) { + beat(data.d.heartbeat_interval) + lanyard.send(JSON.stringify({ + op: 2, + d: { + subscribe_to_id: "534132311781015564" + } + })) + } else if (data.op == 0) { + lanyardData = data + + for (let index = 0; index < sockets.length; index++) { + var socketData = sockets[index] + var socket = socketData.socket; + socket.send(JSON.stringify(lanyardData)) + + if ((Date.now() - socketData.lastPing) > 120000) { + sockets.splice(index, 1) + } + } + } +}) + +wsServer = new WebSocket.WebSocketServer({ port: 8090 }) + +wsServer.on("connection", function connection(socket) { + socket.on('message', function message(data) { + data = JSON.parse(data) + console.log(data) + if (data.op == 3) { + for (let index = 0; index < sockets.length; index++) { + const socketData = sockets[index]; + if (socketData.socket == socket) { + sockets[index].lastPing = Date.now() + } + } + + socket.send(`{"op": 3}`) + } + }) + + socket.send(JSON.stringify(lanyardData)) + socket.send(`{ "op": 1 }`) + + sockets.push({socket, lastPing: Date.now()}) }) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2331f2c..0faf5a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "GPL-3.0-or-later", "dependencies": { - "express": "^4.18.2" + "express": "^4.18.2", + "ws": "^8.16.0" } }, "node_modules/accepts": { @@ -653,6 +654,26 @@ "engines": { "node": ">= 0.8" } + }, + "node_modules/ws": { + "version": "8.16.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz", + "integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } } } } diff --git a/package.json b/package.json index 43f7a81..458690d 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "author": "violet@violets-purgatory.dev", "license": "GPL-3.0-or-later", "dependencies": { - "express": "^4.18.2" + "express": "^4.18.2", + "ws": "^8.16.0" } }